简体   繁体   中英

How to print a line with no field separator in awk?

I have data like this (file is called list-in.dat )

a ; b ; c ; i
d
e ; f ; a ; b
g ; h ; i

and I want a list like this (output file list-out.dat ) with all items, in alphabetically order (case insensitive) and each unique item only once.

a
b
c
d
e
f
g
h
i

My attempt is:

awk -F " ; " ' BEGIN { OFS="\n" ; } {for(i=0; i<=NF; i++) print $i} ' file-in.dat | uniq | sort -uf > file-out.dat

But I end up with all antries except those lines which has only one item:

a
b
c
e
f
g
h
i

How can I get all (unique, sorted) items no matter how many items are in one line / if the field separator is missing?

Using gnu-awk :

awk -F '[[:blank:]]*;[[:blank:]]*' '{
   for (i=1; i<=NF; i++) uniq[$i]
}
END {
   PROCINFO["sorted_in"]="@ind_str_asc"
      for (i in uniq)
         print i
}' file
a
b
c
d
e
f
g
h
i

For non-gnu awk use:

awk -F '[[:blank:]]*;[[:blank:]]*' '{for (i=1; i<=NF; i++) uniq[$i]} 
END{for (i in uniq) print i}' file | sort 
awk -F' ; ' -v OFS='\n' '{$1=$1} 1' ip.txt | sort -fu
  • -F'; ' -F'; ' sets space followed by ; followed by space as field separator
  • -v OFS='\n' sets newline as output field separator
  • {$1=$1} change $0 as per new OFS
  • 1 print $0
  • sort -fu sort uniquely ignoring case in alphabetic order

Could you please try following, awk + sort solution, written and tested with shown samples. In case you want to use ignorecase then add IGNORECASE=1 in awk code.

awk '
BEGIN{
  FS=" ; "
}
{
  for(i=1;i<=NF;i++){
    if(!a[$i]++){  print $i  }
  }
}
'  Input_file | sort

Explanation: Adding detailed explanation for above.

awk '                            ##Starting awk program from here.
BEGIN{                           ##Starting BEGIN section of this program from here.
  FS=" ; "                       ##Setting field separator as space semi-colon space here.
}
{
  for(i=1;i<=NF;i++){            ##Starting a for loop till NF here for each line.
    if(!a[$i]++){  print $i  }   ##Checking condition if current field is NOT present in array a then printing that field value here.
  }
}
'  Input_file | sort             ##Mentioning Input_file name here and passing it to sort as Input to sort the data.

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