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
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 commaReplacement:
\1: Back-reference to captured group #1sed 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With