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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityOne 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
:
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.