LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-27-2019, 07:44 AM   #1
neroja
LQ Newbie
 
Registered: Nov 2019
Posts: 3

Rep: Reputation: Disabled
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
 
Old 11-27-2019, 08:16 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by neroja View Post
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.
 
1 members found this post helpful.
Old 11-27-2019, 08:33 AM   #3
neroja
LQ Newbie
 
Registered: Nov 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-27-2019, 08:39 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by neroja View Post
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.
 
1 members found this post helpful.
Old 11-27-2019, 09:15 AM   #5
neroja
LQ Newbie
 
Registered: Nov 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-27-2019, 09:32 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by neroja View Post
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.
 
1 members found this post helpful.
Old 11-27-2019, 12:02 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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.
 
1 members found this post helpful.
Old 11-27-2019, 12:04 PM   #8
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
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 ?

Last edited by tofino_surfer; 11-27-2019 at 12:16 PM.
 
1 members found this post helpful.
Old 11-27-2019, 12:10 PM   #9
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
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(,)
 
Old 11-27-2019, 12:17 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by tofino_surfer View Post
No it definitely is not. They explained all of this in their original post.

From the very first post:
Point taken.
 
Old 11-27-2019, 12:20 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by tofino_surfer View Post
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.

Last edited by TB0ne; 11-27-2019 at 12:41 PM.
 
2 members found this post helpful.
Old 11-27-2019, 12:47 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.

Last edited by rtmistler; 11-27-2019 at 12:55 PM.
 
1 members found this post helpful.
Old 11-27-2019, 12:48 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
@neroja,

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

What result did you find? Best to post the before/after strings.
 
Old 11-27-2019, 11:19 PM   #14
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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
 
1 members found this post helpful.
Old 11-28-2019, 01:00 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
sed -r -i.bak "s/^|$/'/g;s/,/','/" file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make the output in multiple row become single row? ahpin Linux - Newbie 4 07-19-2013 07:49 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 03:03 PM
gnome-terminal is missing one pixel row and its an important row rednuht Linux - General 1 12-24-2009 10:30 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration