LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using grep with sed (https://www.linuxquestions.org/questions/linux-newbie-8/using-grep-with-sed-4175525161/)

tripialos 11-12-2014 04:17 AM

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

syg00 11-12-2014 04:59 AM

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.

tripialos 11-12-2014 05:52 AM

Quote:

Originally Posted by syg00 (Post 5268620)
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

pan64 11-12-2014 05:59 AM

sed -i has no meaning if you use sed with pipe as input

tripialos 11-12-2014 06:09 AM

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"

syg00 11-12-2014 06:31 AM

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.

pan64 11-12-2014 06:31 AM

try: sed '/email/s/.$//' test.txt


All times are GMT -5. The time now is 11:32 AM.