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:
foo|dog|cat|mouse|lion|ox|tiger|bar
Best Answer
Almost there. Just put both column references next to each other.
Also note that you don't need
cat
here.Also note you can tell
awk
that the column separators is|
, instead of blanks, so you don't needsed
either.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
.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.You can assign these variables when calling
awk
from the command line, via the-v
flag.or simply: