how to extract last word and a number from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to extract last word and a number from a string
# 1  
Old 02-23-2012
how to extract last word and a number from a string

I have the following script (which I made by my self)

Code:
#!/bin/bash

# add a few empty lines to make it more legible

# add a date description on each update interval
echo "" >> /home/user/DYN_DNS_IP_change.log
echo "" >> /home/user/DYN_DNS_IP_change.log
echo "" >> /home/user/DYN_DNS_IP_change.log
#date_ =  date &
#string = "--------------------------------" + date_ + "--------------------------------"
echo "--------------------------------" >> /home/user/DYN_DNS_IP_change.log
date >> /home/user/DYN_DNS_IP_change.log
echo "--------------------------------" >> /home/user/DYN_DNS_IP_change.log
echo string >> /home/user/DYN_DNS_IP_change.log


echo "Running update IP address for @ domain" >> /home/user/DYN_DNS_IP_change.log
wget https://dynamicdns.park-your-domain.com/update?host=@&domain=xxxxx&password=AAAAAAAAAAAAAAA >> /home/user/DYN_DNS_IP_change.log


echo "--------- END OF ENTRY ---------" >> /home/user/DYN_DNS_IP_change.log

Now when running "wget https://dynamicdns.park-your-domain.com/update?host=@&domain=xxxxx&password=AAAAAAAAAAAAAAA", I get somethink like this SETDNSHOSTeng666.666.666.6600true (this really is IP address, I've just masked it, like the passwords and domains, etc.)
Now I'd like to extract this last word (true or false, depending on result) as well as the ip address. I dont know why, but there are 2 zeros right after the ip address. Also how could I make something like this: "----- DATE ----", I can only do "----" new line "DATE" new line "-----".
# 2  
Old 02-23-2012
You can use here-documents to simplify this eg:

Code:
cat <<EOF
 
 
 
-------------------  $(date) ---------------------
Running update IP address for @ domain
EOF >> /home/user/DYN_DNS_IP_change.log

Important to note that the "EOF" string at the end must be at the start of the line (if your indenting your code dont indent the EOF or it won't match with the <<EOF marker).

$(date) runs the date command and puts its output in-line.


Are you sure there aren't any non-printables in that return string? Try piping the output thru od -c eg:
Code:
wget https://dynamicdns.park-your-domain.com/update?host=@&domain=xxxxx&password=AAAAAAAAAAAAAAA | od -c

# 3  
Old 02-24-2012
Code:
user@comp:~$ wget https://dynamicdns.park-your-domain.com/update?host=@&domain=xxxxx.AAA&password=666666666666666666666666666666666 | od -c
[1] 7154
[2] 7155
0000000
--2012-02-24 05:07:52--  https://dynamicdns.park-your-domain.com/update?host=@
user@comp:~$ Resolving dynamicdns.park-your-domain.com... 74.81.78.59
Connecting to dynamicdns.park-your-domain.com|74.81.78.59|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 423 [text/html]
Saving to: `update?host=@.1'

100%[==============================================================================================================>] 423         --.-K/s   in 0s      

2012-02-24 05:07:52 (72.2 MB/s) - `update?host=@.1' saved [423/423]

^C
[1]-  Done                    wget https://dynamicdns.park-your-domain.com/update?host=@
[2]+  Done                    domain=xxxxx.AAA
juzer@ruter:~$

And here I had to ^C to exit and get this [2]+ Done message and return to bash, as it doesnt finish it self.

This is what I get after I manually enter this link to my web browser
SETDNSHOSTeng[my ip address here]00true
# 4  
Old 02-24-2012
For starters, you need to quote your URL. The ampersands in the URL are being interpreted by the shell and your command isn't running as you expect it too.

You might also want to set the -nv command line option on the wget command. That should disable the progress meter. That can be an issue if you are tying to capture other information that wget writes to stderr.
# 5  
Old 02-24-2012
Code:
wget http://dynamicdns.park-your-domain.com/update?host=mail&domain=DOMAIN&password=PASSWORD -O >> /home/user/DYN_DNS_IP_change.log

This is exactly how I execute it, or some other variations of parameters given to wget.
# 6  
Old 02-24-2012
I would suggest:

Code:
wget -nv "http://dynamicdns.park-your-domain.com/update?host=mail&domain=DOMAIN&password=PASSWORD" -O >> /home/user/DYN_DNS_IP_change.log



The quotes prevent shell from treating the '&' as an asynch operator and breaking the command at that point.

The -nv should turn off the progress meter. The progress meter might cause issues if you're trying to capture output from wget. I don't know if this will solve your problem, but they are the two things I'd try straight off.
# 7  
Old 02-25-2012
Nah, by running with that parameter, it just outputs what it had in the input, along with some other stuff, like the time, and some other numbers, but not the output which should've been seen.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract number from string.

Hi I am on Sun os. I have data in the below format and I need to grab the number out from the string. O/p needed: ---------- Post updated at 12:39 PM ---------- Previous update was at 12:32 PM ---------- I tried this but I am getting . at the front (14 Replies)
Discussion started by: dsravanam
14 Replies

2. Shell Programming and Scripting

Command to extract word from a string

Hi All, I have a word and from that word would like to search for certain set of string, is there any command to do so ? EX : Components from the above word, would like to search for strings set and extract the search string and then do if stmt... pon nen ent Com say... (2 Replies)
Discussion started by: Optimus81
2 Replies

3. Shell Programming and Scripting

Extract number part from the string in ksh 88

I have to extract number part (Date and timestamp part ) from the following 3 strings AB_XYZA_20130930183017.log AB_DY_XYZA_20130930183017.log AB_GZU_20130930183017.log Output should be 20130930183017 Please help me to get the string like above Thanks (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

Extract First and matching word from string in UNIX

Thank you (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

5. Shell Programming and Scripting

Using SED to extract a word or string.

I am working with the ksh shell in HP UNIX and I am attempting to extract a word, beginning with a particular string and ending at the first space. for example I want to extract the word or string MS_RECENT_ACTIVITY from the following string " This has been entered in MS_RECENT_ACTIVITY the... (2 Replies)
Discussion started by: simpletech369
2 Replies

6. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

7. Shell Programming and Scripting

Extract number that comes after a given string

Hi! I have a file that contains non-regular strings like: SCSGTI:N="$4,0,1,4,34622991111-->RemoteSPC: 1111", NWID=1; SCSGTI:N="$4,0,1,4,34622991211-->RemoteSPC: 1211", NWID=1; SCSGTI:N="$4,0,1,4,*-->RemoteSPC: 2112,Sec:RemoteSPC: 2212", NWID=1; SCSGTI:N="$4,10,1,4,34622999213-->RemoteSPC:... (4 Replies)
Discussion started by: Flavius
4 Replies

8. Shell Programming and Scripting

Extract single or multi word string in Cshell

I am using the following code: set LASInputFile = `ls *. | head -1` set COMPLine = `grep -i :COMPANY $LASInputFile` to extract the following line from my input file: COMP. XYZ Public Company :COMPANY NAME I now need to extract the full name of the company which... (15 Replies)
Discussion started by: phudgens
15 Replies

9. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

10. Shell Programming and Scripting

perl newbie: how to extract an unknown word from a string

hi, im quite new to perl regexp. i have a problem where i want to extract a word from a given string. but the word is unknown, only fact is that it appears as the second word in the string. Eg. input string(s) : char var1 = 'A'; int var2 = 10; char *ptr; and what i want to do is... (3 Replies)
Discussion started by: wolwy_pete
3 Replies
Login or Register to Ask a Question