Trying to make a script that will search and replace text in a file
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.
./test: line 10: export: `-m': not a valid identifier
./test: line 10: export: `option:': not a valid identifier
line 10 being the line in my script with export and ipcalc. My debian ipcalc has this as options
Code:
Usage: ipcalc [options] <ADDRESS>[[/]<NETMASK>] [NETMASK]
ipcalc takes an IP address and netmask and calculates the resulting broadcast,
network, Cisco wildcard mask, and host range. By giving a second netmask, you
can design sub- and supernetworks. It is also intended to be a teaching tool
and presents the results as easy-to-understand binary values.
-n --nocolor Don't display ANSI color codes.
-b --nobinary Suppress the bitwise output.
-c --class Just print bit-count-mask of given address.
-h --html Display results as HTML (not finished in this version).
-v --version Print Version.
-s --split n1 n2 n3
Split into networks of size n1, n2, n3.
-r --range Deaggregate address range.
--help Longer help text.
So I installed FC7. But for various reasons I decide to finish this off in Debian. So I mounted the FC7 install in Debian and copied /bin/ipcalc (FC7) to /bin in Debian. I then ran the script. What I found interesting is that it would in places, insert the system environment variables. As they would appear if you ran env but with export in front of each variable. The script is exactly as you have it here. I will run it in due course in FC to see whether the same happens.
It was not a job to edit it manually, though either it missed one entry or I deleted one by mistake. I am going to go with the latter.
Chris - Although I still have not solved the minor problem detailed above, I want to give you my heartfelt appreciation for all the effort and help you have put in here. It has helped me enormously.
I will take some time to actually try and work out what everything does and I might come back with some questions. Again, thanks immensely.
Chris - The problem persists in FC7 ie, that variables that are output when I run export from the command line appear in the output file (with export and a space ahead of them). For example, export HOME=/home/jongi appears in the file.
Below is my interpretation of the two lines:
Code:
export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'))
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
I assume the first line sets up the variable NETMASK.
Code:
echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'
each line of newroutes.txt is echoed and that output is processed by sed. Now how the notation for sed is translated is a bit hard for me.
Code:
^.*net\ \([0-9\.\/]*\).*$
This is what sed searches for in the echo output.
and replaces it with
Code:
/\1/
How to interpret it is confusing me. My guess though (based on what it is I am trying to achieve here) is that it outputs something like 216.104.192.0/21. And this is then used by ipcalc to run ipcalc -m 216.104.192.0/21. This returns NETMASK=255.255.248.0 which is then set as the variable NETMASK by export.
Code:
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
The second line takes the same line that has been processed above and replaces /21 with netmask 255.255.248.0. Does it as follows:
Code:
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
Sed again processes the output of echo. Sed searches for
Code:
/[0-9]*
a number starting with 0-9, the start means it is more than 1 digit (?) and preceded by / ie a number like /21
i've lost a bit of the track of this thread TBH, and from the output above, i'm not sure where those variables come from, currently too exhausted to look too!. Not that elegant a solution really though, should need an intermediate file should you?
Code:
cat newroutes.txt | while read route
do
export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'))
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
done | grep route > correct.txt
or if it's actually the export line giving that additional output...
Code:
cat newroutes.txt | while read route
do
export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/')) > /dev/null
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
done > correct.txt
Last edited by acid_kewpie; 07-08-2007 at 12:40 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.