LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 05:17 AM   #1
tripialos
Member
 
Registered: Apr 2012
Posts: 162

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, 05:59 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
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, 06:52 AM   #3
tripialos
Member
 
Registered: Apr 2012
Posts: 162

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, 06:59 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

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

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, 07:31 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
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, 07:31 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

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


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


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