LinuxQuestions.org
Review your favorite Linux distribution.
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 03-13-2012, 02:34 PM   #1
shreyas08
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Rep: Reputation: Disabled
Replace a string with pattern within that string


I want to replace a part of string with a pattern within that string.

for eg:
if my string is "abcd#25 - efgh ijkl" it should print output as "abcd#efgh"

if my string is "xxxx#100 - yyyy zzzz" it should print output as "xxxx#yyyy"

The pattern is # followed by integer should be replaced with first word after # integer -

Sorry for my bad english.
 
Old 03-13-2012, 03:20 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
Try the following:
Code:
sed 's/\(.*#\).*- \(.*\) \(.*\)/\1\2/' file
See if that's what you want.
 
Old 03-13-2012, 04:27 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Use the -r option to remove the need to backslash everything.

Code:
sed -r 's/(.*#).*- (.*) (.*)/\1\2/' file
With grep and sed, their basic regular expressions level treats parens and many other regex characters as literal. But as a gnu extension you can backslash escape them to enable their special meanings.

But when you use the extended regular expressions (sed -r/grep -E/egrep), everything is reversed. All regex characters are enabled by default, and escaping disables them.

Check out the grep man page for details (and the info page for even more).

Last edited by David the H.; 03-13-2012 at 04:31 PM.
 
Old 03-13-2012, 04:31 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
Quote:
Originally Posted by David the H. View Post
Use the -r option to remove the need to backslash everything.

Code:
sed -r 's/(.*#).*- (.*) (.*)/\1\2/' file
With grep and sed, their basic regular expressions level treats parens and many other regex characters as literal. But as a gnu extension you can backslash escape them to enable their special meanings.

But when you use the extended regular expressions (sed -r/grep -E/egrep), everything is reversed. All regex characters are enabled by default, and escaping disables them.
I keep forgetting about the existence or ERE
 
Old 03-14-2012, 01:07 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Code:
awk -F"[ #]" '{print $1"#"$4}' 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
replace string by another string in bash shell chinhth2010 Linux - General 2 08-02-2011 11:40 PM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
[SOLVED] replace string with empty string of dir path ted_chou12 Linux - Newbie 3 04-02-2011 07:43 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 12:05 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 03:06 AM


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