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.
# network.txt
config interface lan
option ifname eth0.1
option type bridge
option proto static
option ipaddr 192.168.1.1
option netmask 255.255.255.0
config interface wan
option ifname eth1 #<- How do I grab this, store it as a var?
option proto dhcp
In this case I'm trying to grab eth1. It's not aways going to be eth1.
I was trying something like:
cat network.txt | grep "something"
If I try cat network.txt | grep "option ifname"
I get
Code:
option ifname lo
option ifname eth0.1
option ifname eth1
However, I only wanted the line that belonged to
Code:
config interface wan
which would be
Code:
option ifname eth1
but then that's still too much info because I wanted the last word:
Code:
eth1
Then I want to take the answer (In this case eth1) and store it as X=(the answer)
I'm assuming you're using strictly utilities available in the terminal, i.e. bash commands, is that right?
If so, do you plan on writing a script to do this or what?
When you suggest storing your result as X=(the answer) are you referring to an environment variable called X?
Will Networking.txt always be the same?
This is NOT the cleanest or best way to do this, in fact, it can likely be done with one or two "awk" or "grep" or "sed" statements, I'm just not that good, so hacking though,
Here's my suggestion, hopefully it gets you in the right direction.
//(1) write out the network.txt file with line numbers and print the line that has "config" and ends with "wan" because that's the interface you're looking for
cat -n network.txt | grep -r "config.*wan$"
//(2)send (1) via pipe to this statement to get just the line number from that string
awk -F " " '{print $1}'
//(3) with (2), we now just have a line-number (let's suppose it was at line 8), which we pipe to "bc" to add with 7 from the echo the simplified code looks like this
//we want to add some lines to the number eight where we found the "config.*wan$" string because the option iface will be after that
echo 7 + 8 | bc
//this echo piped to bc produces the number 15.
//(4) with the number 15, we pass as an argument to "head", which will pull the first 15 lines of a file provided
head -15 network.txt
//(5) with the first 15 lines of network.txt, we don't need all of them, we really only need the ones concerning the "wan" interface, so we pipe to tail
//so we end up with the last 5 lines of the first 15 lines of network.txt
head -15 network.txt | tail -5
//(6) we pipe this to grep for the "option iface" you were actually looking for
head -15 network.txt | tail -5 | grep "option ifname"
//(7) now we have the line we want "option ifname eth1" which we can cut up with awk to get the 3rd "element" of
head -15 network.txt | tail -5 | grep "option ifname" | awk -F " " '{print $3}'
//(8) we're all done, and it prints
eth1
//(9) set it to a variable
a=$(head -`echo 7 + $(cat -n network.txt | grep -r "config.*wan$" | awk -F " " '{print $1}') | bc` network.txt | tail -5 | grep "option ifname" | awk -F " " '{print $3}')
That version didn't allow for tabs, which I fixed shortly after I posted it (the tabs copied as spaces from the forum page). Please see my post again for a better version.
Kevin Barry
That version didn't allow for tabs, which I fixed shortly after I posted it (the tabs copied as spaces from the forum page). Please see my post again for a better version.
Kevin Barry
Kevin, Would it be possible to make the code also work for:
TaOkira, your code works great on both. Is there any way possible you could explain your code in such a way that a complete amateur such as myself could at least understand some of it?. I try using sed for simple things but I get stuck when I run into special characters.
grail, I could not get your awk or sed code to work. I was just getting a flashing prompt. I don't know what I was doing wrong.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.