简体   繁体   中英

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"; RS = "" I want to compare first word from first line with first word from second line and so on.

I used 'find' command with 'grep' to find exactly match pattern and it works as I expected. 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"; RS = "" and for loop with condition bounded by NF and display each line separately.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM