使用sed
我想通过在每行末尾添加当前行数来处理文件。
例如,给定textfile.txt
,包含:
$ cat textfile.txt
word
letters
music
变为:
$ sed '<magic sed commands>' textfile.txt
word line1
letters line2
music line3
例如,在awk
中,我可以按照以下方式执行此操作:
$ awk '{print $1"\tline"NR;}' textfile.txt
word line1
letters line2
music line3
我已尝试在sed中使用=
玩,但它在替换's///'
块中并未获得荣誉,即's///; ='
有效,但{{1}显然没有('s//=/'
按字面解释)。
答案 0 :(得分:2)
尝试使用GNU sed:
sed -n 'p;=' file | sed 'N;s/\n/\tline/'
输出:
word line1 letters line2 music line3
答案 1 :(得分:0)
grep -n '' YourFile | sed 's/^\([0-9]*\):\(.*\)/\2 line \1/'
因为sed不允许在1个sed动作脚本中轻松完成任务,我用grep和sed编号后行。
可以用sed编号(=
无法帮助,因为它将信息发送到输出而不是工作缓冲区,所以没有对它进行任何操作)但是其他工具在绞索中操作的非常繁重的操作(像awk一样快速替代这里)
awk '{print $0 " line " NR}' YourFile