我有10个文件。每个文件包含多行,但我对2条特定行感兴趣。unix从文件中提取行并合并
比方说,2号线的格式是:
test1_hello
test2_world
第二个文件包含:
test1_hallo
test2_germany
我想要做的是recursivley跑过来的文件和提取 相关从每个文件到一个主文件的字符串。
所以final_file的最终结果的内容是:
helloworld
hallogermany
这是可能在Bash的一个命令行的?
我有10个文件。每个文件包含多行,但我对2条特定行感兴趣。unix从文件中提取行并合并
比方说,2号线的格式是:
test1_hello
test2_world
第二个文件包含:
test1_hallo
test2_germany
我想要做的是recursivley跑过来的文件和提取 相关从每个文件到一个主文件的字符串。
所以final_file的最终结果的内容是:
helloworld
hallogermany
这是可能在Bash的一个命令行的?
for file in $(ls -1); do grep ^test* $file | awk -F'_' '{print $2}' >> myfile;done
Ferdy,这条命令的输出是每行换行。 – user3532371
这里是什么样的东西你要找的,可能是接近:
for f in * ; do sed 's#[^_]*_##' $f | tr -d '\n' ; echo ; done
sed
将第一_
之前删除的部分(包括_
)由小号 ubstituting的正则表达式[^_]*_
由空字符串组成,而tr
将删除换行符字符(\n
)。用你的例子:
$ cat f1
test1_hello
test2_world
$ cat f2
test1_hallo
test2_germany
$ for f in * ; do sed 's#[^_]*_##' $f | tr -d '\n' ; echo ; done
helloworld
hallogermany
太棒了。你能解释一下sed命令吗 – user3532371
我已经更新了我的答案。 – Pierre
使用bash,csh或类似的东西?或者使用编程语言(C++,python等)。 你的问题不是很好。如果你想写一个linux脚本,那么你应该写在正确的问答 – Dave
和Dave,我的主要目标是通过SSH发送一个bash命令,并最终检索包含所有组合字符串的最终结果文件 – user3532371
最好显示我们自己已经给了你一些工作。你到哪里去了?你有什么尝试? – stef