solved -gawk, search for pattern - mark the previous line as a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting solved -gawk, search for pattern - mark the previous line as a variable?
# 1  
Old 05-13-2010
solved -gawk, search for pattern - mark the previous line as a variable?

Im trying to parse ifconfig with awk and setup a bunch of variables in one shot. But Im having trouble figuring out how to work with data in previous lines.

ifconfig output:
Code:
eth0      Link encap:Ethernet  HWaddr 00:50:DA:10:7F:1B  
          inet addr:10.10.10.10  Bcast:10.10.10.127  Mask:255.255.255.192
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1307626 errors:0 dropped:0 overruns:199 frame:0
          TX packets:429249 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:104476566 (99.6 Mb)  TX bytes:46971778 (44.7 Mb)
          Interrupt:12 Base address:0xc000 

eth1      Link encap:Ethernet  HWaddr 00:0B:AD:01:11:3F  
          inet addr:192.168.1.200  Bcast:192.168.1.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:456 (456.0 b)  TX bytes:126 (126.0 b)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:80524 errors:0 dropped:0 overruns:0 frame:0
          TX packets:80524 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:23978596 (22.8 Mb)  TX bytes:23978596 (22.8 Mb)

awk command:
Code:
ifconfig | awk '/addr\:/ {if($0 ~ "192.168.1" || $0 ~ "127.0.0.1") {next};\
 x=$2; y=NR-1}\
 /addr\:/ {if($0 ~ "192.168.1"); a=$2; b=NF-1}\
\
 END \
\
{sub("addr:","",x); split(x,ip,"."); split(b,ethlan,FS); split(y,ethwan,FS);\
\
 printf "export ipaddress=%s\nexport ISPIP=%s\nexport BUSIP=%s.%s.%s.%s\nexport VIPA=%s.%s.%s.%s\nexport WAN_DEV=%s\nexport LAN_DEV=%s\n",x,x,ip[1],ip[2],ip[3],int(ip[4]+1),ip[1],ip[2],ip[3],int(ip[4]+5),ethwan[1],ethlan[1]}'

I know that's sloppy formatting.. sorry about that.

Current output is this:

Code:
export ipaddress=10.10.10.10
export ISPIP=10.10.10.10
export BUSIP=10.10.10.11
export VIPA=10.10.10.15
export WAN_DEV=3
export LAN_DEV=1

Id like for it to be this:
Code:
export ipaddress=10.10.10.10
export ISPIP=10.10.10.10
export BUSIP=10.10.10.11
export VIPA=10.10.10.15
export WAN_DEV=eth0
export LAN_DEV=eth1

My problem is getting variable y and b, to be the eth0 and eth1 line (rather then the line number). Smilie Im sure that awk line could be more efficient but im not the greatest with awk... I cant even format the spacing correctly Smilie

---------- Post updated 05-13-10 at 01:02 PM ---------- Previous update was 05-12-10 at 03:37 PM ----------

ok, I got this worked through... I had a c dev help me out and he cleaned up my logic, and added a hold=$0 to the end, then I just change the b and y=NF-1 to b=$0 and y=$0. which gives me a variable for the previous line.

Last edited by trey85stang; 05-12-2010 at 05:57 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

How to delete the previous line after pattern match?

Team, I am writing a shell script to perform few health checks of the system, where I need to delete the previous line in the text file after pattern match using sed (or) awk. Could you please help me out on this? For example, <td> <td style=color:green align=center> </td> </tr>... (6 Replies)
Discussion started by: Nagaraj R
6 Replies

3. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

4. Shell Programming and Scripting

awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line. I want the pattern to be the beginning of the new line. I thought sed or awk could do this, but everything I try in sed gives me a "sed... (2 Replies)
Discussion started by: Drenhead
2 Replies

5. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

6. Shell Programming and Scripting

search pattern and mark/tag

Hi All, I have to search for patterns from a pattern file in a file and mark the matching lines. Input File: Student1 60 30 Student2 71 91 Student3 88 98 Pattern file: Student1 Fail Student2 Pass Student2 Pass Desired output: Student1 60 30 Fail Student2 71 91 Pass (5 Replies)
Discussion started by: saint2006
5 Replies

7. Shell Programming and Scripting

Sed: Working on a line Previous to a pattern.

Hello everyone, I am working with some train time tables, and i have hit a bit of a road block. Using grep/sed i have done a reasonable job of parsing the html into comma delimited format, but NJ transit prints The Track number and status on a new line, and I would much prefer it all on a... (6 Replies)
Discussion started by: mussen
6 Replies

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. UNIX for Dummies Questions & Answers

return previous line for pattern match

Hi, Need some idea on file processing, I have file like below, Processing al sources ... ...No value found : CHECK. Completed comparing all sources. Comparing schedulers... Processing al targets ... ...No value found : From above I need to extract the line where "No value... (4 Replies)
Discussion started by: braindrain
4 Replies

10. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies
Login or Register to Ask a Question