2

I would like to delete column 3 and keep the same structure on output file.

input file

12,10,10,10 10,1
12,23 1,45,6,7
11  2,33,45,1,2
1,2,34,5,6

I tried

awk -F, '!($3="")' file | awk -v OFS=',' '{$1=$1}1'
12,10,10,10,1
12,23,1,6,7
11,2,33,1,2
1,2,5,6

Output desired

12,10,10 10,1
12,23 1,6,7
11  2,33,1,2
1,2,5,6

Appreciate your help

Cyrus
  • 84,225
  • 14
  • 89
  • 153
OXXO
  • 724
  • 5
  • 12

4 Answers4

3

Better to use sed here:

sed -E 's/^(([^,]*,){2})[^,]*,/\1/' file

12,10,10 10,1
12,23 1,6,7
11  2,33,1,2
1,2,5,6

Search regex:

  • ^: Start
  • (: Start 1st capture group
    • (: Start 2nd capture group
    • [^,]*,: Match 0 or more non-comma characters followed by a comma
    • ){2}: End 2nd capture group. {2} means match 2 pairs of above match
  • ): End 1st capture group
  • [^,]*,: Match 0 or more non-comma characters followed by a comma

Replacement:

  • \1: Back-reference to captured group #1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • anubhava, please can you explain how the code works ( the logic ), it can work to able to delete any column ?. In my input example i show only 5 columns but this can be more? .. Appreciate your comments.. – OXXO Dec 14 '17 at 15:14
  • I have added detailed explanation in my answer. Let me know if there is any further doubt. – anubhava Dec 14 '17 at 15:31
  • 1
    anuhava, works perfectly, many thanks for the explanation in the code – OXXO Dec 14 '17 at 19:59
2

sed solution:

sed -E 's/^([^,]+,[^,]+,)[^,]+,/\1/' file
  • ^ - start of the string
  • [^,]+ - match any char(s) except ,
  • \1 - points to the 1st captured parenthesized group (...)

The output:

12,10,10 10,1
12,23 1,6,7
11  2,33,1,2
1,2,5,6
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
2

In awk:

$ awk 'BEGIN{FS=OFS=","}{for(i=3;i<NF;i++)$i=$(i+1);NF--}1' file
12,10,10 10,1
12,23 1,6,7
11  2,33,1,2
1,2,5,6
James Brown
  • 36,089
  • 7
  • 43
  • 59
2

Perl solution:

perl -l -aF/,/ -ne 'splice @F, 2, 1; print join ",", @F'
  • -l removes newlines from input and adds them to prints
  • -n reads the input line by line
  • -a splits each line into the @F array
  • -F specifies how to split (/,/ means on comma)
  • splice removes elements from arrays, in this case it removes 1 element at position 2.
choroba
  • 231,213
  • 25
  • 204
  • 289