简体   繁体   English

如何使用awk,sed或grep将迭代器添加到每行?

[英]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. 因此,基本上将文件的行号插入行本身。

In pure : 用纯

while IFS=, read -r a1 a2 a3 a4; do
    echo "[func (func (func $((c++)))) [$a1 $a2 $a3 $a4]]"
done < file

You can use awk and the NR variable. 您可以使用awkNR变量。

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.

相关问题 如何使用 sed、awk、Z4A037BAC753C8548F24Z、awk、Z4A037BAC753C8548F24 将跨多个文件的一行的 substring 中的空格替换为 %20 - How to substitute spaces with %20 in a substring of a line across multiple files using sed, awk, grep etc 如何使用 SED 或 AWK 在一行中添加双引号? - How to add double quotes to a line with SED or AWK? 如何使用sed / awk / grep ...从文件中提取块,每个块将其保存到文件中 - How to use sed / awk / grep … to extract blocks from a file and each block save it into a file sed / grep / awk? :将匹配模式附加到行尾 - sed / grep / awk? : append matching pattern to end of line 如何使用sed / awk在路径中将每个目录截断为n个字符? - How to truncate each directory to n chars in a path using sed/awk? 如何使用sed,awk或grep从HTML表格单元格中提取数据? - How can I extract data from HTML table cells using sed, awk, or grep? 如何使用grep / regex / cut / awk / sed等提取drbd状态 - How to extract drbd status using grep/regex/cut/awk/sed etc 如何使用 unix/linux grep/awk/sed 从文件中使用空格 output 段落 - how to output paragraphs with spaces from a file using unix/linux grep/awk/sed 使用Grep Sed或Awk搜索和替换EDL文件中的行 - Using Grep Sed or Awk to search and replace lines in EDL file 使用curl和grep / sed / awk在HTML标签中获取时间 - Get time in HTML tags using curl and grep/sed/awk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM