LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2006, 07:54 AM   #1
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
sed, negate and variable


Hey!
I'm trying to use sed to search through a text, and replace a variable containing spaces with 'bar'.
But i don't want to replace on lines containing 'foo'.
The reason i want to use a variable is that the sed-command is in a for-loop, so it get's a different variable each loop.

It works when i want to replace on lines with foo:
Code:
$ echo ${test}
test1 test2
$ echo ${test// /\\ }
test1\ test2
$ cat file
test1
test2
replace this test1 test2
don't replace when foo test1 test2
$ sed "/foo/s/${test// /\\ }/bar/g" file
test1
test2
replace this test1 test2
don't replace when foo bar
But when i want to negate lines, bash freaks out.
Code:
$ sed "/foo/!s/${test// /\\ }/bar/g" file
bash: !s/${test//: event not found
Also, this works of course:
Code:
$ sed '/foo/!s/test1\ test2/bar/g' file
test1
test2
replace this bar
don't replace when foo test1 test2
But i have to use double quotes for sed to understand my variable.
Otherwise i get:
Code:
$ sed '/foo/s/${test// /\\ }/bar/g' file
sed: -e expression #1, char 17: unknown option to `s'
So, can i negate the regular expression foo without using !
Or can i use sed-branching to test if the regexp foo is not on a line?
Or does anyone have a good hint on how to achieve this? Thanks!
 
Old 04-15-2006, 08:38 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
$ sed "/foo/!s/${test// /\\ }/bar/g" file
When in double quotes, the exclamation point is still a special character which replays a previous command.

You need the sed command in single quotes. You can fragment it so parts that need shell expansion are in double quotes.
sed '/foo/!s/'"${test// /\\ }"'/bar/g' file
 
Old 04-16-2006, 09:02 AM   #3
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Original Poster
Rep: Reputation: 38
Ok, it works! I thought i had tried every possible combination of backticks, double/single quotes. Apperently not
Thanks heaps jschiwal!
 
  


Reply



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
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
Bash, using variable in sed fur Programming 3 11-12-2005 07:41 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM
sed and variable chirophil91 Programming 2 10-19-2004 03:23 AM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 05:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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