ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have a mrtg config file that i am trying to replace some strings in. the format of the lines in question are like this
Title[3560-1_10127]: Traffic Analysis for 10127 -- 3560-1
where the 2nd instance on the line of 10127 needs replaced with the string "Port 27". thats easy to do with sed but what i want to do is to do that for all the ports incrementing my way through the file. in other words, there are lines that say 10101 through 10148 and i want to replace the 2nd instance on the line of 10101 with "Port 1" and then go onto the line with 10102 and replace its 2nd instance with "Port 2"
this is breaking my brain. any of you have any ideas?
$ cat rock1961.txt
Title[3560-1_10127]: Traffic Analysis for 10127 -- 3560-1
$ ./rock1961.gawk rock1961.txt
Title[3560-1_10127]: Traffic Analysis for Port 27 -- 3560-1
Notes:
This assumes that your port number is derived from the second part of the code in the square brackets by removing the first three digits of that part.
To make the script executable, you need to do a chmod +x with the script name as the second argument.
The input is copied to /dev/stdout, with the targeted lines altered. To write it to a file, just redirect the output to whatever file you wish. (E.g., ./rock1961.awk data.file > altered_data.file)
The target lines are selected by the presence of the [nnn-nn_nnnnn], but not checked for the "Title" at the start. If that would be useful, just change the selection criteria from /\[[[:digit:]-]+_[[:digit:]]+\]/ to /Title[[:space:]]*\[[[:digit:]-]+_[[:digit:]]+\]/
Last edited by PTrenholme; 08-02-2011 at 05:11 PM.
You might want to consider always using at least the maximum number of digits required for the largest port number, with leading zeros. This makes the listing sort well, since the alphabetic form and the numeric form sort the same. Might not apply to your situation, but if it does, now is the time to get it right.
--- rod.
Edit: wouldn't you know someone would say the same thing while I was typing. Note to self: learn to type fasterer.
This is a little off though, since it will match everything from 10100-10149. It sure would be nice if regex could match number sequences larger than one digit.
So you'd probably have to embed it in a shell loop instead to match each number in turn, and fool with zero-padding as well.
bash v.4's zero-padding in brace-expansion makes it relatively easy.
Code:
for num in {01..48}; do
sed -i "s/101${num}/Port ${num#0}/2" file
done
Change the brace expansion to "{0{1..9},{10..48}}" for v.3.
$ cat rock1961.txt
Title[3560-1_10127]: Traffic Analysis for 10127 -- 3560-1
$ ./rock1961.gawk rock1961.txt
Title[3560-1_10127]: Traffic Analysis for Port 27 -- 3560-1
Notes:
This assumes that your port number is derived from the second part of the code in the square brackets by removing the first three digits of that part.
To make the script executable, you need to do a chmod +x with the script name as the second argument.
The input is copied to /dev/stdout, with the targeted lines altered. To write it to a file, just redirect the output to whatever file you wish. (E.g., ./rock1961.awk data.file > altered_data.file)
The target lines are selected by the presence of the [nnn-nn_nnnnn], but not checked for the "Title" at the start. If that would be useful, just change the selection criteria from /\[[[:digit:]-]+_[[:digit:]]+\]/ to /Title[[:space:]]*\[[[:digit:]-]+_[[:digit:]]+\]/
this was the ticket. running this script against the file first and redirecting to another file and then changing the Title string to PageTop and running it again against the new file did exactly what i wanted. that you so much for your help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.