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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-10-2017, 02:08 PM
|
#1
|
LQ Newbie
Registered: Jul 2017
Posts: 5
Rep:
|
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
Last edited by deep_kol; 07-10-2017 at 02:09 PM.
|
|
|
07-10-2017, 02:26 PM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,145
|
Quote:
Originally Posted by deep_kol
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
Last edited by TB0ne; 07-10-2017 at 02:28 PM.
|
|
1 members found this post helpful.
|
07-10-2017, 02:30 PM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,506
|
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"
|
|
|
07-10-2017, 02:47 PM
|
#4
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,892
|
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.
|
|
|
07-10-2017, 05:40 PM
|
#5
|
Senior Member
Registered: Apr 2010
Posts: 2,215
|
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
|
|
|
07-11-2017, 11:11 AM
|
#6
|
LQ Newbie
Registered: Jul 2017
Posts: 5
Original Poster
Rep:
|
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
|
|
|
07-11-2017, 12:06 PM
|
#7
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,145
|
Quote:
Originally Posted by deep_kol
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 $...
|
|
|
07-11-2017, 12:11 PM
|
#8
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,506
|
Quote:
Originally Posted by deep_kol
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 01:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|