标签: cat

在bash脚本循环中打印cat语句的输出

我正在尝试为来自cat命令的每一行执行命令.我的基础是我从供应商处获得的示例代码.

这是脚本:

for tbl in 'cat /tmp/tables'
do
   echo $tbl
done
Run Code Online (Sandbox Code Playgroud)

所以我期待输出是文件中的每一行.相反,我得到了这个:

cat
/tmp/tables
Run Code Online (Sandbox Code Playgroud)

这显然不是我想要的.

我将用一个与数据库连接的实际命令替换echo.

任何帮助纠正这一点将不胜感激.

linux bash for-loop cat

10
推荐指数
2
解决办法
4万
查看次数

bash cat多个文件

我正在尝试cat三个文件,并\n 在每个文件后获取并插入换行符,我想到使用类似的东西:

cat f1 f2 f3|tr "\EOF" "\n"
Run Code Online (Sandbox Code Playgroud)

没有成功.

实现这一目标的最简单方法是什么?

linux bash shell cat

10
推荐指数
3
解决办法
1万
查看次数

按日期合并多个日志文件,包括多行

我有几个日志包含所有以时间戳开头的行,以便以下工作按预期合并它们:

cat myLog1.txt myLog2.txt | sort -n > combined.txt
Run Code Online (Sandbox Code Playgroud)

问题是,myLog2.txt还可以包含没有时间戳的行(例如java堆栈跟踪).是否有一种简单的方法,没有任何自定义脚本仍然合并它们并保留多行内容?

示例myLog1.txt

11:48:18.825 [main] INFO  org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
11:48:55.784 [main] INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema
Run Code Online (Sandbox Code Playgroud)

示例myLog2.txt

11:48:35.377 [qtp1484319352-19] ERROR c.w.b.c.ControllerErrorHandler -
org.springframework.beans.TypeMismatchException: Failed to convert value of type   'java.lang.String' to required type 'org.joda.time.LocalDate'; nested exception is    org.springframework.core.convert.ConversionFailedException: Failed to convert from type     java.lang.String to type @org.springframework.web.bind.annotation.RequestParam   @org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDate for value    '[2013-03-26]'; nested exception is java.lang.IllegalArgumentException: Invalid format: "    [2013-03-26]"
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:68) ~[spring-beans-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:45) ~[spring-beans-3.2.1.RELEASE.jar:3.2.1.RELEASE] …
Run Code Online (Sandbox Code Playgroud)

sorting logging command-line log-files cat

10
推荐指数
1
解决办法
8380
查看次数

cat命令中"-u"选项的用途是什么?

我在man cat(GNU/Linux)中遇到过这个问题.
手册只是说-u (ignored)没有解释原因.
出于好奇,我在谷歌搜索,但似乎没有人问过这个.
然后我看着其他实现cat.
我注意到Apple的cat也实现了这个选项(参见cat.c).在它的源代码中说

...
case 'u':
    setbuf(stdout, NULL);
    break;
...
Run Code Online (Sandbox Code Playgroud)

我想这个选项意味着'无缓冲'.这个选项的使用是什么?它为什么存在,为什么被忽略?我认为必须有一些原因.

c unix linux cat

10
推荐指数
4
解决办法
1087
查看次数

用cat读取:不接收数据时停止

有没有办法告诉cat命令在没有收到任何数据时停止阅读?可能有一些"超时",指定没有数据传入多长时间.

有任何想法吗?

unix linux shell sh cat

9
推荐指数
2
解决办法
5037
查看次数

在Python中重现Unix cat命令

我目前正在复制以下Unix命令:

cat command.info fort.13 > command.fort.13
Run Code Online (Sandbox Code Playgroud)

在Python中使用以下内容:

with open('command.fort.13', 'w') as outFile:
  with open('fort.13', 'r') as fort13, open('command.info', 'r') as com:
    for line in com.read().split('\n'):
      if line.strip() != '':
        print >>outFile, line
    for line in fort13.read().split('\n'):
      if line.strip() != '':
        print >>outFile, line
Run Code Online (Sandbox Code Playgroud)

这是有效的,但必须有一个更好的方法.有什么建议?

编辑(2016):

四年后,这个问题又开始受到关注.我在这里用更长的Jupyter笔记本写了一些想法.

问题的关键在于我的问题与(我意想不到的)行为有关readlines.我可以更好地回答我的目标,这个问题可以更好地回答read().splitlines().

python cat

9
推荐指数
3
解决办法
3万
查看次数

将标头添加到制表符分隔文件

我想在制表符分隔文件中添加一个标题,但我不知道如何在linux中的一行中添加它.

我们说我的文件是:

roger\t18\tcolumbia\tnew york\n
albert\t21\tdartmouth\tnew london\n
etc...
Run Code Online (Sandbox Code Playgroud)

现在我想添加一个标题:

name\tage\tuniversity\tcity
Run Code Online (Sandbox Code Playgroud)

我怎么能在linux的一行中做到这一点?我很好用awk,sed,cat等等,虽然对perl不熟悉.

unix linux awk sed cat

9
推荐指数
3
解决办法
4万
查看次数

是否有R等价的其他语言三重引号?

我知道你可以使用"\"来转义特殊字符,但我很有兴趣创建将包含特殊字符的终端命令,这些命令无法很好地读取反斜杠.

作为一个简单的例子,我想要一个看起来像这样的命令:

echo hello "w" or'l'd
Run Code Online (Sandbox Code Playgroud)

这可以通过类似的东西来实现

system(command="""echo hello "w" or'l'd""")
Run Code Online (Sandbox Code Playgroud)

但R不处理三重引号.还有另外一种方法吗?即使从cat()捕获输出也没关系.例如newCommand = cat("echo hello \"w \"orld")

谢谢.

r system cat

9
推荐指数
1
解决办法
1607
查看次数

我需要cat将heredoc写入文件吗?

我有一个写入文件的脚本,如下所示:

cat >myfile <<EOF
some lines
and more lines
EOF
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否是无用的猫......

bash cat

8
推荐指数
1
解决办法
2867
查看次数

如何在管道中使用猫

我有以下命令:

httpd.conf | grep AuthUserFile | cut -d" " -f4 | sed -e 's|["'\'']||g' 
Run Code Online (Sandbox Code Playgroud)

其输出是:

/etc/httpd/secure/htpasswd.training
Run Code Online (Sandbox Code Playgroud)

我做了:

httpd.conf | grep AuthUserFile | cut -d" " -f4 | sed -e 's|["'\'']||g'| cat 
Run Code Online (Sandbox Code Playgroud)

然而这刚刚返回:

/etc/httpd/secure/htpasswd.training
Run Code Online (Sandbox Code Playgroud)

我想抓取文件的内容。我该怎么做呢?

unix bash cut pipe cat

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

cat ×10

linux ×5

bash ×4

unix ×4

shell ×2

awk ×1

c ×1

command-line ×1

cut ×1

for-loop ×1

log-files ×1

logging ×1

pipe ×1

python ×1

r ×1

sed ×1

sh ×1

sorting ×1

system ×1