Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a column from a delimited file in linux

Tags:

linux

unix

sed

awk

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
like image 961
MyFirstName MyLastName Avatar asked Oct 03 '12 20:10

MyFirstName MyLastName


2 Answers

You could simply use cut.

cut -d'|' -f1-2,4- file
like image 60
tripleee Avatar answered Nov 07 '22 13:11

tripleee


This might work for you (GNU sed):

sed 's/[^|]*|//3' file
like image 45
potong Avatar answered Nov 07 '22 12:11

potong