Awk 两个LinuxBash案例
应该是这样的:Awk 两个LinuxBash案例,awk,Awk,应该是这样的: ways ric ighl ft not anne hope 我使用了'awk'{print NR'.$s}'1]使用tac来逐行反转文件的内容 4. ft 3. not 1. hope 2] 链接了一系列3个命令 awk '{print NR ". " $s}' filename.txt | tac 对于第一个问题,如果它是一个小文件,一个解决方案是 echo "yesyesyes:"; cat filename.txt; tail -1 filename.txt
ways
ric
ighl
ft
not
anne
hope
我使用了'awk'{print NR'.$s}'1]使用tac来逐行反转文件的内容
4. ft
3. not
1. hope
2] 链接了一系列3个命令
awk '{print NR ". " $s}' filename.txt | tac
对于第一个问题,如果它是一个小文件,一个解决方案是
echo "yesyesyes:"; cat filename.txt; tail -1 filename.txt
使用tac
$ awk 'NR==FNR{n=NR; next} {print (n-FNR+1)".", $0}' file{,}
或
将多个问题捆绑在一个问题中不是一个好主意,因此跳过下一个问题。也许你应该为这个问题创建一个单独的问题。1)使用tac
:
$ tac file | nl -nln -w1 -s'. ' | tac
$ tac file | cat -n | tac
7 ways
6 ric
5 ighl
4 ft
3 not
2 anne
1 hope
$ tac file | awk -v OFS='. ' '{print NR, $0}' | tac
7. ways
6. ric
5. ighl
4. ft
3. not
2. anne
1. hope
1) 无tac
:
$ tac file | nl -nln -w1 -s'. ' | tac
$ tac file | cat -n | tac
7 ways
6 ric
5 ighl
4 ft
3 not
2 anne
1 hope
$ tac file | awk -v OFS='. ' '{print NR, $0}' | tac
7. ways
6. ric
5. ighl
4. ft
3. not
2. anne
1. hope
如何使新行看起来像hope(新行)hope而不是hopehope你可以在tail命令之前回显新行。但是如果有任何文件的末尾可能已经有了换行符,并且您不想重复。阅读:思考
$s
的含义以及它产生任何输出的原因。接下来,请一次只问一个问题。请看。为什么您编辑了您的问题,以至于发布的预期输出现在对我们的答案毫无意义?
$ cat -n file | sort -k1,1rn | cut -f2- | cat -n | sort -k1,1rn
7 ways
6 ric
5 ighl
4 ft
3 not
2 anne
1 hope
$ awk -v OFS='. ' '{buf[NR]=$0} END{for (i=1; i<=NR; i++) print NR-i+1, buf[i]}' file
7. ways
6. ric
5. ighl
4. ft
3. not
2. anne
1. hope
$ awk 'BEGIN{print "yesyesyes"} {print; last=$0} END{print last}' file
yesyesyes
ways
ric
ighl
ft
not
anne
hope
hope
$ awk 'BEGIN{prev="yesyesyes"} {print prev; prev=$0} END{print prev ORS prev}' file
yesyesyes
ways
ric
ighl
ft
not
anne
hope
hope