Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract first and last column from text file

Tags:

unix

I have a text file, I only want the first column and the last column. However the problem is they are separated by both whitespace and tabs at random places, making it hard for me to use cut

USER02163 name BAD
USER5415 ab lsi ei GOOD
USER15356 sl oe ow BAD

Desired output is:

USER02163 BAD
USER5415 GOOD
USER15356 BAD

Does anyone have a hacky way I can get my desired output?

like image 420
user1899415 Avatar asked Sep 11 '13 02:09

user1899415


People also ask

How do I extract a column in notepad?

ALT + Left Mouse Click puts you in Column Mode Select. It's quite an useful shortcut that may help you. This is by far the simplest solution. Copy the file, edit the copy, cut out the non-needed columns.

How do I awk my first column?

awk to print the first column. The first column of any file can be printed by using $1 variable in awk. But if the value of the first column contains multiple words then only the first word of the first column prints. By using a specific delimiter, the first column can be printed properly.


1 Answers

Easiest by using awk. The command is:

awk '{print $1, $NF}' filename
like image 102
unxnut Avatar answered Sep 30 '22 15:09

unxnut