Select the last field of a line with uniq count equal to one

55 views Asked by At

I am writing a bash script and I have a file like this :

      3 5d74727d50368c4741d76989586d91de  ./2.txt
      3 63ce7868edd509987925e98e129e232a  ./3.txt
      2 9fc16dbef316035af400fac82aba9c36  ./fff/fff/fff/fff
      3 d41d8cd98f00b204e9800998ecf8427e  ./1
      1 d41d8cd98f00b204e9800998ecf8427e  ./11.txt
      3 d41d8cd98f00b204e9800998ecf8427e  ./12/1
      3 d41d8cd98f00b204e9800998ecf8427e  ./12/1.txt
      3 d41d8cd98f00b204e9800998ecf8427e  ./12/2.txt
      1 d41d8cd98f00b204e9800998ecf8427e  ./13
      3 d41d8cd98f00b204e9800998ecf8427e  ./4.txt
      3 d41d8cd98f00b204e9800998ecf8427e  ./5.txt
      1 d41d8cd98f00b204e9800998ecf8427e  ./6.txt
      3 d41d8cd98f00b204e9800998ecf8427e  ./ala
      1 d41d8cd98f00b204e9800998ecf8427e  ./a.txt
      3 d41d8cd98f00b204e9800998ecf8427e  ./b/c/d/e/f/ass
      3 d41d8cd98f00b204e9800998ecf8427e  ./kota
      1 d41d8cd98f00b204e9800998ecf8427e  ./ma
      2 d41d8cd98f00b204e9800998ecfkkkkk  ./6.txt
      2 d41d8cd98f00b204e9800998ecflllll  ./a.txt
      2 d41d8cd98f00b204e9800998ecfmmmmm  ./ma

that is an effect of a this command:

cat file1 file2 file2 | sort | uniq -c

Now I would like to select lines with count equal to 1 and then print only the last field of the line so the desired effect would be :

./11.txt
./13
./6.txt
./a.txt
./ma

I am not that comfortable with bash and I was wondering if there is a simple solution to that.

2

There are 2 answers

1
Inian On BEST ANSWER

You can do this in Awk simply as

awk '$1=="1"{print $NF}' file

meaning, in Awk $1 is the first field in each line split by the delimiter (if no other options are set single white-space being the default). We look into the lines containing first field as 1 and in those lines the action clause is to print the last field (represented by $NF in Awk)

1
RavinderSingh13 On

try following too once and let me know if this helps you.

awk '{printf("%s",$1==1?$NF RS:"")}'  Input_file

Using printf to print, checking there if first field is 1 then print last column with new line(RS is record separator whose default value is new line) else print ""(NULL).