[英]How to add iterator to each line using awk, sed or grep?
How would I use grep, sed or awk to insert an iterator to each line (row), the length of the file? 我将如何使用grep,sed或awk在每行(行)(文件的长度)中插入迭代器?
Say my data looks like this, in csv format: 说我的数据看起来像这样,以csv格式:
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
.
.
.
1,2,3,4
I want to make it look like this, using grep, awk or sed (or whatever else works): 我想使用grep,awk或sed(或其他有效方法)使其看起来像这样:
[func (func (func 0)) [1 2 3 4]]
[func (func (func 1)) [1 2 3 4]]
[func (func (func 2)) [1 2 3 4]]
.
.
.
[func (func (func N)) [1 2 3 4]]
Where N
is the number of rows in the file. 其中N
是文件中的行数。 So basically inserting the line number, of the file, into the line itself. 因此,基本上将文件的行号插入行本身。
You can use awk
and the NR
variable. 您可以使用awk
和NR
变量。
Something like this: 像这样:
awk '{ gsub(/,/, " "); print "[func (func (func "(NR-1)")) ["$0"]]"; }' datafile
This might work for you (GNU sed): 这可能对您有用(GNU sed):
sed = file | sed 'N;s/\(.*\)\n\(.*\)/[func (func (func \1)) [\2]/'
This inserts the line number into the output stream (Stdout) and then uses a second invocation of sed to pair it to its line and output the required string. 这会将行号插入输出流(Stdout),然后使用sed的第二次调用将其与行配对,并输出所需的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.