LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I want to add single quotes on every row (https://www.linuxquestions.org/questions/linux-newbie-8/i-want-to-add-single-quotes-on-every-row-4175665004/)

neroja 11-27-2019 07:44 AM

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

TB0ne 11-27-2019 08:16 AM

Quote:

Originally Posted by neroja (Post 6062332)
Input in a file is :
Code:

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
Code:

'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

Happy to help; so post what you have written/done/tried so far, and tell us where you're stuck. Otherwise, you should read the "Question Guidelines" link in my posting signature. While we're happy to give assistance, just posting what you WANT and showing no effort of your own isn't likely to get you a lot...doing a bit of basic research will give you some starting points.

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.

neroja 11-27-2019 08:33 AM

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

TB0ne 11-27-2019 08:39 AM

Quote:

Originally Posted by neroja (Post 6062343)
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

You don't say what version/distro of Linux you're using, but on openSUSE Tumbleweed, this:
Code:

sed -i "s/^/'/g;s/$/'/g;s/,/','/g" filename
...works just fine. You aren't including the 'g' in your sed, which means it will only ever replace the first occurrence of things...the 'g' tells it to replace ALL of them.

neroja 11-27-2019 09:15 AM

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

TB0ne 11-27-2019 09:32 AM

Quote:

Originally Posted by neroja (Post 6062360)
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

I gave you an exact sed string that works; can't be more plain.

scasey 11-27-2019 12:02 PM

Quote:

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'
That's a slight change in your question.
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
If you only want to put quotes around the first comma, remove the g modifier on that pattern.

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.

tofino_surfer 11-27-2019 12:04 PM

Quote:

I gave you an exact sed string that works; can't be more plain.
Except it doesn't do what the OP requested. They only need the first comma to be surrounded by quotes. The second or third commas in the line should be untouched.

Quote:

I can put quote only after and before the 1st comma.
I need this line : 'ETPFcode','5101','5201
as 'ETPFcode','5101,5201'
Quote:

You aren't including the 'g' in your sed, which means it will only ever replace the first occurrence of things...the 'g' tells it to replace ALL of them.
They only need the first instance of a comma "," to be replaced by "','" so they definitely don't want to use g for global in this case. You seem to have misunderstood the requirements in the first post and then claim that your solution works perfectly.

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:

You don't say what version/distro of Linux you're using, but on openSUSE Tumbleweed, this:
Why would sed work differently on different distributions ?

tofino_surfer 11-27-2019 12:10 PM

Quote:

That's a slight change in your question.
No it definitely is not. They explained all of this in their original post.

From the very first post:

Quote:

I want output as below
'ETPFcode','5101,5201'

I want to put quotes at the front of each line, end of each line, left of 1st comma(,) and right of 1st comma(,)

scasey 11-27-2019 12:17 PM

Quote:

Originally Posted by tofino_surfer (Post 6062427)
No it definitely is not. They explained all of this in their original post.

From the very first post:

Point taken.

TB0ne 11-27-2019 12:20 PM

Quote:

Originally Posted by tofino_surfer (Post 6062425)
Except it doesn't do what the OP requested. They only need the first comma to be surrounded by quotes. The second or third commas in the line should be untouched.

They only need the first instance of a comma "," to be replaced by "','" so they definitely don't want to use g for global in this case. You seem to have misunderstood the requirements in the first post and then claim that your solution works perfectly.

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.

Ok, so where's your suggested solution??? Yes, I did misread something...so instead of
Code:

sed "s/^/'/g;s/$/'/g;s/,/','/g" filename
...use
Code:

sed "s/^/'/g;s/$/'/g;s/,/','/1" filename
..which only changes the first comma. You seem to want to take issue with my post and scasey's, but aren't putting forth one of your own.
Quote:

Why would sed work differently on different distributions ?
Because the OP mentions unix, not Linux...and things on AIX, HPUX, or Solaris have different versions of things, which behave differently. The GNU version may not be installed.

rtmistler 11-27-2019 12:47 PM

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.

rtmistler 11-27-2019 12:48 PM

@neroja,

Have you tried the sed string from post #4 exactly?

What result did you find? Best to post the before/after strings.

Kenhelm 11-27-2019 11:19 PM

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
'bcde                                    # Displayed in a terminal the ' overprints the first character on the line
                                        # But the actual line sed created is abcde\r'\n


grail 11-28-2019 01:00 AM

Code:

sed -r -i.bak "s/^|$/'/g;s/,/','/" file


All times are GMT -5. The time now is 09:48 PM.