Sponsored Content
Top Forums Shell Programming and Scripting ...yet another string of awk/sed questions from a RegExp-Challenged luser %-\ Post 302358482 by SteveB-in-LV on Friday 2nd of October 2009 05:37:34 PM
Old 10-02-2009
...sorry about that. I certainly didn't mean to post with all kinds of weird formatting. :-(

My previous post was just text entered into the forums' Web interface.

...I'll try it again.

--Steve

---------- Post updated at 02:20 PM ---------- Previous update was at 02:06 PM ----------

Greetings all,

...here is yet another string of awk/sed questions from a RegExp-Challenged luser

I'm looking to have sed/awk do some clean-up on routing tables and to that end, I would like to do the following:

1.) If a line contains the word "masks" or "subnets" prepend CR/LF to the beginning of the line.
For Example, turn the following:
=======================================================
Code:
 
C 192.168.1.1/32 is directly connected, Loopback0
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
D 172.16.0.128/25 
[90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10
D 172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10
C 172.16.255.1/32 is directly connected, Loopback1
D 172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10
D 172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10
D 172.16.255.10/32 
[90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
D 172.16.255.11/32 
[90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10
D 172.16.10.0/24 
[90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
C 172.16.1.0/29 is directly connected, Vlan10
D 172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
D 172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001
D 172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10
[90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001

=======================================================

Into this:
=======================================================
Code:
 
C 192.168.1.1/32 is directly connected, Loopback0
 
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
D 172.16.0.128/25 
[90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10
D 172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10
C 172.16.255.1/32 is directly connected, Loopback1
D 172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10
D 172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10
D 172.16.255.10/32 
[90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
D 172.16.255.11/32 
[90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
D 172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10
D 172.16.10.0/24 
[90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
C 172.16.1.0/29 is directly connected, Vlan10
D 172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10
 
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
D 172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001
D 172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10
[90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001

=======================================================

2.) If a line begins with a [CDS] move this character to the end of the line.
Now the routing table snippet above would look like this:
=======================================================
Code:
 
192.168.1.1/32 is directly connected, Loopback0 C
 
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
172.16.0.128/25 D
[90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10 D
172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10 D
172.16.255.1/32 is directly connected, Loopback1 C
172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10 D
172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10 D
172.16.255.10/32 D
[90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
172.16.255.11/32 D
[90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10 D
172.16.10.0/24 D
[90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
172.16.1.0/29 is directly connected, Vlan10 C
172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10 D
 
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001 D
172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10 D
[90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001

=======================================================

3.) If a line begins with a "[" append it to the preceding line (i.e. insert a few backspace characters to basically over-write the CR/LF of the preceding line ?).
Now the routing table snippet above would look like this:
=======================================================
Code:
 
192.168.1.1/32 is directly connected, Loopback0 C
 
172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
172.16.0.128/25 D [90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10 D
172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10 D
172.16.255.1/32 is directly connected, Loopback1 C
172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10 D
172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10 D
172.16.255.10/32 D [90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
172.16.255.11/32 D [90/13184000] via 172.21.1.1, 5d04h, Tunnel172021
172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10 D
172.16.10.0/24 D [90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
172.16.1.0/29 is directly connected, Vlan10 C
172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10 D
 
172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001 D
172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10 D [90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001

=======================================================
4.) Delete all the leading space (I think this will do the trick):
Code:
sed 's/^[ ]*//g'

5.) Sort the one or more lines that appear between blank lines, by IP address.
Now the routing table snippet above would look like this:
=======================================================
192.168.1.1/32 is directly connected, Loopback0 C

172.16.0.0/16 is variably subnetted, 21 subnets, 5 masks
172.16.0.128/25 D [90/13058560] via 172.21.1.1, 5d04h, Tunnel172021
172.16.1.0/29 is directly connected, Vlan10 C
172.16.1.48/30 [90/28416] via 172.16.1.3, 5d05h, Vlan10 D
172.16.3.0/25 [90/28928] via 172.16.1.3, 1w0d, Vlan10 D
172.16.10.0/24 D [90/13058560] via 172.22.1.1, 5d04h, Tunnel172022
172.16.254.0/24 [90/28928] via 172.16.1.3, 2w4d, Vlan10 D
172.16.255.1/32 is directly connected, Loopback1 C
172.16.255.2/32 [90/156160] via 172.16.1.2, 5d05h, Vlan10 D
172.16.255.3/32 [90/156672] via 172.16.1.3, 2w3d, Vlan10 D
172.16.255.4/32 [90/156672] via 172.16.1.3, 1w4d, Vlan10 D
172.16.255.10/32 D [90/13184000] via 172.22.1.1, 5d04h, Tunnel172022
172.16.255.11/32 D [90/13184000] via 172.21.1.1, 5d04h, Tunnel172021

172.19.0.0/16 is variably subnetted, 8 subnets, 4 masks
172.19.255.1/32 [90/12953600] via 172.17.0.38, 5d05h, Tunnel1720170001 D
172.19.255.2/32 [90/12956160] via 172.17.1.2, 4d23h, Vlan10 D [90/12956160] via 172.17.0.38, 4d23h, Tunnel1720170001
=======================================================

In the most general terms, I'm struggling to work with metacharacters like tab, CR/LF and backspace (1. and 3. above) and with chunks of a file that contain multiple lines (5.) in sed.

I image awk may be the better choice for this task but I'm completely unfamiliar with awk scripting.

So what do you think? Is the above even possible?
Is it a complete kludge to which you can offer a more elegant and efficient solution?

I'm novice enough in the scripting world so that just about everything I do is a bit of a kludge

...anyway...PLEASE let me know your thoughts on how I might make a large collection of routing tables easier to read/report on/summarize/further analyze or just how I might do a couple of cool new tricks with sed or awk (...or whatever other tool you think is suited to this task).

THANK YOU VERY MUCH, in advance, for any help or suggestions you may offer me!

--Steve

---------- Post updated at 02:24 PM ---------- Previous update was at 02:20 PM ----------

...is my latest post in a more readable/usable format?

--Steve

---------- Post updated at 02:37 PM ---------- Previous update was at 02:24 PM ----------

...I've made a little progress on this problem since my first post (...but knowing me my "solution" is more klunky than it should be).

...anyway, here's what I've come up with to move the initial route source identifiers to the end of their respective line and than clear all the leading white space from every line:

Code:
sed '/^D EX/s/.$/\tD EX/;s/^D EX//;/^D/s/.$/\tD/;s/^D//;/^C/s/.$/\tC/;s/^C//;/^S\*/s/.$/\tS\*/;s/^S\*//;/^S/s/.$/\tS/;s/^S//;s/^[ \t]*//' GNWAN-RT > GNWAN01-RT

I'd like to make this whole line cleaner by handling all the route source identifiers in a single characters set, but I don't yet know how to do this.
The character set would look something like this:
Code:
["D EX"D"S*"SC]

Appending a line to the one preceding is still a mystery to me too.

...anyway...thanks for looking at this!

--Steve
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

regexp with sed again!!!

please help: I want to add 1 space between string and numbers: input file: abcd12345 output file: abcd 1234 The following sed command does not work: sed 's/\(+\)\(+\)/\1 \2/' file Any ideas, please Andy (2 Replies)
Discussion started by: andy2000
2 Replies

2. Shell Programming and Scripting

regexp to get first line of string

Hi everybody for file in * #Bash performs filename expansion #+ on expressions that globbing recognizes. do output="`grep -n "$1" "$file"`" echo "$file: `expr "$output" : '\(^.*$\)'`" done In the above bash script segment, I try to print just the first line of string named... (3 Replies)
Discussion started by: jonas.gabriel
3 Replies

3. Shell Programming and Scripting

sed regexp

Hi, I am not that good with reg exp and sed. But I was just looking at something the other day and came across a situation. When I ran the below command: echo "123 word" | sed 's/*/(&)/' the op was: (123) word But when I ran: echo "123 word" | sed 's/*/(&)/g' the o/p was: (123)... (4 Replies)
Discussion started by: King Nothing
4 Replies

4. UNIX for Dummies Questions & Answers

sed before and after regexp

Dear all i have the code which print 1 line of context before and after regexp, with line number sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h the code work well but any one can tell me what each letter mean {=;x;1!p;g;$!N;p;D;} also how i can print 2 line before and onle line after ... (2 Replies)
Discussion started by: soly
2 Replies

5. Shell Programming and Scripting

awk, sed or perl regexp to print values from file

Hello all According to the following file (orignal one contains 200x times the same structure...) I was wondering if someone could help me to print <byte>??</byte> values example, running this script/command like ./script.sh xxapp I would expect as output: 102 116 112 ./script.sh xxapp2... (2 Replies)
Discussion started by: cabrao
2 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

8. Shell Programming and Scripting

Regexp for string that might contain a given character

I'm probably just not thinking of the correct term to search for :-) But I want to match a pattern that might be 'ABC' or '1ABC' there might be three characters, or there might be four, but if there are four, the first has to be 1 (1 Reply)
Discussion started by: jnojr
1 Replies

9. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

10. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies
All times are GMT -4. The time now is 07:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy