Delete a column from a delimited file in linux

前端 未结 7 1048
星月不相逢
星月不相逢 2020-12-19 02:43

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 \"|\") fr

7条回答
  •  爱一瞬间的悲伤
    2020-12-19 03:09

    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
    

提交回复
热议问题