LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sed column replacement. (http://www.linuxquestions.org/questions/programming-9/sed-column-replacement-684710/)

keysorsoze 11-19-2008 03:57 PM

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

syg00 11-19-2008 04:21 PM

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.

keysorsoze 11-19-2008 06:16 PM

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

pixellany 11-19-2008 06:23 PM

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.

keysorsoze 11-19-2008 06:47 PM

Thanks, pixellany will give this doc a read and thank you for the explanation.

syg00 11-19-2008 07:31 PM

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.

keysorsoze 11-19-2008 11:44 PM

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.

syg00 11-20-2008 03:29 AM

Quote:

Originally Posted by keysorsoze (Post 3348293)
\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.

pixellany 11-20-2008 06:28 AM

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?

syg00 11-20-2008 02:11 PM

No idea. I must have needed it sometime, I tried it, it worked ...

"Regex extended" encourages me to try what I want/need.

keysorsoze 11-30-2008 11:32 PM

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.

keysorsoze 12-01-2008 09:50 AM

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.


All times are GMT -5. The time now is 08:15 AM.