Delete a column from a delimited file in linux

2020-02-12 04:55发布

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

标签: linux unix awk sed
7条回答
何必那么认真
2楼-- · 2020-02-12 05:15

Here's a possible sed solution:

sed -i.bak filename -e 's;\(^.*|.*|\).*|\(.*\);\1\2;'

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 called filename.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.

查看更多
小情绪 Triste *
3楼-- · 2020-02-12 05:19

This might work for you (GNU sed):

sed 's/[^|]*|//3' file
查看更多
老娘就宠你
4楼-- · 2020-02-12 05:22

Another awk solution could be useful if you have many columns

awk -F'|'  '{$3="";$0=$0;$3=$3}1' FPAT='[^|]+' OFS='|' file
查看更多
男人必须洒脱
5楼-- · 2020-02-12 05:26

cut command will help to achieve this

 cat filname | cut -d'|' -f1,2,4
查看更多
看我几分像从前
6楼-- · 2020-02-12 05:29

You could simply use cut.

cut -d'|' -f1-2,4- file
查看更多
forever°为你锁心
7楼-- · 2020-02-12 05:37
awk  'BEGIN{FS=OFS="|"}{print $1,$2,$4}'   file

should give you the output.

it is the very basic awk usage.

edit

you didn't mention 70 columns... :(

try this:

awk  -F'|' '{s="";for(i=1;i<=NF;i++){f=(NF==i)?"":FS;if(i!=3)s=s $i f;}print s}' file
查看更多
登录 后发表回答