1

I have a text file with 9 fields separated by :

survey:m1111771::rent:travel:::Morning:

How do I pull field 1 and 8 only to a separate file

for example it would look like this,

survey:Morning
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
riley
  • 351

2 Answers2

2

One of many ways:

$ awk -F: '{print $1":"$8}' <file>
survey:Morning

and as @cas as pointed out, where OFS is the "Output Field Separator"

$ awk -F: -v OFS=: '{print $1,$8}'
survey:Morning
KM.
  • 2,224
  • 1
    hard-coding a single double-quoted : character is OK when you're only printing two fields, but it gets tedious and error-prone very quickly if you want to print more fields. It's better to set the Output Field Separator (OFS) to :. e.g. awk -F: -v OFS=: '{print $1,$8}'. hard-coding it is also a pain if you ever want to change it (e.g. to a tab or a comma), as you'd then have to manually change every occurrence. – cas May 19 '16 at 00:14
1

Another version using cut :

cut -d: -f1,8 file > newfile
magor
  • 3,752
  • 2
  • 13
  • 28