Extract first and last columns from text file

I have a text file, I only need the first column and the last column. However, the problem is that they are separated by spaces and tabs in random places, which makes it difficult to use cut

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

Required Conclusion:

 USER02163 BAD USER5415 GOOD USER15356 BAD 

Does anyone have a hacker way to get the desired result?

+6
source share
3 answers

The easiest way to use awk . Command:

 awk '{print $1, $NF}' filename 
+12
source

When you specify a field separator as a space in awk, it will contain one and more spaces. So you can use this,

 awk -F " " '{print $1,$NF}' file 

Same as unxnut. Added a field separator that gives a clear view.

+1
source

Use this sed command

 $ sed -r 's/([^ ]*) (.*) (.*$)/\1 \3/' filename 

Output:

  USER02163 BAD USER5415 GOOD USER15356 BAD 
+1
source

Source: https://habr.com/ru/post/953600/


All Articles