I have a file in the following format:
col1|col2|col3|col4
a|b|c|d
e|f||h
i|j|k|l
I would like to delete col3 (with the delimiter "|") from the header and the data as well. Can this be done using awk/sed?
Plese NOTE that the data in col3 maybe empty (row 2).
The output should be:
col1|col2|col4
a|b|d
e|f|h
i|j|l
Here's a possible sed solution:
This will work great for your example, and could be adjusted for other examples, but isn't really a general purpose solution.
Explanation:
-i.bak
Edit the file in place, first making a backup calledfilename.bak
.\(^.*|.*|\)
From the start of the line, match everything up to and including the second delimiter. The parenthesis group this match (group 1)..*|
Match everything up to and including the last delimiter.\(.*\)
Match the rest and group (group 2).\1\2
Replace all of the previous matches with the text from group 1 and group 2.This might work for you (GNU sed):
Another
awk
solution could be useful if you have many columnscut command will help to achieve this
You could simply use
cut
.should give you the output.
it is the very basic awk usage.
edit
you didn't mention 70 columns... :(
try this: