[SOLVED] Using a variable in sed to pick a specific line from a text file, bash
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Using a variable in sed to pick a specific line from a text file, bash
I'm trying to run the command iwlist scan, remove the errors and process the output to my own specification by using grep & sed to find and pick specific lines, from a text file, over a for loop and then store some of the information as a variable for future use as I develop the code
my code looks like this at the moment
Code:
#!/bin/bash
# counts number of cells found, i.e. number of broacasting routers in range
varA=`iwlist scan 2>/dev/null | egrep -c "Cell"`
#saves the output minus errors
iwlist scan 2>/dev/null | egrep "Address" > ~/Desktop/coutput1
# my for loop where i try to set var(1,2,3 etc) to the output in coutput1
for i in $(eval echo {1..$varA})
do
var$i=`sed -n "$i"p "~/Desktop/coutput1" | cut -c 30-47`
done
# print var's
echo $varA
echo $var$1
I try to cut the output below and for example save var1 as the address for cell 01
The issue with the line is the use of variable expansion on the left side of the equals.
I would also query the need to use eval?? Is there a reason you do not create a simple for loop?
My suggestion for the "var" variable would be to make it an array
My only knowledge really comes from Matlab, how would you suggest the for loop is created to incorporate the maximum being the variable varA?
instead of storing as an array (which is like a row vector?) can I create a 2D-matrix (to store more information as I increase the variables, I would like to store SSID's and channels etc)?
thanks for your help
Last edited by Sunvic; 08-12-2012 at 06:38 AM.
Reason: more info
You can sometimes get away with it, but it is recommended practice to always quote the sed command string. The typical recommendation is to use hard quotes (single), unless, there is a need for soft quotes (eg your case).
Also, the use of backtics (while still legal) is "deprecated".
Incorporating these 2 comments, the form would be:
#!/bin/bash
clear
#save the output minus errors
iwlist scan 2>/dev/null | egrep "Address" > ~/Desktop/coutput1
# counts number of cells found, i.e. number of broacasting routers in range
varA=`egrep -c "Cell" ~/Desktop/coutput1`
echo $varA
# for loop to assign array var to each address
for ((i=1; i<=$varA; i++));
do
echo $i
var[$i-1]=$(sed -n -e "/$i/p" ~/Desktop/coutput1 | cut -c 30-46)
done
echo ${var[@]}
Is my new code (including a change in the way the for loop is built) but it returns something confusing.
I have var starting at 0 as I have read this is the first entry?
But on the last echo command I get more entries than varA implies I should be getting. ie if there's 3 addresses var seems to have these 3 addressed stored multiple times and in strange orders.
Here's an example if I run the script of the output of echo var:
I was wondering if you could show the output of your 'iwlist scan' as this seems to be a bit excessive (number of steps and some redundancy of repeated tasks, ie sed and grep)?
In answer to your question, have a think about what you have asked of sed? ie. On the first run when i = 1, you tell sed to find and print all lines that contain a '1'.
So if you extrapolate this into what would be presented each time (btw this is a simple grep when you look at it), you can see that there may be more lines with a 1, 2 or 3 on them so multiple returns.
Oh I see what you mean about the command i've given sed and it makes perfect sense, clearly I was under the impression that using -n -e searched for the specific line number given and this is not true.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.