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 |
Try
Code:
sed -nre '/host_name/s/(\S*$)/1111-\1-app/p' hosts.cfg |
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 |
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. |
Thanks, pixellany will give this doc a read and thank you for the explanation.
|
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. |
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. |
Quote:
Quote:
|
Quote:
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? |
No idea. I must have needed it sometime, I tried it, it worked ...
"Regex extended" encourages me to try what I want/need. |
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. |
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 01:39 PM. |