Shell – How to use a shell command to only show the first column and last column in a text file


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

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

My feeble attempt at getting the last column to show as well was:

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:


Best Answer

  • Almost there. Just put both column references 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
  • Related Question