LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-12-2012, 05:36 AM   #1
Sunvic
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Rep: Reputation: Disabled
Post 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

where coutput1 looks like this:
Code:
          Cell 01 - Address: AA:BB:CC:DD:EE:FF
          Cell 02 - Address: AA:BB:CC:DD:EE:FF
          Cell 03 - Address: AA:BB:CC:DD:EE:FF
the line
Code:
var$i=`sed -n "$i"p "~/Desktop/coutput1" | cut -c 30-47`
just doesn't seem to want to work and i don't know where to go. Maybe there's other problems with the rest of the code?

I appreciate that this code is inefficient but most of it was for my own learning and compiled from multiple online guides etc

Last edited by Sunvic; 08-12-2012 at 05:46 AM. Reason: small mistake
 
Old 08-12-2012, 06:07 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
1 members found this post helpful.
Old 08-12-2012, 06:24 AM   #3
Sunvic
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thumbs up

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`
which now cuts the address out precisely and stores it as the specific var
 
Old 08-12-2012, 06:30 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
1 members found this post helpful.
Old 08-12-2012, 06:35 AM   #5
Sunvic
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
*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

Last edited by Sunvic; 08-12-2012 at 06:38 AM. Reason: more info
 
Old 08-12-2012, 07:55 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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)
 
1 members found this post helpful.
Old 08-12-2012, 08:30 AM   #7
Sunvic
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Question

Thanks for the continued help

Code:
#!/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:
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
when I thought it should only return 3 addresses for this case?
 
Old 08-12-2012, 08:50 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
1 members found this post helpful.
Old 08-12-2012, 09:08 AM   #9
Sunvic
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Smile

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)
which seems to have sorted out my problem.

Thanks for your help

Last edited by Sunvic; 08-12-2012 at 10:13 AM. Reason: update
 
Old 08-12-2012, 10:21 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
-n means do not print by default
-e means use extended regexes

SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 08-12-2012, 11:03 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The sed would work just as well:
Code:
sed -n "$i p" ~/Desktop/coutput1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration