I would like to a search and place, with a list of patterns on one file, and the lines to be worked on in a different file. I believe I could do this in a full script if I had more time, but alas, time is running out. The contents of the pattern file looks remarkably like a list of ip addresses, 1 per line. The file to be worked on is a large script that does things with ip addresses. I would like to find all instances of ip addresses, 1 at a time and if they are not already commented out, comment them out with a leading pound sign. So a one-liner to print would be:
A one-liner to do a simple substitution would be:perl -pe /print/ ip_addr_file
I would like to do both at once. Step through the list of ip addresses, one at a time, and use that pattern to search the script_file. When I find it in there, then comment that line out of the script_file. Something like this:perl -pi -e s/^/#/ if /66.11.10.20/' script_file
My perl skills are rusty enough that I cannot quite get there. Looking for wisdom. Thanks!perl -pi -e "s/^/#/ if /`perl -pe /print/ ip_addr_file` /" script_file
Update:
I was tried seveal different variations and suggestions to try and do this in a perl one-liner. I got close, but could not coax it into doing what I wanted. I reverted to one of the first suggestions and did it in a perl script.
First let me say thanks to the monks, and in particular to jeffa, FreeBeerReekingMonk, Laurent_R, and aaron_baugher for your suggestions and time. It is appreciated.
I've been relunctant to share my code only because my employer is a little parnoid about putting information and data on the Internet. (It has been suggested that we should not share company name and job title on LinkedIn.) I am not quite that paranoid, but I don't need to poke the bear either.
I'll provide some details about this task. We have a script to add static routes on a Solaris box. It was maintained by hand for many years. Over that time, different people did that task different ways. It was decided to move to linux. linux has a routes file that can be used to populating static routes, but the format of the file is different than the route command. Also embedded in the static route file is comments that pertain to a given route (like change request number, customer, etc.) The problen at first was to change the lines in the static routes file that add routes, while not changing the content of the comment lines, and not changing the order of the lines in the file. That became more complicated when it was discovered that there were many variances in the format of the file; it really became a data normalization problem. In Solaris, you can add a route or delete a route. In linux, they only care about the route adds. So if I did this conversion, if there was a delete route on the Solaris box, it would be carried as an add route on the linux box.
I did a lot of the data massaging by hand with grep, fgrep, awk and vi. This perl script was to go into the static routes file, find instances of a stand-alone delete commands, and then comment it out. I was able to do that (thanks!). Originally, I was going to do the data conversion purely in vi. I could go to particular lines in the file, manipulate only those, and leave the others in place. Alas, it got too complicated for that.
Although, I don't feel comfortable sharing the perl script, I can share this nugget I discovered within vi:
That will swap the 2nd and 4th fields on every line with XXX .:g/XXX/s/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\)/\1 \4 \3 \2/
Thanks again. MrGibbs
In reply to one liner with separate input file? by MrGibbs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |