LinuxQuestions.org
Visit Jeremy's Blog.
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 07-10-2017, 02:08 PM   #1
deep_kol
LQ Newbie
 
Registered: Jul 2017
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 07-10-2017, 02:26 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,723

Rep: Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748
Quote:
Originally Posted by deep_kol View Post
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.
Old 07-10-2017, 02:30 PM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,063
Blog Entries: 3

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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"
 
Old 07-10-2017, 02:47 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,937
Blog Entries: 13

Rep: Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423Reputation: 3423
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.
 
Old 07-10-2017, 05:40 PM   #5
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,626

Rep: Reputation: 276Reputation: 276Reputation: 276
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
 
Old 07-11-2017, 11:11 AM   #6
deep_kol
LQ Newbie
 
Registered: Jul 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-11-2017, 12:06 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,723

Rep: Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748Reputation: 5748
Quote:
Originally Posted by deep_kol View Post
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 $...
 
Old 07-11-2017, 12:11 PM   #8
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,063
Blog Entries: 3

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Quote:
Originally Posted by deep_kol View Post
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
remove a line from a comma delimited file that contains a single digit in position 2 j-me Linux - General 6 05-30-2013 08:25 AM
Remove the first comma in a TXT fil betsy1181 Linux - Newbie 3 02-29-2012 03:04 PM
[SOLVED] bash/sed/awk to convert comma's not in quotes in a line with many comma's oly_r Programming 23 01-25-2012 08:53 AM
[SOLVED] How to remove line breaks from lines ending with a comma jasonws Linux - General 1 11-10-2010 12:03 PM
using sed to remove line in a comma-delimited file seefor Programming 4 03-10-2009 03:35 PM

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

All times are GMT -5. The time now is 02:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration