LinuxQuestions.org
Help answer threads with 0 replies.
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 02-14-2011, 04:39 AM   #1
abubakar-malik
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Rep: Reputation: 3
Replace a whole string which contains a spefiic word(s) in it


Hi All,

I am quite new in Linux same as with 'Sed' command. I have found most of my solutions on the forums here except this last one. Plz guide me!

I have following input lines in my source file;

07/02/2011/105139 10.0.117.41:58251 3456.52
07/02/2011/105152 P17StreamDistributorI15BlockingLFQueue9AbsThreadE0x2aaad3e4f460 3849.7

I want to replace every string which contains 'StreamDistributor' word in it, with "CTF-Stream--Distributor" so the desire output should be as follows;

07/02/2011/105139 10.0.117.41:58251 3456.52
07/02/2011/105152 CTF-Stream--Distributor 3849.7

I will prefer to use 'Sed' if its possilbe. Any idea????

Regards,
A.Malik
 
Old 02-14-2011, 04:54 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Would probably depend on if the format is always as you have shown, ie does 'StreamDistributor' always appear in the second column of information delimited by a space?

Also, what have you tried so far?
 
Old 02-14-2011, 05:29 AM   #3
abubakar-malik
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 3
Thanks for reply grail.
I already have found solution

Here is it ;

Code:
echo 07/02/2011/105152 P17StreamDistributorI15BlockingLFQueue9AbsThreadE0x2aaad3e4f460 3849.7 | sed 's/P17StreamDistributor[^]]* /StreamDistributor /g'
 
Old 02-14-2011, 07:46 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Here are a couple of alternatives for you to think about:
Code:
sed -r '/StreamDistributor/s/^([^ ]* )[^ ]+( .*)$/\1CTF-Stream--Distributor\2/' file

awk '$2 ~ /StreamDistributor/{$2 = "CTF-Stream--Distributor"}1' file
 
  


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
Trying to use sed to replace a number that is in a string with a word simpsonc Linux - Newbie 3 09-12-2010 07:55 PM
Global Replace A String That's Not A Whole Word onesikgypo Programming 5 09-18-2009 08:06 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 03:19 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