Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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'
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'
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.