Need to print some particular line

I have a file which have lots of line.
delimiter is |
i am counting the delimiter and want to print some lines on basis of this count.
here is my command.

$ awk -F\| '{print NF}' abc.txt | sort -u 
5
99
2

Now want to show the record count also as per this delimiter.

$ awk -F\| '{print NF}' abc.txt | sort | uniq -c 
  2 5
266 99
  5 2

Now i want to print those 5 records which have 2 delimiter.

Please suggest any command here.

Hello BithunC,

Could you please let us know the sample input of your requirement with sample output as requirement is not clear, following are some points on same.

 awk -F"|" '{print NF}' abc.txt    ### Means it will print number of records as per line.
 

When you are using command:

 awk -F\| '{print NF}' abc.txt | sort -u #### Which means it is just simply sorting the output of first awk command which simply shows the number of fields of each line. So if you want to sort the actual output which I think you may require then above command may not help you.
 

Also for following command:

 awk -F\| '{print NF}' abc.txt | sort | uniq -c   ##### This command again first print the number of each files by awk then sorting it and then counting like how many number of fields are present in whole file, eg--> 2 5 in output means 5 number of fields occurred in file for 2 times and so on in your shown output.
 

Could you please be more specific in your requirement so that we may help you on same.

Thanks,
R. Singh

Sorted by field

perl -nlaF'\|' -e '$NF{$F[-1]}++; END{for $f (sort keys %NF){print "$NF{$f} $f"}}' BithunC.file

or not sorted

perl -nlaF'\|' -e '$NF{$F[-1]}++; END{for $f (keys %NF){print "$NF{$f} $f"}}' BithunC.file

According to the data you have shown us above, there are no lines with 2 delimiters. There are 2 lines with 4 delimiters (AKA 5 fields), there are 266 lines with 98 delimiters (AKA 99 fields), and 5 lines with 1 delimiter (AKA 2 fields).

To print the 5 lines with 2 fields, try:

awk -F'|' 'NF==2' abc.txt

To see that there are no lines with 2 delimiters, try:

awk -F'|' 'NF==3' abc.txt

Thanks Don, its perfectly workingh :slight_smile: