LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2012, 03:25 PM   #1
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 26

Rep: Reputation: Disabled
Printing foremated output unto a variable using AWK


I am trying to use AWK to search a text file and pull out a field. I want to take the output of the awk, strip off the first eleven characters and place that into a variable in a bash script.
The test script I am using for proof of concept is this:

Code:
#!/bin/bash

# test searching a file for an ip address based on an input variable

MSR=unset

echo "enter your PE router :"
read MSR

gawk -v pe="$MSR" '$1 == pe { print $NF }' lo1map
echo ""
echo ""
RD=$(gawk -v pe="$MSR" '$1 == pe { FS= "."} ; { print $5 }' lo1map)
sleep 3
echo "$MSR has a route descriptor of $RD"
this gives me an output of:
-bash-3.2$ ./greptest1
enter your PE router :
router1.nyr
192.168.0.50


router1.nyr has a route descriptor of

~~~~~~~~ several blank lines later~~~~~~~
66
53
78
62
70
69
79
58
68

What I am expecting for output is:
-bash-3.2$ ./greptest1
enter your PE router :
router1.nyr
192.168.0.50


router1.nyr has a route descriptor of 50

My input file lo1map looks like this (It's a lot longer though)

router1.ald 192.168.0.9
router1.bol 192.168.0.17
router1.chd 192.168.0.11
router1.dan 192.168.0.13
router1.dck 192.168.0.3
router1.den 192.168.0.15
router1.ft3 192.168.0.25
router1.lay 192.168.0.5
router1.mmd 192.168.0.19
router1.nyr 192.168.0.50


Thanks in advance!
 
Old 02-24-2012, 03:45 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Honestly, I think you went a bit overboard.....
Code:
#!/bin/bash

# test searching a file for an ip address based on an input variable

MSR=unset
INFILE=lo1map

echo "enter your PE router :"
read MSR

RD=$(grep $MSR $INFILE | awk -F. '{ print $5 }')

echo "$MSR has a route descriptor of $RD"
Cheers!

Josh
 
Old 02-24-2012, 10:51 PM   #3
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
Well my first question is why you set a variable to a string to then be overwritten later?
Code:
MSR=unset
My thought is you are trying to make sure the variable is not set which would be:
Code:
unset MSR
awk generally becomes clumsy when variables have to be used, so some other options are (using corp769's variable names):
Code:
RD=$(grep $MSR $INFILE | grep -Eo '[0-9]+$')

#or

RD=$(sed -n "/$MSR/s/.*\.//p" $INFILE)
 
1 members found this post helpful.
Old 03-01-2012, 11:32 AM   #4
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
thanks for the help, I went with the grep and awk option. i am very new to linux and bash scripting and I just don't have me head around SED yet.

thanks again for the quick responses!!
 
  


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
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
[SOLVED] storing grep output to feed awk to retrieve entire records matching variable chargaff Programming 8 08-13-2010 06:10 AM
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 07:13 AM
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 02:08 PM
how to pipe/redirect awk output into a variable? johnpaulodonnell Linux - Newbie 2 01-25-2007 06:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

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