Capture string contained on a line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture string contained on a line?
# 1  
Old 06-28-2013
Capture string contained on a line?

Hello All,

I'm working on a script that runs the wget command on a list of IP Address in order to capture the data at that address' index.html.
That part works fine to get the HTML code at that address but the data I'm trying to pull out is on a line containing a BUNCH of
code for an HTML Table.

The line with the Table code contains about 2000 characters, give or take a few, and I'm trying to pull out the Serial Number I find
in that table. Each of the Serial Numbers is located/formatted like so:
Code:
<TABLE BORDER="0"........more table code.......more...more...more....<B> Serial Number</B></TD><td width=20></TD><TD><B>FCH*********</B>....more...code.....

So if you look at the end of the line with the TABLE code you'll see a string beginning with "FCH". Basically all the Serial Numbers
begin with "FCH" so I'm trying to capture starting from "FCH" until the end of the string or I guess something like:
"From 'FCH' till the first occurrence of '<' or '</B>' ".

I figure this is possible using grep, sed, and awk, but I wasn't sure which one would be best to use in this situation?

Could someone tell me what would be the best way to capture a string starting from one substring (FCH) till another substring (</B>).

Any thoughs or suggestions would be greatly appreciated!


Thanks in Advance,
Matt
# 2  
Old 06-28-2013
awk has the useful property of using whatever you want as the "line feed", not necessarily \n.

Code:
awk -v RS="<" -F">" '/FCH/ { print $2 }' filename

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-28-2013
Hey Corona688, thanks for the quick reply!

Outstanding...!! Works like a charm.

So how exactly is that doing it? I know the 'RS' is the record separator and '-F' is for the Field Separator...

Is it basically saying, Split each record on "<" then split each Field on ">", then find "FCH" and print the second field?


Thanks Again,
Matt
# 4  
Old 06-28-2013
That's exactly what it's doing, yes. For every 'line', check if FCH is in it, and if so, print.

Given this text:

Code:
<html><body><h1>HI!</h1></body></html>

it will interpret it like this:

Code:
html
body
h1      HI!
/h1
/body
/html

Not a bad way to make a rough-cut XML/HTML/whatever parser, though far from compliant, and some awks have an annoying 2000-byte "line" size limit.

Last edited by Corona688; 06-28-2013 at 02:30 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-28-2013
Hey Corona, ok cool makes sense...

Yea this is just a temporary script to use wget on some Cisco IP Phones, given a list of IP Addresses to get their Serial Numbers.

Already finished up the script and it worked like a charm!! Thanks again!

Just in case anyone is interested here's the script:
*Basic idea is to collect Serial Numbers from some Cisco IP Phones, when you have the list of the Phone's IP Addresses...
Code:
#!/bin/bash


# Pass the File containing the IP Addresses to this script as the only CLI Arg:
IP_AddressFile="$1"

# Set the IFS to the newline to capture each IP:
OLD_IFS=$IFS
IFS='
'

# Read in the IP Address (*one line at a time) and save them to this Array:
addresses=( $(cat $IP_AddressFile) )

# Increment/Counter Var:
#x=0

# Loop through Array of Addresses and run the wget command on each one:
for (( x=0; x<${#addresses[@]}; x++))
 do
    echo "${addresses[$x]}"

    # Run wget and print output to stdout and ignore the stderr from wget:
    tmp_SN=$(wget -O - ${addresses[$x]} 2>/dev/null | grep -i 'Serial Number' | awk -v RS="<" -F ">" '/FCH/ {print $2}')

    echo -ne " $x. ${tmp_SN}'" | awk {'print $2'}
    echo -ne "\n"
done

IFS=$OLD_IFS

Thanks again Corona for the help and explanation!


Thanks,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk:String search more than one time and capture OP

Dear All During one of mine script developemnt i am stuch at one sub part. Requiremnt is as below kindly help me. IP file: 2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001X (unavailable) 2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001Y (unavailable) 2015-02-28 10:10:15 AL M... (6 Replies)
Discussion started by: jaydeep_sadaria
6 Replies

2. Shell Programming and Scripting

Script to capture string in a log file

Dear all, I have a log file to be analysed. this log file contains vaiours lines of code starting with date timestamp. if my search string is exception then that resepective log statement starting from the date is required. example: 2014/10/01 16:14:44.459|>=|E|X|19202496|2832|... (5 Replies)
Discussion started by: shravee
5 Replies

3. Shell Programming and Scripting

How to capture string below a line?

Hi, I need to collect IP address of all servers (more than 300). One way is put it in for loop and run nslookup for that servers list, but there are multiple fields in output. (tty/dev/pts/13): bash: 1011 > nslookup vplssor04 Server: 10.58.115.34 Address: 10.58.115.34#53 Name: ... (7 Replies)
Discussion started by: solaris_1977
7 Replies

4. Shell Programming and Scripting

String capture from ip file

Dear All From below mention input file I want op file as mention. Kindly help. IP file: "BSCGNR4_IPA17_C" 329 140119 0717 RXOCF-105 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Needed OP: 140119 0717 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Note that string mark in red as variable in... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

5. Shell Programming and Scripting

awk help string capture

Dear All My input file as under. From it I want op fine as mention below. Kindly help. I use below code but not help. code: awk -F" " '{print $2}' ip file: "BSCJNGR_IPA17_C" 030 131207 1305 RXOCF-353 PBD011_BGIL BOTH AC FAULTY "BSCJNGR_IPA17_C" 991 131207 1637 RXOCF-224 NAV001_BGIL ... (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

6. Shell Programming and Scripting

How to capture a string enclose by a pattern within a file?

Hi all, My file :test.txt just like this: ........................... From: 333:123<sip:88888888888@bbbb.com To: <sip:123456@aaaaa.com ......................... I want a script to capture the string between sip: & @ Expect output: 88888888888 123456 Please help! (4 Replies)
Discussion started by: Alex Li
4 Replies

7. UNIX for Advanced & Expert Users

capture data from matched string/line

Hi, I have a query as follows : suppose I am matching a string in a file say "start from here" and I want to pick up 'n' number of lines () from the matched string. Is there any way to do that ? 1) going forward I want to do this for every match for the above string 2) or limit this to... (2 Replies)
Discussion started by: sumoka
2 Replies

8. Shell Programming and Scripting

Need to capture certain text from a string in a different file

Hi, I wanted to know how i could accomplish this in a script using ksh. Lets say there is a file called test.dat and it has a certain input like below : . . Hi = 56 Hi = 67 . . 1 record(s) selected Now i need to capture the numbers after the = sign and store them in a... (3 Replies)
Discussion started by: Siddarth
3 Replies

9. UNIX for Dummies Questions & Answers

Modifying a particulae data in a line contained in File

Hi, I have a file containing data: systemname/userid/password/comment systemname1/userid1/password1/comment1 systemname2/userid2/password2/comment2 I want to modify the "password" using script. Can anybody help me with this problem?:confused: (2 Replies)
Discussion started by: pgarg1989
2 Replies

10. Shell Programming and Scripting

Find a string under a directory that is contained in another file

Hi I am loking for some help in writing a script that will take a number that is located in one file and search a folder structure for that string in any file under that directory. I can do this manually with : find /"directory" -type f -exec grep -l 'Number String' {} \; But now I will... (3 Replies)
Discussion started by: BMC
3 Replies
Login or Register to Ask a Question

Featured Tech Videos