LinuxQuestions.org
Review your favorite Linux distribution.
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-12-2014, 04:17 AM   #1
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Rep: Reputation: Disabled
using grep with sed


Hi guys

Here is a weird one...at least for me.

i have a test file where some lines contain the world e-mail followed by some e-mails. I want to remove the last comma (,) character of each of these lines .

I can do this with the below command

Code:
cat test.txt |grep emails|sed 's/,$//'
      emails= 
      emails= 
      emails=  test3@mail.com, testmail@mail.com,test2@mail.com
      emails=
The above command does not save the result so when i add the "-i" on the sed command i get the following error:

Code:
cat test.txt|grep emails|sed -i   's/,$//'
sed: no input files
Thats quite normal since the data were piped to sed and were not given from a file in order for sed to save the changes.

How do you overcome this one? Any ideas?

Thanks
 
Old 11-12-2014, 04:59 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
You don't need cat, you don't need grep.
You should be able to do it all in one sed command - problem goes away.
 
Old 11-12-2014, 05:52 AM   #3
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
You don't need cat, you don't need grep.
You should be able to do it all in one sed command - problem goes away.
Thanks will try it out
 
Old 11-12-2014, 05:59 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,897

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
sed -i has no meaning if you use sed with pipe as input
 
Old 11-12-2014, 06:09 AM   #5
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Well i came up with this solution

Code:
sed  's/,$//' test.txt
But its not 100% corret but it does the job. However it does not remove the last comma from lines which contains the word "email" it actually removes the comma "," from every line that end with ",". In my case is not an issue since i have no other lines that end with "," but still is not 100% "accurate"
 
Old 11-12-2014, 06:31 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Have a look at the manpage for sed in the section "Addresses". You can use regex to select the lines you want - the regex can be a simple string.
 
Old 11-12-2014, 06:31 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,897

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
try: sed '/email/s/.$//' test.txt
 
  


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
sed or grep pgb_710 Programming 20 09-04-2011 12:12 PM
Grep or Sed ? How can I use it ? ArOnaXx Linux - Newbie 6 04-12-2009 02:08 PM
how to use grep/sed tnik Linux - General 3 11-06-2007 07:51 PM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 08:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

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

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

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