据我所知,grep可以逐行从文件中提取特定内容。 只是想知道如何在每行之前或之后添加另一列作为索引。 例如:
grep "aaa" text.txt > tmp.txt
在tmp.txt文件中,我们可以看到如下内容,
aaawekjre
qejrteraaa
wrgeaaaere
但是,我想添加一个特定的索引作为额外的列。 因此,tmp.txt可能如下所示:
John aaawekjre
John qejrteraaa
John wrgeaaaere
答案 0 :(得分:8)
您可以使用awk:
awk '/aaa/{print "John", $0}' text.txt > tmp.txt
答案 1 :(得分:6)
method1 ()
{
//remote functon call
remoteFunction( input);
// i want to get the input to method2 and set the method2Output here
// so that the set method2Output will be returned mt method2()
// i want method 2 to wait till method1 gets the method2 input and sets the
// method2Output and then method2 should return the method2Output .
}
remoteFunction(Input input)
{
// some processing
method2(output)
}
public method2Output method2 (Output output)
{
// processing done my method1
return method2Output ;
}
$ sed -n '/aaa/ s/^/John /p' text.txt
John aaawekjre
John qejrteraaa
John wrgeaaaere
除非我们明确要求,否则告诉sed不要打印任何内容。
-n
这会选择包含/aaa/ s/^/John /p
的行。对于这些行,我们进行替换(aaa
)以将s/^/John /
放在行的开头,然后打印行(John
)。
这样,永远不会打印不包含p
的行。因此,不需要单独的aaa
流程。
答案 2 :(得分:2)
试试这个
grep "aaa" text.txt | awk '{print "John " $0}' > tmp.txt