0

I have a csv file:

test_1,2,data,hi,cat
test_2,3,4,5,6
test_1,3,7,8,9

I want to delete column 3 of the rows which begin with test_1.

I used the cut command to delete column 3 but I do not know how to do it only for a row that begins with test_1.

0

Normally every row of a well formed csv file has the same number of columns, which this would break, but...

awk 'BEGIN{ FS=OFS="," }
     $1 == "test_1" { print $1,$2,$4,$5; next } 1' yourfile
0

With sed:

sed '/^test_1/s/,[^,]*//2' file
  • /^test_1/ search for the lines starting with test_1, then
  • s/,[^,]*//2 replace the second occurrence of comma, followed by any non-comma characters with the empty string
0

Done by below command

awk -F "," '$1 == "test_1" {$3=""}1' filename|sed -r "s/\s+/,/g"

output

test_1,2,hi,cat
test_2,3,4,5,6
test_1,3,8,9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.