shell script parsing with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers shell script parsing with sed
# 1  
Old 12-11-2007
Question shell script parsing with sed

#I'm quite new to scripting and my boss has asked me to solve a simple problem and sadly, I can't figure out how to do it. Any help is appreciated.
Smilie

#The following is a small shell script and the output that it produces for google.com.

#!/bin/sh
whois $1 | grep "Name Server"
dig +short $1 mx
dig +short mail.$1

[jjamd64@localhost ~]$ ./dn.sh google.com
Name Server: NS1.GOOGLE.COM
Name Server: NS2.GOOGLE.COM
Name Server: NS3.GOOGLE.COM
Name Server: NS4.GOOGLE.COM
10 smtp3.google.com.
10 smtp4.google.com.
10 smtp1.google.com.
10 smtp2.google.com.
googlemail.l.google.com.
66.249.83.83
66.249.83.19

#I need to modify this script so that it deletes the spaces in the beginning of the "name server" line, deletes the numbers and space before the mx records and replaces with a + symbol, and places a - symbol before the IP's. Here is what it should look like:

[jjamd64@localhost ~]$ ./dn.sh google.com
Name Server: NS1.GOOGLE.COM
Name Server: NS2.GOOGLE.COM
Name Server: NS3.GOOGLE.COM
Name Server: NS4.GOOGLE.COM
+smtp3.google.com.
+smtp4.google.com.
+smtp1.google.com.
+smtp2.google.com.
googlemail.l.google.com.
-66.249.83.83
-66.249.83.19

#I would like to use the sed command if possible and any hints would be much appreciated. I'm open to other suggestions as long as it produces the desired output. I really appreciate any direction and tips. THANKS! Smilie
# 2  
Old 12-11-2007
Quote:
Originally Posted by jjamd64
I would like to use the sed command if possible
Out of interest, given that you say you can't figure it out, why do you care whether sed is used or not?
# 3  
Old 12-11-2007
I would like to get more familiar with sed if possible, but I am open to absolutely any suggestions.
# 4  
Old 12-11-2007
OK, I have the first two parts done, the following sed arguments produce the output I need, but I dont know how to insert the - symbol before the IP's. Anyone got any ideas?


#!/bin/sh
whois $1 | grep "Name Server"|sed "s/ //"
dig +short $1 mx|sed "s/[0-9][0-9] /+/"
dig +short mail.$1

[jjamd64@localhost ~]$ ./dn.sh google.com
Name Server: NS1.GOOGLE.COM
Name Server: NS2.GOOGLE.COM
Name Server: NS3.GOOGLE.COM
Name Server: NS4.GOOGLE.COM
+smtp3.google.com.
+smtp4.google.com.
+smtp1.google.com.
+smtp2.google.com.
googlemail.l.google.com.
66.249.83.83
66.249.83.19
# 5  
Old 12-11-2007
Personally I would pipe it through a read loop

Code:
while read N
do
       case "$N" in
       [0-9]* )
             echo \-"$N"
             ;;
       * )
             echo "$N"
            ;;
       esac
done

But I'm sure some AWK guru can produce some hieroglyphics in a single line to do the job.
# 6  
Old 12-11-2007
Try this:

Code:
sed 's/^\(..\....\...\...$\)/-\1/'

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing XML using shell script

Well, issue is i have to parse this script to get the VERSION: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleAllowMixedLocalizations</key> ... (9 Replies)
Discussion started by: zorosinister
9 Replies

2. Shell Programming and Scripting

parsing using shell script

I have a file parameters.txt which contains 151524 151525 I have another file OID.csv which contains NE Version Object Type ID SDK param name Object OID test1 Start: 4.2 End: 4.2 pan 151524 speed ... (5 Replies)
Discussion started by: LavanyaP
5 Replies

3. Shell Programming and Scripting

XML parsing using shell script

I have a xml file like this <bul:collectionStrategy name="strategy1"> <bul:collectionTemplateGroup name="15min group"/> <bul:collectionTemplateGroup name="hourly group"/> </bul:collectionStrategy> <bul:CollectionTemplateGroup name="hourly group" > ... (2 Replies)
Discussion started by: LavanyaP
2 Replies

4. Shell Programming and Scripting

XML parsing in a shell script.

Below is a XML I have... <?xml version="1.0" encoding="UTF-8" ?> <component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:XXXXX-www-Install-Manifest manifest.xsd" xmlns="urn:qqqqq-Install-Manifest" name="OM" ... (1 Reply)
Discussion started by: dashok.83
1 Replies

5. Shell Programming and Scripting

Parsing XML using Shell Script

Hello, I'm a starting shell scripter and no Perl knowledge. I've trying to do this for a while: I want to parse an XML file and get certain data out of it and write that data into a CSV file, all this using Shell Scripting (in Bash). Or Perl without any XML Parser/Interpreter (if possible). ... (1 Reply)
Discussion started by: Kage Musha
1 Replies

6. Shell Programming and Scripting

Shell script to parsing log

Hi I Have log like this : 0 234: { 3 2: 04 EE 7 14: '20081114081' 23 1: 00 79 10: '38809' 91 15: '528111510010159' 143 29: 'Streaming/downloading service' 174 3: 'MTV' 179 43: 'rtsp://172.28/MTV2GO-Loop.sdp' 224 1: 05 ... (10 Replies)
Discussion started by: justbow
10 Replies

7. Shell Programming and Scripting

parsing a string in a shell script

I need to parse a string in a shell script. I understand there is some in built function to use that. can someone explain the syntax ? Say, it is like this #!/bin/ksh read input # input is entered as 'welcome' #Now I want to extract say only first 4 characters or last four #characters. ... (19 Replies)
Discussion started by: asutoshch
19 Replies

8. Shell Programming and Scripting

Parsing a line in Shell Script

I have record line somthing like below with first line showing char spacing not real record line 1 | 2 | 3rd Field--------------|-4th field--| This is charcatersapcing of line DF20000000000000000130.7890000000750 I shoudl get two line from above line 1st line should 1 | 2 | 3rd... (3 Replies)
Discussion started by: unishiva
3 Replies

9. Shell Programming and Scripting

Parsing a file in Shell Script

Hi, I have a requirement. I have an application which can take a file as inputs. Now the file can contain any number of lines. The tool has to pick up the first uncommented line and begin processing it. For example the file could be like this: #MANI123|MANI1234 #MANI234|MANI247... (4 Replies)
Discussion started by: sendhilmani123
4 Replies

10. Shell Programming and Scripting

shell script argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies
Login or Register to Ask a Question