我正在尝试为来自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.
任何帮助纠正这一点将不胜感激.
我正在尝试cat三个文件,并\n
在每个文件后获取并插入换行符,我想到使用类似的东西:
cat f1 f2 f3|tr "\EOF" "\n"
Run Code Online (Sandbox Code Playgroud)
没有成功.
实现这一目标的最简单方法是什么?
我有几个日志包含所有以时间戳开头的行,以便以下工作按预期合并它们:
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) 我在man cat(GNU/Linux)中遇到过这个问题.
手册只是说-u (ignored)没有解释原因.
出于好奇,我在谷歌搜索,但似乎没有人问过这个.
然后我看着其他实现的cat.
我注意到Apple的cat也实现了这个选项(参见cat.c).在它的源代码中说
...
case 'u':
setbuf(stdout, NULL);
break;
...
Run Code Online (Sandbox Code Playgroud)
我想这个选项意味着'无缓冲'.这个选项的使用是什么?它为什么存在,为什么被忽略?我认为必须有一些原因.
有没有办法告诉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().
我想在制表符分隔文件中添加一个标题,但我不知道如何在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不熟悉.
我知道你可以使用"\"来转义特殊字符,但我很有兴趣创建将包含特殊字符的终端命令,这些命令无法很好地读取反斜杠.
作为一个简单的例子,我想要一个看起来像这样的命令:
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")
谢谢.
我有一个写入文件的脚本,如下所示:
cat >myfile <<EOF
some lines
and more lines
EOF
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否是无用的猫......
我有以下命令:
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)
我想抓取文件的内容。我该怎么做呢?