LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-05-2012, 07:47 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201
Blog Entries: 3

Rep: Reputation: 37
Replace string using sed command


I want to replace a particular data in text file.
Ex- a1@b44, a2@b90, a3@b76 and there are several entries like this between 1 to 100. I mean maximum number is 100. i.e. a100@b73 or a7@b100.
I want to add .com with every entry.
It means a1@b44 should be a1@b44.com
a2@b90 should be a2@b90.com
a3@b76 should be a3@b76.com
a100@b73 should be a100@b73.com
a7@b100 should be a7@b100.com

Please help. If you can give solution using sed, it will be greatest help from your side.
Ask anything if I am not able to explain completely.
 
Old 07-05-2012, 08:18 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
If there's only one entry per line and nothing else, you could do:
Code:
sed 's/$/.com/' file.txt
If that's not the case, you'll have to provide more details. Ideally, a sample of the input file.
 
Old 07-05-2012, 08:23 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Alternatively, this might be a better solution. Here there can be more text after the email address.

Code:
sed 's/@.[^ ]*/&.com/g' aba.txt
Still, to be sure you'd have to provide a sample of the file.
 
Old 07-05-2012, 09:13 AM   #4
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
Thanks for the perfect solution.
No, it was not the only string in line. There are many words before and after the particular string.
But your second solution is fit for my text file.
I want to ask something. If we put or do not put backslash before @, it does not affect the result. why? Is @ not the meta character?
If we remove [^ ] from the command. what does it mean?
sed 's/@.*/&.com/g' aba.txt
Because it is giving the correct result too. Does it mean, single and multiple occurrences of character @.
Then how will we write that single and multiple occurrences of other characters after @?
 
Old 07-05-2012, 09:52 AM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
want to ask something. If we put or do not put backslash before @, it does not affect the result. why? Is @ not the meta character?
I don't think @ has any special meaning here.

Glad it worked for you.

Quote:
If we remove [^ ] from the command. what does it mean?
sed 's/@.*/&.com/g' aba.txt
Because it is giving the correct result too. Does it mean, single and multiple occurrences of character @.
If there are other characters and spaces after the email address, it can't give you the same result.

@.* means @ and 0 or more of any character

@.[^ ]* means @ and 0 or more of any character until you reach a blank space


Quote:
Then how will we write that single and multiple occurrences of other characters after @?
Again, you must be more specific. I have no idea what you mean.

Please, provide at least one line of the actual file. Otherwise, we might be wasting a lot of time trying to guess what you mean. If you can please provide examples of those multiple occurrences as well so that it's clear what you mean.
 
Old 07-06-2012, 02:11 AM   #6
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
Quote:
Originally Posted by sycamorex View Post
If there are other characters and spaces after the email address, it can't give you the same result.
@.* means @ and 0 or more of any character
@.[^ ]* means @ and 0 or more of any character until you reach a blank space
Thanks for the specification.Now I got difference between @.[^ ]* & @.*
Actually I was confused between occurrences of same characters and any character after the particular character.
Ex- @@@@ - four occurrences of same character.
@.5yBW - more than one occurrence of any character after @.

The command you gave was perfect for my issue.
Code:
sed 's/@.[^ ]*/&.com/g' untitled.csv
But if we remove [^ ], it adds the .com at last of the line, the same result given by your first command.

I am not able to give my complete text file but it is exactly like this.

Code:
InfoKey: a15@b74 Creator
InfoValue: LaTeX  a44@b89 with hyperref package
InfoKey: a1@b7 Producer
InfoValue: dvips + a13@b78 GNU Ghostscript 7.07
NumberOfPages: a22@b88 190
BookmarkTitle:  Table of Contents
BookmarkLevel: a42@b39 1
BookmarkPageNumber: a92@b68 3 Table of Contents
 
Old 07-06-2012, 03:03 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
But if we remove [^ ], it adds the .com at last of the line, the same result given by your first command.
That's correct. It'll add the suffix .com to the end of a line.

I guess the line below is your question:

Quote:
Then how will we write that single and multiple occurrences of other characters after @?
Not sure what you mean by that? Write where? Do you mean match? See below:

Code:
InfoKey: a15@b74 Creator
InfoValue: LaTeX  a44@b89 with hyperref package
InfoKey: a1@b7 Producer
InfoValue: dvips + a13@b78 GNU Ghostscript 7.07
NumberOfPages: a22@b88 190
BookmarkTitle:  Table of Contents
BookmarkLevel: a42@b39 1
BookmarkPageNumber: a92@b68 3 Table of Contents
Boodafa adf: a08@b100 dfadaadfa; afdad df
dadfa adfaf a09@b1 blah blahblah
Code:
sed -n 's/.*@\(b[0-9][0-9]*\).*/\1/p' file
b74
b89
b7
b78
b88
b39
b68
b100
b1
It also works for strings with .com added.

Hope that helps.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed replace string xmickaelx Programming 3 04-07-2011 06:42 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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