I want to add single quotes on every row
Input in a file is :
HCCurmonth,Oct HCCurYear,2018 FunnelCurmonth,Oct Fncuryr,2018 FVCurmonth,Jul Funnelcuryear,2019 Curmonth,Oct Curyear,2019 prevmonth,Sep ETPFcode,5101,5201 ETPAccyrmonth,201903 I want output as below 'HCCurmonth','Oct' 'HCCurYear','2018' 'FunnelCurmonth','Oct' 'Fncuryr','2018' 'FVCurmonth','Jul' 'Funnelcuryear','2019' 'Curmonth','Oct' 'Curyear','2019' 'prevmonth','Sep' 'ETPFcode','5101,5201' 'ETPAccyrmonth','201903' I want to put quotes at the front of each line, end of each line, left of 1st comma(,) and right of 1st comma(,) and make a separate file, use it for loading data to database. I want to write a unix shell script to do this preferably a bash script. Please help |
Quote:
That said, you can use sed to replace the beginning and ending of a line with a single-quote, and also to replace a comma with a " ',' ", which will give you what you want. You can also use awk to read the lines with a field-separator of a comma, and output things accordingly. Start there. |
I tried sed and perl command
perl -plne "s/^/'/; s/$/'/" ff_HRCtrigger.txt_bkp >> xyz.txt sed "s/^/'/;s/$/'/" ff_HRCtrigger.txt_bkp >> abc.txt But only get single quote at start of the file 'HCCurmonth,Oct 'HCCurYear,2018 'FunnelCurmonth,Oct 'Fncuryr,2018 'FVCurmonth,Jul 'Funnelcuryear,2019 'Curmonth,Oct 'Curyear,2019 'prevmonth,Sep 'ETPFcode,5101,5201 'ETPAccyrmonth,201903 |
Quote:
Code:
sed -i "s/^/'/g;s/$/'/g;s/,/','/g" filename |
I am not able to find the unix version.
Looks like sed -i "s/$/'/g" ff_HRCtrigger.txt_bkp isnt working. Any way instead of "s/,/','/g" ff_HRCtrigger.txt_bkp, I can put quote only after and before the 1st comma. I need this line : 'ETPFcode','5101','5201 as 'ETPFcode','5101,5201' 'HCCurmonth','Oct 'HCCurYear','2018 'FunnelCurmonth','Oct 'Fncuryr','2018 'FVCurmonth','Jul 'Funnelcuryear','2019 'Curmonth','Oct 'Curyear','2019 'prevmonth','Sep 'ETPFcode','5101','5201 'ETPAccyrmonth','201903 |
Quote:
|
Quote:
Please use code tags when posting code or output. Please expand on "isn't working" -- that doesn't tell us much to help you with. Code:
sed "s/^/'/;s/$/'/;s/,/','/" filename > newfilename I'm not sure what the g modifier does for replacing at the start of line (^) or end of line ($) anchors, as each line only has one beginning and end anyway...I suppose it doesn't hurt. Also remove the -i (inplace) option, as you asked for a new file, and redirect the output to your new file. I don't see that you're trying exactly what TB0ne suggested. It gets tricky if you don't do all three substitutions in one pass, especially if you don't want to change the original file. Try man sed and search for "regular expression syntax" for details. Regular expressions take some effort to understand. |
Quote:
Quote:
Quote:
The OP needs each line to have only two pairs of single quotes, from the start of the line to the first comma, and from after the first comma to the end of the line. Any commas after the first should be ignored. They mentioned that they are loading this data into a database. They only want two fields of input on each line. Everything after the first comma would be seen as a single field even if it includes further commas inside of its text. Quote:
|
Quote:
From the very first post: Quote:
|
Quote:
|
Quote:
Code:
sed "s/^/'/g;s/$/'/g;s/,/','/g" filename Code:
sed "s/^/'/g;s/$/'/g;s/,/','/1" filename Quote:
|
It's a good point to stick to post #1 I/O examples and design the sed terms to get that desire result.
I'd like to point out that I have tried TB0ne's original solution on my system and it works to attain the desired result. EDIT: I don't know about the modified solution haven't tried it. The original solution in post #4 worked fine on my Mint VM. Second EDIT: I tried that slight modification and see absolutely no difference between option (a) and (b) Not trying to stir things up, I'd like to hear from the OP what incorrect result occurred and also get a determination as to what their environment is, having also seen the Unix statement. |
@neroja,
Have you tried the sed string from post #4 exactly? What result did you find? Best to post the before/after strings. |
sed "s/$/'/" will apparently fail if the file has Microsoft \r\n end-of-line characters instead of Unix \n.
The \r takes printing back to the start of the line creating the impression of failure when the line is viewed in a terminal. Code:
echo -e 'abcde\r' | sed "s/$/'/" # Echo a line with a carriage return, \r |
Code:
sed -r -i.bak "s/^|$/'/g;s/,/','/" file |
All times are GMT -5. The time now is 09:48 PM. |