Basic sed replace question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic sed replace question
# 1  
Old 05-09-2007
Basic sed replace question

Hello,

I have a .htaccess file as follows... No I want to comment the last line ( in this case.. it may not be a last line for other cases).. please advise how to add a # (comment infront of "Options All -Indexes" not assuming this line to be the last line using sed.

root@server1 [/home/name/public_html]# cat .htaccess
# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>


Options All -Indexes
root@server1 [/home/name/public_html]#


Result should be like
------------------------

root@server1 [/home/name/public_html]# cat .htaccess
# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>


#Options All -Indexes
root@server1 [/home/name/public_html]#




Please advise.

Thanks.
# 2  
Old 05-09-2007
sed 's/Options All -Indexes/#Options All -Indexes/' filename

assuming single occurrence of pattern
else use /g
# 3  
Old 05-09-2007
Thanks... works beautifully.


How about say.. I have some 50 files in a folder... which are actually valiases files.. where the catch all or default address is set as ....

*: username for a domain... xyz.com

*: username1 for a domain... abc.com


I.e.

if you do ..

cat /etc/valiases/xyz.com
*: username

cat /etc/valiases/abc.com
*: username1

cat /etc/valiases/def.com
*: username2


Now.. i want to replace all instances of ..

*: username2 or *: username or *: username1 to

*: :fail: No Such Address Here



So after conversion..

cat /etc/valiases/def.com
*: :fail: No Such Address Here

same for other domains..


Can you please advise.


Thanks.
# 4  
Old 05-09-2007
Code:
perl -i -ne ' print "*: :fail: No Such Address Here\n" if( /^\*:/ ) ' /etc/valiases/*.com

# 5  
Old 05-09-2007
Code:
sed 's/\*: username.*/\*: :fail: No Such Address Here/' file

# 6  
Old 05-09-2007
Hello,

Its working.. but it prints the output.. how can I make a script so that it changes the line automatically.

root@dallas [/opt/abc]# sed 's/\*: username.*/\*: :fail: No Such Address Here/g' *
*: :fail: No Such Address Here
*: :fail: No Such Address Here
*: :fail: No Such Address Here
root@dallas [/opt/abc]# cat abc.com
*: username1

root@dallas [/opt/abc]# ls
./ ../ abc.com def.com xyz.com
root@dallas [/opt/abc]#


This doesnot seem to be working.
-------------------------------------
root@dallas [/opt/abc]# perl -i -ne ' print "*: :fail: No Such Address Here\n" if( /^\*:/ ) ' /etc/valiases/*.com
root@dallas [/opt/abc]# cat abc.com
*: username1
# 7  
Old 05-09-2007
Quote:
Originally Posted by fed.linuxgossip
This doesnot seem to be working.
-------------------------------------
root@dallas [/opt/abc]# perl -i -ne ' print "*: :fail: No Such Address Here\n" if( /^\*:/ ) ' /etc/valiases/*.com
root@dallas [/opt/abc]# cat abc.com
*: username1
This command has changed files in /etc/valiases directory. In which directory are you checking the files?
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

simple sed question - replace from phrase to end of line

I have the following line an in input file I want to digest with sed and simple replace the bold part with a variable defined in my bash script. I can do this in several sed operations but I know there must be a way to do it in a single sed line. What is the syntax? Line in file:... (1 Reply)
Discussion started by: graysky
1 Replies

3. Shell Programming and Scripting

Basic sed question

Please have a look at below examples. Why do these 3 sed commands deliver the same result? Especially, why are there 4 "x" in the result instead of 3? 1. echo "abc" | sed 's/d*/x/g' xaxbxcx 2. echo "abc" | sed 's/d*/&x/g' xaxbxcx 3. echo "abc" | sed 's/d*/x&/g' xaxbxcx Thanks for... (2 Replies)
Discussion started by: Werner Gross
2 Replies

4. Shell Programming and Scripting

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",... (3 Replies)
Discussion started by: RickS
3 Replies

5. 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

6. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

7. Shell Programming and Scripting

sed: a "replace unless match" question

Context: I am using sed in a cronjob to change the dates in a separate sql script every week. Each week the dates must be updated to reflect the Monday and Friday of the previous week. I have solved the problem but in solving it I discovered a major weakness in my knowledge of sed. Lines to be... (0 Replies)
Discussion started by: Bubnoff
0 Replies

8. Shell Programming and Scripting

sed - replace string question

Hi Everybody Uning sed I need to replace: "count(user) = 0" -o "uid = ftp" -n -o "gid = ftp" by "read only = no" Thanks in advance. (4 Replies)
Discussion started by: danmero
4 Replies

9. 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

10. Shell Programming and Scripting

basic cat replace string

trying to exclude hostnames ending in "s" from a host file: # cat hosts ssef ssefd ssefsfff ssefsfs # for x in `cat hosts`; do echo "${x/*s}" ;done ef efd fff # How can I echo/or not echo only 'ssefsfs' ?? thanks (4 Replies)
Discussion started by: prkfriryce
4 Replies
Login or Register to Ask a Question