Consider the space between two lines using awk or any

I am trying to write a bash script where I used awk to get a specific field.

Example:

# cat test.txt
USER NAME    LOCATION
Frank        China
Martin       Indonesia
Tom Hanks    United States
Ron Howard   Germany

When I tried to take the first field using awk:

# cat test.txt  | awk '{print $1}'
USER
Frank
Martin
Tom
Ron

But I want a conclusion like

Expected Result

#cat test.txt
USER NAME
Frank        
Martin       
Tom Hanks    
Ron Howard 

I even tried awk -F"\t" '{print $1 (or) $2}', but got only an empty output. I use this file in a while loop.

 #cat test.txt  | awk -F"\t" '{print $2}'

Any hints would be helpful.

+4
source share
1 answer

You can use a delimiter with two or more spaces:

awk -F ' {2,}' '{print $1}' file

USER NAME
Frank
Martin
Tom Hanks
Ron Howard
+4
source

All Articles