[英]print only first word using awk command when RS=“” and FS=“\n”
Need help with awk command to print only first word of line when using FS ="\\n"; 在使用FS =“ \\ n”时,需要awk命令帮助仅打印行的第一个单词; RS = "" I want to compare first word from first line with first word from second line and so on.
RS =“”我想比较第一行的第一个单词和第二行的第一个单词,依此类推。
I used 'find' command with 'grep' to find exactly match pattern and it works as I expected. 我将'find'命令与'grep'一起使用以找到完全匹配的模式,并且按我的预期工作。 The return of this command is one big string which I set this string to awk command to split it line by line by using FS ="\\n";
该命令的返回是一个大字符串,我将该字符串设置为awk命令,以使用FS =“ \\ n”逐行拆分它; RS = "" and for loop with condition bounded by NF and display each line separately.
RS =“”并以NF为边界的条件循环,并分别显示每行。
find . -name 21* -path "*/path1/*" -exec grep -FHI -A 4 -we "\$dup" {} \; | egrep "(A|B)_param" | sort | uniq -c | sort -nrk 5 | awk 'BEGIN{FS ="\n"; RS = ""}{ for (i = 1; i <= NF; i++) {print i $i}}'
Need print only first word from line number $i and compare with first word from line number $i+1. 仅需要打印行号$ i中的第一个单词,并与行号$ i + 1中的第一个单词进行比较。
Got: 得到:
128 ./debug/21.file1.log- parameter \B_param 129 128 ./debug/21.file1.log- parameter \A_param 129 34 ./debug/21.file1.log- parameter \B_param 128 34 ./debug/21.file1.log- parameter \A_param 128 2 ./debug/21.file1.log- parameter \B_param 66 3 ./debug/21.file1.log- parameter \A_param 66 64 ./debug/21.file1.log- parameter \B_param 65 65 ./debug/21.file1.log- parameter \A_param 65 1 ./debug/21.file1.log- parameter \B_param 65 1 ./debug/21.file1.log- parameter \A_param 65
Need: 需要:
128
128
34
34
2
3
64
65
1
1
and compare in such way: 并以这种方式进行比较 :
128==128 ? //equal do nth 34==34 ? //equal do nth 2==3 ? //not equal so print line $i (2 ./debug/21.file1.log- parameter \B_param 66) 64==65 ? //not equal print line $i (64 ./debug/21.file1.log- parameter \B_param 65) 1==1 ? //equal do nth
Could not test it since structure is not present, could you please try following. 由于没有结构,因此无法测试,请尝试以下方法。 Where I am removing
$i
from your code and printing only i
, which may give results as per your need. $i
将从您的代码中删除$i
并仅打印i
,这可能会根据您的需要提供结果。
find . -name 21* -path "*/path1/*" -exec grep -FHI -A 4 -we "\$dup" {} \; | egrep "(A|B)_param" | sort | uniq -c | sort -nrk 5 | awk 'BEGIN{FS ="\n"; RS = ""}{ for (i = 1; i <= NF; i++) {print i}}'
Or if above doesn't work then try following. 或者,如果上述方法不起作用,请尝试以下方法。
find . -name 21* -path "*/path1/*" -exec grep -FHI -A 4 -we "\$dup" {} \; | egrep "(A|B)_param" | sort | uniq -c | sort -nrk 5 | awk 'BEGIN{FS ="\n"; RS = ""}{ for (i = 1; i <= NF; i++) {split(i,array," ");print array[1]}}'
echo "Got text" | awk '
(NR % 2) == 1 {prev_id=$1; prev_line=$0; next}
$1 != prev_id {print prev_line}
'
2 ./debug/21.file1.log- parameter \B_param 66
64 ./debug/21.file1.log- parameter \B_param 65
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.