Score:0

How to add quotes to second element of a string after comma in a text file bash

bt flag

File contents:

1234,/Product/number 234567
2456,/Product/number 456789

and so on...many lines

Want the the output as follows:

1234,"/Product/number 234567"
2456,"/Product/number 456789"
David avatar
cn flag
Then try to make the code yourself and post it. If there are errors someone may try to help you. No one is going to do it for you.
pLumo avatar
in flag
What problem do you want to solve? Please check: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
ru flag
Do you want to simply display the contents of that file with the added quotes or do you want to replace those contents with the new ones (with the quotes) in that file?
mchid avatar
bo flag
Did you actually want `1234,"/Product/number 234567" 2456,"/Product/number 456789"` instead of two individual lines? In any case, my answer below will work.
Score:3
hr flag

For structured data (i.e. that is describable in terms of records consisting of delimited fields) I'd suggest a tool like awk:

awk -F, 'BEGIN{OFS=FS} {$2 = "\"" $2 "\""} {print}' file

or (more compactly - making use of the default print action)

awk -F, 'BEGIN{OFS=FS} {$2 = "\"" $2 "\""} 1' file

If you really need to do it natively in bash, then

while IFS=, read -r a b; do printf '%s,"%s"\n' "$a" "$b"; done < file

(but don't - see Why is using a shell loop to process text considered bad practice?).

Score:2
bo flag
You can do this using perl regex and you can substitute similar to using sed.

Assuming you will always have /Product/number followed by a single group of digits, you can use the following command to print the changes:

perl -pe 's|/Product/number (\d+)|"$&"|g' filename

If you are satisfied with the output, you can use the following command to edit the file:

perl -pi -e 's|/Product/number (\d+)|"$&"|g' filename
  • (\d+) matches any group of one or more digits
  • $& backreferences the entire matched string, similar to using & in sed

Also, I use | as a separator instead of / because the string contains forward-slashes.


Alternatively, you can use sed by using the following command to print:
sed -Ee 's|/Product/number [[:digit:]]+|"&"|g' filenameme

or the following command to actually edit changes to the file:

sed -Ei 's|/Product/number [[:digit:]]+|"&"|g' filenameme

This command uses the extended regular expressions option -E

  • [[:digit:]]+ matches a group of one or more digits
  • & backreferences the entire matched string
Score:0
cn flag

If your data really are as simple as you show, all you want is to add a double quote character after the first comma and after the last character of every line. If so, just do:

$ sed 's/,/,"/; s/$/"/' file
1234,"/Product/number 234567"
2456,"/Product/number 456789"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.