2

I have a file where the fields are encapsulated with double quotes - general example of file:

"Internal ID", "External ID", "Name"
"123", "ABC", "ABC Incorporated"

Desired outcome, removing first column and saving the file back with the same name:

"External ID", "Name"
"ABC", "ABC Incorporated"

Can the above sed be modified to handle the "," delimiter? If so, how? Or are there better alternatives? Still a shell noob but I tried the following with no success:

    sed -i 's/[^'\"','\"']*,//' file.csv

Any help be greatly appreciated.

lojkyelo
  • 121
  • 3
  • 15

4 Answers4

3

This is a job for cut.

 cut -d, -f 2- < file.csv

Use the shell to rename the output. Or use the script named inline at sed edit file in place and invoke cut as:

inline file.csv cut -d, -f 2-
Community
  • 1
  • 1
William Pursell
  • 204,365
  • 48
  • 270
  • 300
2

You could try the below sed command,

$ sed 's/^.*\(".*", ".*"\)$/\1/' file
"External ID", "Name"
"ABC", "ABC Incorporated"

It prints the last two values.

$ sed 's/^"[^"]*", //' file
"External ID", "Name"
"ABC", "ABC Incorporated"

It removes the first value in a comma separated string.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

quick way:

sed 's/[^,]*,\s*//' file
"External ID", "Name"
"ABC", "ABC Incorporated"

Note this would fail if your field contains comma like "ABC,DEF","ABC Incor.."

Kent
  • 189,393
  • 32
  • 233
  • 301
0

Using Awk:

awk -F", " '{print substr($0, index($0,$2))}' file > tmp && mv tmp file
John B
  • 3,566
  • 1
  • 16
  • 20