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 where coutput1 looks like this: Code:
Cell 01 - Address: AA:BB:CC:DD:EE:FF Code:
var$i=`sed -n "$i"p "~/Desktop/coutput1" | cut -c 30-47` I appreciate that this code is inefficient but most of it was for my own learning and compiled from multiple online guides etc |
If I read it correctly, you want to print only the lines containing the contents of the variable "i".
For that, write the SED command like this: sed -n "/$i/p" filename The double-quotes allow the variable name to be expanded before sed processes it. The address range in SED is either by line number, or by content. If you use line numbers, then the /../ is not used. |
Thanks for your quick and helpful reply.
I changed the code slightly to build an array and using your /../ I altered the line to read: Code:
var[$i]=`sed -n /$i/p ~/Desktop/coutput1 | cut -c 30-47` |
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 |
*thinking I had created a simple for loop*
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 |
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: Code:
variable=$(sed -n "/$i/p" ~/Desktop/coutput1 | cut -c 30-47) |
Thanks for the continued help
Code:
#!/bin/bash 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: Code:
2C:B0:5D:CC:98:26 00:01:E3:E8:D6:88 2C:B0:5D:CC:98:26 00:22:3F:32:9C:52 00:22:3F:32:9C:52 00:01:E3:E8:D6:88 |
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.
altered the line to read: Code:
var[$i]=$(awk "NR==$i" ~/Desktop/coutput1 | cut -c 30-46) Thanks for your help |
-n means do not print by default
-e means use extended regexes SED tutorial here: http://www.grymoire.com/Unix/Sed.html |
The sed would work just as well:
Code:
sed -n "$i p" ~/Desktop/coutput1 |
All times are GMT -5. The time now is 02:53 PM. |