LinuxQuestions.org
Visit Jeremy's Blog.
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 01-03-2013, 04:05 AM   #1
coolpraz
Member
 
Registered: Feb 2010
Posts: 35

Rep: Reputation: 0
How to replace text using sed or awk in a file without changing the commented text


Hi,
im wirting the bash script to change the text from configuration file of httpd.conf, In a configuration file there are many comment similar to configuration. when i change the text using sed its change the commented section tooo. how to overcome form this .
eg: -
$ cat test.txt
#User mysql
User mysql


(i want to change User mysql to User apache, not the #User mysql)

Last edited by coolpraz; 01-03-2013 at 04:07 AM.
 
Old 01-03-2013, 04:12 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
sed '/^#/!s/User mysql/User apache/' test.txt
This tells sed to not change the lines matching a hash at the beginning of the line. The other lines are changed instead.
 
1 members found this post helpful.
Old 01-03-2013, 04:20 AM   #3
coolpraz
Member
 
Registered: Feb 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Code:
sed '/^#/!s/User mysql/User apache/' test.txt
This tells sed to not change the lines matching a hash at the beginning of the line. The other lines are changed instead.
hey thanx a lot colucix but its echo the changes, original file didn't changed so i use

$ sed -i '/^#/!s/User */User apache/' test.txt(here i used wild card * )
$ cat test.txt
#User mysql
User apachemysql

(its adding but not changing the mysql to apache)
 
Old 01-03-2013, 05:54 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I usually omit to mention the -i option, unless specifically request, for safety. What if someone less experienced than you apply a command without testing to multiple files and scramble them all?!

Regarding your command, you're using the wildcard as the shell's file matching pattern. Instead in regular expressions it means zero or more occurrences of the preceding character. Therefore using
Code:
/User */
you match a string with User followed by zero or more blank spaces. You have to use the following
Code:
/User .*/
to match User followed by a blank space and any number (zero included) of any character. Beware that in this case it matches anything till the end of the line:
Code:
sed -i '/^#/!s/User .*/User apache/' test.txt

Last edited by colucix; 01-03-2013 at 05:55 AM.
 
Old 01-03-2013, 06:19 AM   #5
coolpraz
Member
 
Registered: Feb 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
I usually omit to mention the -i option, unless specifically request, for safety. What if someone less experienced than you apply a command without testing to multiple files and scramble them all?!

Regarding your command, you're using the wildcard as the shell's file matching pattern. Instead in regular expressions it means zero or more occurrences of the preceding character. Therefore using
Code:
/User */
you match a string with User followed by zero or more blank spaces. You have to use the following
Code:
/User .*/
to match User followed by a blank space and any number (zero included) of any character. Beware that in this case it matches anything till the end of the line:
Code:
sed -i '/^#/!s/User .*/User apache/' test.txt
thanx colucix thanku very much
 
  


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
How do I replace the text between patterns located on separate lines? (sed, awk, etc) Quon Programming 5 02-12-2012 06:27 AM
sed or awk for text file manipulations rng Linux - General 4 12-18-2011 08:10 PM
how do you replace text between two words in a whole file not just 1 line w/ sed/awk lityit Programming 5 11-04-2011 12:04 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 07:35 PM

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

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