LinuxQuestions.org
Visit Jeremy's Blog.
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 11-19-2008, 03:57 PM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
Sed column replacement.


Hi, I am attempting to perform the following replacement using the dataset below:

define host {
host_name lnxbox
alias lnxbox
address 127.0.0.1
use Unix-Server
}


This is a Nagios host.cfg config file and I am trying to manipulate the 2 column values.
My end goal is to surround "lnxbox" with <location-number-lnxbox-app>. This will give me the final result
of something linke this: 1111-lnxbox-app.

The problem I am facing is when I do the following:

sed -ne '/host_name/s/^/&/p' hosts.cfg

host_name lnxbox
host_name lnxbox2

What do I use to match all fields in the second column of "lnxbox", "lnxbox2", "lnxbox3".


sed -ne '/host_name/s/lnxbox/1111-&-app/p' hosts.cfg

gives me:

host_name 1111-lnxbox-app

which is what I want but how do I match all fields in the 2 column? Would I need to write a awk script to
extract the second column and feed into a for loop?


Thanks
 
Old 11-19-2008, 04:21 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,057

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
Try
Code:
sed -nre '/host_name/s/(\S*$)/1111-\1-app/p' hosts.cfg
Might be more obvious in awk - you can do similar data selection, and use a column selection.
 
Old 11-19-2008, 06:16 PM   #3
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks sy00, one quick question did you gather this from the sed&awk book from O'relly or did you use some online reference to obtain this. I am confused on how why you used \1 instead of the & in the substitution field and would like to look this up.

Thanks again the command gave me exactly what I needed
 
Old 11-19-2008, 06:23 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
My favorite tutorial on SED and AWK: http://www.grymoire.com/Unix/

"\1" is the SED syntax for a backreference. It inserts the first instance of a pattern match specified inside "escaped parentheses" i.e.: \(...\)
"\2" would insert the 2nd instance, etc.
 
Old 11-19-2008, 06:47 PM   #5
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks, pixellany will give this doc a read and thank you for the explanation.
 
Old 11-19-2008, 07:31 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,057

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
I tend to have a couple of lazy habits - I (almost) always use "-r" to save having to escape everything.
Likewise I tend to use "-i" with grep unless I have a reason not to.
 
Old 11-19-2008, 11:44 PM   #7
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
syg00 or pixellany, I have done some reading and think I understand what this sed line does. Here is my brief explanation:

sed -nre '/host_name/s/(\S*$)/1111-\1-app/p' hosts.cfg

syg00 you passed the "sed -r" so that you can use the regex "\S" right?

\S*$ - match any non-whitespace character at the end of the line
( ) - Used to keep part of the pattern - so (\S*$) stores the pattern matched

\1 - prints out the remembered pattern.

One thing is though why didn't the ( ) parethesis have any backslashes in them? I read the grymoire document on sed and the () with backslashes and \1. Why didn't you use backslashes?

Thanks for the help again.
 
Old 11-20-2008, 03:29 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,057

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
Quote:
Originally Posted by keysorsoze View Post
\S*$ - match any non-whitespace character at the end of the line
Close - it matches all non-whitespace at the end of the line.
Quote:
One thing is though why didn't the ( ) parethesis have any backslashes in them?
Re-read my previous reponse.
 
Old 11-20-2008, 06:28 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Quote:
\S*$ - match any non-whitespace character at the end of the line
Close - it matches all non-whitespace at the end of the line.
Had not seen this before....looked it up on grymoire:

http://www.grymoire.com/Unix/Regular.html#uh-13

\S (meaning any non-whitespace character) is listed under "Perl extensions". Do all these work in SED? Where documented?
 
Old 11-20-2008, 02:11 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,057

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
No idea. I must have needed it sometime, I tried it, it worked ...

"Regex extended" encourages me to try what I want/need.
 
Old 11-30-2008, 11:32 PM   #11
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Guys, I had one last question regarding this post. I am trying to match
and store into patterns into \1,\2 again but am having an issue I believe with the regex. I am basically trying to get from the line "1101-xgate2k3-app" only "1101-xgate2k3-" and store it into \1 so I can change app to dev etc... on multiple files. Here are my attempts to do this but its not working. Could it be my regex issue? I am using Regex buddy to craft my regex to store into \( \).

Here are my attempts at this

sed -nre '/xgate2k3/s/(\s+(\d){0,4}-(.*)-)/\1-dev/p' windows_*


This is the data I am trying to modify again:

host_name 1101-xgate2k3-app
alias 1101-xgate2k3-app


What the am I doing wrong?


Thanks again.
 
Old 12-01-2008, 09:50 AM   #12
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Guys, think I found a simpler solution sed -i "/$hostname/s/app/$replace/" $DIR/windows_* did the trick for me.

Got to wrapped up in the regex.
 
  


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
replacement with sed DeepSeaNautilus Programming 6 10-01-2008 06:48 AM
Replacement with sed or awk DeepSeaNautilus Programming 4 08-05-2008 11:17 AM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 11:53 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM


All times are GMT -5. The time now is 09:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration