LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Remove comma in last line (https://www.linuxquestions.org/questions/linux-newbie-8/remove-comma-in-last-line-4175609588/)

deep_kol 07-10-2017 02:08 PM

Remove comma in last line
 
Hi Folks ,
I am new to unix . Need your help in unix to solve this issue .
I have a csv file like below .
1,2,3
,4,
,5,
,7,

I need to write a script which out put (pick the second column from csv 2 4 5 7 )should be
abc 2,
abc 4,
abc 5,
abc 7 --Last line no comma

Thanks in advance !
Deep

TB0ne 07-10-2017 02:26 PM

Quote:

Originally Posted by deep_kol (Post 5733286)
Hi Folks ,
I am new to unix . Need your help in unix to solve this issue . I have a csv file like below .
1,2,3
,4,
,5,
,7,

I need to write a script which out put (pick the second column from csv 2 4 5 7 )should be
abc 2,
abc 4,
abc 5,
abc 7 --Last line no comma

Ok...please read the LQ Rules and the "Question Guidelines" link in my posting signature. We are happy to try to help you with your script, but we are NOT going to write them for you. Show us what you have written/done/tried on your own so far, and tell us where you're stuck, and we will help. You can easily remove the last comma with sed, but you say you need a script to do further processing of the file.

So please check out any of the easily-found bash scripting tutorials that will help you get started. Post back when you've written something if you're stuck

Turbocapitalist 07-10-2017 02:30 PM

sed can do that. The substitute command s/// can zap the comma and if you precede it with a range the substitution is restricted to that range. That range can be a pattern such as a dollar sign to stand for the end of the file. What pattern can you come up with for s/// to remove a comma at the end of the line?

See:

http://www.grymoire.com/Unix/Sed.html#uh-25
https://www.gnu.org/software/sed/man...ange-Addresses

And of course, "man sed"

rtmistler 07-10-2017 02:47 PM

Hi deep_kol and welcome to LQ.

Please follow TB0ne's recommendation and review the LQ rules as well as show some work you've tried to accomplish this programming effort.

As it stands, no one here has any idea what script language you are familiar with, if any, and what knowledge you have of scripting.

Remember that everyone here is a volunteer, not paid to provide support, and the intentions of the LQ site are so that you learn about what you're trying to do so that you can increase your knowledge and be able to go further with that in the future. If someone hands you a result in the form of code, you may not understand it, even if it benefits you immediately.

JJJCR 07-10-2017 05:40 PM

Is it an school assignment?

Having a solid fundamentals will go a long way, shortcut method doesn't play well at the end.

Check out this link it might give you some idea: https://unix.stackexchange.com/quest...nerated-string

deep_kol 07-11-2017 11:11 AM

Thanks Friends your suggestion . I have gone through the post and will follow the guideline .
My challenge is to remove the comma (,) in the last line which i could not do it through sed (sed '$s/.$//' filename)

abc 2,
abc 4,
abc 5,
abc 7 --Last line no comma

TB0ne 07-11-2017 12:06 PM

Quote:

Originally Posted by deep_kol (Post 5733605)
Thanks Friends your suggestion . I have gone through the post and will follow the guideline .
My challenge is to remove the comma (,) in the last line which i could not do it through sed (sed '$s/.$//' filename)

abc 2,
abc 4,
abc 5,
abc 7 --Last line no comma

Right...that's what you first said. The sed you posted needs a space after the first $...

Turbocapitalist 07-11-2017 12:11 PM

Quote:

Originally Posted by deep_kol (Post 5733605)
i could not do it through sed (sed '$s/.$//' filename)

You're really close. Do you have any white space there at the end of the line in the data file? And you have a period instead of a comma in the regular expression. Otherwise, as a regular expression, a period will zap any character at all not just a comma.

Try "man 7 regex"


All times are GMT -5. The time now is 04:07 PM.