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
0

2 Answers 2

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
  • 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
    Commented May 19, 2016 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 .