Bash sed search and replace question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash sed search and replace question
# 1  
Old 08-03-2010
Bash sed search and replace question

I have several files that I need to modify using sed. I know how to do that, but now a new requirement has come up.

Now, I need to make changes to all lines that don't start with certain strings. For example, I need to change all lines except for lines that start with "a", "hello there", "hows_it_going" or "b".

Does sed have some sort of syntax that means "don't change the line under these conditions"?
# 2  
Old 08-09-2010
sed

to exchange all "X" by "Y" in the lines not beginning with "a".

Code:
sed '/^a/! s/X/Y/g' file.txt

# 3  
Old 08-09-2010
Quote:
Originally Posted by elbrand
to exchange all "X" by "Y" in the lines not beginning with "a".

Code:
sed '/^a/! s/X/Y/g' file.txt

Can anyone post alternative sed command to do the same thing?
Actually replacing this "/^a/!"
This User Gave Thanks to cola For This Post:
# 4  
Old 08-09-2010
Thanks Elbrand! You rock!

Cola, if I understand you correctly it would be:
Code:
sed 's/^a/b/g' file.txt

That would replace all a's at the beginning of a line with a b.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another sed/awk search=>replace question

Hello, Need a little bit of help. Basically I need to replace lines in a file which were calculated wrong as it would 12 hours to regenerate the data. I need to calculate values based on other files which I've managed to figure out with grep/cut but now am stuck on how to shove these new... (21 Replies)
Discussion started by: f77coder
21 Replies

2. Shell Programming and Scripting

Search and replace question

Hi all, I am trying to modify an xml file and I wanted to search and replace using the sed command but here is my issue. I want to search and replace maximumHeapSize="512" and replace it with maximumHeapSize="768" but I have multiple files with different values so I can't search for... (2 Replies)
Discussion started by: reyes99
2 Replies

3. Emergency UNIX and Linux Support

search replace regex question

Hi, I need to run a search and replace on a large database, what I need to change is all instances of #### (eg. 1764 or 1964) to (####) (eg. (1764) or (1964)) But there might be other numbers in there such as (1764) and I do not need those changed to ((1764)) How can I... (7 Replies)
Discussion started by: lawstudent
7 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. Shell Programming and Scripting

Search & Replace question

Hi all, I have one question that hopefully isn't too complicated for the more advanced users here. In one of the Solaris KSH scripts I'm working on, is it possible to script the following: - If there "is" an empty blank line "at the end" of /tmp/text.txt, then remove only that one empty... (3 Replies)
Discussion started by: chatguy
3 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

bash search and replace

Hello all, I would like to replace some text that are forwarded in standard output from a script, then save the replaced text to a file. The text i would like to replace is in the form of: 1 some text 1.1 other text 1.2 more text 1.2.1 still more text i would like to replace 1 some... (5 Replies)
Discussion started by: dolphin06
5 Replies

8. Shell Programming and Scripting

SED Search and Replace Question for Google Analytics

Well, I'm losing my regex ability in sed! Please help. I need to search for this text in multiple html files in a directory: </body> and add the following lines in front of the text above: <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> ... (11 Replies)
Discussion started by: Neo
11 Replies

9. UNIX for Dummies Questions & Answers

search and replace one more question

Hi, I have a file that contains the following contents: 14:05 apple orange123 456mango 16:45 banana I wanted to replace ONLY the "14:05 " and "16:45" with nothing and trying to use the following syntax sed -e 's/*//g' -e 's/^: //g' my_file > new_temp cat new_temp apple orange... (2 Replies)
Discussion started by: ghazi
2 Replies

10. Shell Programming and Scripting

search and replace by bash

Hello everyone! I have a file: text.txt and also a list of replacement: replace.txt (tabbed) is isn\'t Mac Windows will won\'t \.\n \! <--- ! plus 2 spaces I want a script which will change text.txt as I made a script: #!/bin/bash echo Please input the file of... (2 Replies)
Discussion started by: Euler04
2 Replies
Login or Register to Ask a Question