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

marked as duplicate by Jeff Schaller, MelBurslan, jimmij, roaima, garethTheRed May 18 '16 at 19:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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
  • 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 0:14
1

Another version using cut :

cut -d: -f1,8 file > newfile

Not the answer you're looking for? Browse other questions tagged or ask your own question.