stephan
stephan

Reputation: 117

awk delete column in a "sparse" text file

I've a text file with 4 columns like this one

2010-01-03 10:58:00     268435456       
2010-01-03 10:59:00     268435456       
2010-01-03 11:00:00 -134    0       
2010-01-03 11:01:00 -131    0       
...

In column 3 you can find the values I want to keep, in column 4 you can find an error code.

Both columns, however, does not have values in every line. Thus awk '!($4="")' file deletes the 4th column only when the 3rd column exists. Otherwise the error code is written into column 3 and thus mixed up with my observation values.

How can I really discard column 4?

Upvotes: 1

Views: 63

Answers (2)

Claes Wikner
Claes Wikner

Reputation: 1517

Just skip last column.

awk '{$NF=""}1' file

output

2010-01-03 10:58:00 
2010-01-03 10:59:00 
2010-01-03 11:00:00 -134 
2010-01-03 11:01:00 -131 

Upvotes: 1

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8164

"Fields are normally separated by whitespace sequences (spaces, TABs, and newlines), not by single spaces. Two spaces in a row do not delimit an empty field. The default value of the field separator FS is a string containing a single space, " ". If awk interpreted this value in the usual way, each space character would separate fields, so two spaces in a row would make an empty field between them. The reason this does not happen is that a single space as the value of FS is a special case—it is taken to specify the default manner of delimiting fields." from "GAWK: Effective AWK Programming"

Solution, In your case the fields is separated by tabs or spaces

awk -F "[\t ]" '{$4=""}1' file

or

awk -F "[[:blank:]]" '{$4=""}1' file

you get,

2010-01-03 10:58:00  
2010-01-03 10:59:00  
2010-01-03 11:00:00 -134 
2010-01-03 11:01:00 -131 

Upvotes: 2

Related Questions