awk read RS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk read RS
# 1  
Old 05-23-2012
awk read RS

I need to know if there is a way to use the while read command from a awk record. The record has 3 or 4 lines and I need the line to be all of the record instead of just each line of it.
Code:
nawk 'BEGIN {RS="!"} /atm pvc/ {print $0}' router.list | while read line
do
VP=`echo "$line" | egrep "atm pvc" | nawk '{print $4}'`
VC=`echo "$line" | egrep "atm pvc" | nawk '{print $5}'`
PROFILE=`echo "$line" | egrep "atm pvc" | nawk '{print $7}'`
ENACP=`echo "$line" | egrep "encapsulation"`
INTERFACE=`echo "$line" | egrep "ip description" | nawk '{print $3}'`
echo "atm pvc $VP $VC profile dsl-$PROFILE $ENCAP"
if [[ $ENCAP == "encapsulation pppoe" ]] ; then
echo "bind authentication pap maximum 1"
else
echo "bind interface $INTERFACE [ADSL]"
fi
done

An example of the input file is:
Code:
interface atm 1/0.20292 point-to-point
 atm pvc 100020292 2 292 aal5snap 768 768 10
 encapsulation pppoe
! 
interface atm 1/0.20292.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20293 point-to-point
 atm pvc 100020293 2 293 aal5snap 1500 1500 10
 encapsulation pppoe
! 
interface atm 1/0.20293.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20294 point-to-point
 atm pvc 100020294 2 294 aal5snap 1500 1500 10
 encapsulation pppoe
! 
interface atm 1/0.20294.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20295 point-to-point
 atm pvc 100020295 2 295 aal5snap 3000 3000 10
 encapsulation pppoe
! 
interface atm 1/0.20295.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20296 point-to-point
 atm pvc 100020296 2 296 aal5snap 6000 6000 10
 encapsulation pppoe
! 
interface atm 1/0.20296.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20297 point-to-point
 atm pvc 100020297 2 297 aal5snap 1500 1500 10
 encapsulation pppoe
! 
interface atm 1/0.20297.1
 encapsulation ppp
 ppp authentication pap
 profile any "prof-pppoe"
! 
interface atm 1/0.20298 point-to-point
 atm pvc 100020298 2 298 aal5snap 3000 2900 10
 encapsulation bridge1483
 ip description AAAService-1/0-2-298 [local]
 ip address 192.168.217.86 255.255.255.248
!

# 2  
Old 05-23-2012
And the expected output is ... ?
# 3  
Old 05-23-2012
For an example, of one record.
Input
Code:
interface atm 1/0.20293 point-to-point
 atm pvc 100020293 2 293 aal5snap 1500 1500 10
 encapsulation pppoe

Output
Code:
atm pvc 2 293 profile dsl-1500 encapsulation pppoe
bind authentication pap maximum 1

# 4  
Old 05-23-2012
And... What logic converts one to the other? Where does 'dsl-' come from? etc.
# 5  
Old 05-23-2012
Code:
awk '/^!/ && prf { 
    print atm, atm_, vp, vc, prf, encap
    print "bind", (encap == "pppoe" ? "authentication pap maximum 1" : iface)
    prf = encap = x    
  }
/atm pvc/ { 
  atm = $1; atm_ = $2; vp = $4; 
  vc = $5; prf = "profile dsl-" $7 
  }
/encapsulation/ { encap = $2 }
/ip description/ { iface =  "interface " $3 " [ADSL]" }
  ' infile


Last edited by radoulov; 05-23-2012 at 07:32 PM.. Reason: Shortened ...
# 6  
Old 05-23-2012
from an echo statement with variables pulled from the awk record like:
Code:
echo "atm pvc $VP $VC profile dsl-$PROFILE $ENCAP"

---------- Post updated at 05:32 PM ---------- Previous update was at 05:25 PM ----------

Radoulov, I get the folling from your code:
Code:
#
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 5
awk: bailing out near line 5
 
#!/bin/ksh 
#
awk 'END {
  if (prf) print atm, atm_, vp, vc, prf, encap
  print "bind", (encap == "pppoe" ? "authentication pap maximum 1" : iface)
  }
/^interface/ {
  if (prf) {
    print atm, atm_, vp, vc, prf, encap
      print "bind", (encap == "pppoe" ? "authentication pap maximum 1" : iface)
    prf = encap = iface = x
    }
  }
/atm pvc/ {
  atm = $1; atm_ = $2; vp = $4;
  vc = $5; prf = "profile dsl-" $7
  }
/encapsulation/ { encap = $2 }
/ip description/ { iface =  "interface " $3 " [ADSL]" }
  ' input.file

# 7  
Old 05-23-2012
Quote:
Originally Posted by numele
Radoulov, I get the folling from your code:
Code:
#
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 5
awk: bailing out near line 5
[...]

Try the new version and use nawk instead of awk.
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a file using awk for a given no of lines.

Hi, how do i read a file using awk for a given no of line? e.g 1. read only first 50 line. 2. read starting from line 20 to line 60.. thanks in advance. -alva (8 Replies)
Discussion started by: alvagenesis
8 Replies

2. Shell Programming and Scripting

read the variable with awk

final.txt file contains SY10020021 SY10023077 3199 4 803.815 11884 4 1825.22 2.2707 say set FIRSTLINE = SY10020021 set SECONDLINE=SY10023077 cat final.txt | awk '{if($1==${FIRSTLINE} & $2==${SECONDLINE}){print $9}else{print "ll"}}'..............this should give me value... (1 Reply)
Discussion started by: Indra2011
1 Replies

3. Shell Programming and Scripting

How to use awk with while read line

Hi All, Content of mydatafile- Name Age -------------- --------------- Raju P 20 years Hari 25 years Priya S 30 years I need output like- The age of Raju P is 20 years The age of Hari is 25 years The age of Priya S is... (3 Replies)
Discussion started by: NARESH1302
3 Replies

4. Shell Programming and Scripting

Awk and read bailing out

Hi All, Can some body help me in this to work #!/bin/ksh nof=`wc -l outFile_R.out | sed -e 's/*//g' ` no_of_lines=`expr $nof - 0` z=1 while ] do cat outFile_R.out | awk -v I="$z" 'NR==I { print $0 }' | read from_date to_date id echo "executing $from_date... (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

6. UNIX for Dummies Questions & Answers

Using Awk within awk to read all files in directory

I am wondering if anyone has any idea how to use an awk within awk to read files and find a match which adds to count. Say I am searching how many times the word crap appears in each files within a directory. How would i do that from the command prompt ... thanks (6 Replies)
Discussion started by: flevongo
6 Replies

7. Shell Programming and Scripting

Using awk to read a field

I am trying to output the total number of records that have name and address within there specific fields i.e. $6 (surname) $9 (address). The file that redirects in is a csv file. The code is wrong somewhere as i have another awk similar to this that reads in the same file and that works... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

8. Shell Programming and Scripting

How to re-read a file in AWK

Hi , i am having some problem with re-reading the same file in AWK. here is the scenario. function 1 { some_string > " file1 " # i have redirected output to file1. ........... ........ } Now in function 2 { ... (1 Reply)
Discussion started by: madhaviece
1 Replies

9. Shell Programming and Scripting

File Read using awk

Hi, I need to read two colums(4th and 5th) from a file and do some manipulation Input file is 401500 IOC Q 14 14 406200 LC Q 1 1 410124 IOC Q 5 4 410124 LC Q 11 8 410132 IOC Q 230 229 410148 IOC Q ... (3 Replies)
Discussion started by: rejirajraghav
3 Replies

10. Shell Programming and Scripting

Help with awk - read from file

Hi, I've got a file like the following: Starting to process segment 0 (and symmetry related segments) Number of (cancelled) singularities: 0 Number of (cancelled) negative numerators: 0 Segment 0: 5.49secs Starting to process segment 1 (and symmetry related segments) Number of... (7 Replies)
Discussion started by: giorgos193
7 Replies
Login or Register to Ask a Question