I need some help to figure out how to use the sed command to only show the first and last column in a text file Here's what i have so far for column 1

cat logfile | sed 's/\|/ /'|awk '{print $1}'

My poor effort at showing the last column was unsuccessful

cat logfile | sed 's/\|/ /'|awk '{print $1}{print $8}'

However this takes the first column and last column and merges them together in one list. Is there a way to print the first column and last columns clearly with sed and awk commands?

Sample input.

foo|dog|cat|mouse|lion|ox|tiger|bar
Best Answer


It's almost there You'll put the columns next to each other

cat logfile | sed 's/|/ /' | awk '{print $1, $8}'

Also note that you don't need cat here.

sed 's/|/ /' logfile | awk '{print $1, $8}'

Also note you can tell awk that the column separators is | , instead of blanks, so you don't need sed either.

awk -F '|' '{print $1, $8}' logfile

As per suggestions by Caleb, if you want a solution that still outputs the last field, even if there are not exactly eight, you can use $NF .

awk -F '|' '{print $1, $NF}' logfile

Also, if you want the output to retain the | separators, instead of using a space, you can specify the output field separators. Unfortunately, it's a bit more clumsy than just using the -F flag, but here are three approaches.

  • You can assign the input and output field separators in awk itself, in the BEGIN block.

    awk 'BEGIN {FS = OFS = "|"} {print $1, $8}' logfile
    
  • You can assign these variables when calling awk from the command line, via the -v flag.

    awk -v 'FS=|' -v 'OFS=|' '{print $1, $8}' logfile
    
  • or simply:

    awk -F '|' '{print $1 "|" $8}' logfile