sanigirl
sanigirl

Reputation: 541

Cut first and last line field

I'm using grep to pull specific lines from multiple files and I'm trying to use cut to get the first and last field then use sort to get unique lines.

I can cut the first or last field using:

command output | rev | cut -f1 | rev
command output | cut -d(delimiter) -f1

I'm failing to find any documentation or previous questions that allow me to cut both fields in one shot with the number of fields in between being inconsistent.

The output before cut looks like:

object garble garble #
object garble garble #
object garble garble garble #
object garble garble #

And the output I need is:

object #
object #
object #
object #

Note that the value of 'garble' is inconsistent as well, they represent notes made.

Upvotes: 2

Views: 2583

Answers (1)

heemayl
heemayl

Reputation: 42017

This is an easy job for awk, get the first and last field:

awk '{print $1, $NF}' file.txt

To preserve the field separator:

awk 'BEGIN{OFS=FS} {print $1, $NF}' file.txt

Upvotes: 3

Related Questions