I have a tab-delimited text file in the following format.
Col1 | Col2 | Col3
123.0 | 534.2 | Blah0 2031/23/12
23.00 | 786.2 | Blah1 2033/01/01
12.40 | 343.0 | Blah2 2031/27/11
I need to remove all the characters after the space from the last column. So my output would be
Col1 | Col2 | Col3
123.0 | 534.2 | Blah0
23.00 | 786.2 | Blah1
12.40 | 343.0 | Blah2
How should I go about this using Awk
or something similar?
With awk:
awk -F '\t' 'BEGIN { OFS = FS } NR != 1 { sub(/ [^ ]*$/, "", $NF) } 1' filename
That is:
BEGIN { OFS = FS } # the output should be separated the same way as
# the input
NR != 1 { # in all lines except the header line:
sub(/ [^ ]*$/, "", $NF) # replace the last space and everything after it
} # in the last field ($NF) with the empty string
# (i.e., remove it)
1 # in all lines: print.
If there can be several spaces in the last field and you want to remove everything after the first space, use sub(/ .*/, "", $NF)
instead. It wasn't entirely clear in the question what should happen in such a case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.