print variable length text from the middle of a line?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers print variable length text from the middle of a line?
# 1  
Old 02-05-2012
print variable length text from the middle of a line?

I am trying to get text from a webpage, in terminal form. So far I am:

1. getting the html for the page printed using curl (curl -s webpage.com), which is then

2. piped to awk, printing line number 29 (awk NR==29), then

3. this is where I am sort of lost. I know where in the printed line I want to start (character 686), but the text I want to print can vary quite a bit in length.

Here is part of the output:

<span style="font-size:14px; color:#347C17">THIS IS THE SECTION I WANT PRINTED</span>

In that example, starting at character 686 (of the full output) would be the capital T. And like I said, that "section" before the </span> can be different lengths.

I believe it is the only set of <span> tags on the line, if that helps at all.

Code:
curl -s webpage.com | awk NR==29 | wtf goes here?


[edit] got it to work, but it looks pretty terrible


Code:
curl -s website.com | awk NR==29 | cut -c 686-750 | tr '\n' '|' | sed "s/\(.*\)<\/span.*/\1/" | tr '|' '\n'


Last edited by darkfalz; 02-05-2012 at 11:30 AM..
# 2  
Old 02-05-2012
If it is the only set of <span> tags on the line, then does this work:

Code:
curl -s website.com | awk 'NR==29 { gsub( "</span>.*", "" ); gsub( ".*>", "" ); print }'

# 3  
Old 02-05-2012
Quote:
Originally Posted by agama
If it is the only set of <span> tags on the line, then does this work:

Code:
curl -s website.com | awk 'NR==29 { gsub( "</span>.*", "" ); gsub( ".*>", "" ); print }'

Hm...tried this, but it didn't output anything.
# 4  
Old 02-05-2012
Try:
Code:
curl -s website.com | awk 'NR==29{len=match ($0,"</")-686;print substr($0,686,len)}'

# 5  
Old 02-05-2012
Quote:
Originally Posted by bartus11
Try:
Code:
curl -s website.com | awk 'NR==29{len=match ($0,"</")-686;print substr($0,686,len)}'

It gives me

Code:
awk: can't open file {len=match ($0,"</")-686;print substr($0,686,len)}
 source line number 1

# 6  
Old 02-05-2012
What version of awk are you running, and under which O/S? If you are on a Solaris box you might want to use nawk or /usr/xpg4/bin/awk (I think that's the path -- I don't have a sun box at hand to check this morning).
# 7  
Old 02-05-2012
Quote:
Originally Posted by agama
What version of awk are you running, and under which O/S? If you are on a Solaris box you might want to use nawk or /usr/xpg4/bin/awk (I think that's the path -- I don't have a sun box at hand to check this morning).
Not a Sun box, just my MacBook Pro running 10.7

awk version 20070501
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

4. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

5. Shell Programming and Scripting

Inserting a line when its length is variable

Hi Unix experts I have simple text files in which the number of lines vary from one file to another. They look like the following: # # . . 34 46 76 72 39 68 I want to grab the first number of the last line of each file (let's say A= 39 in the above example), which is... (2 Replies)
Discussion started by: nxp
2 Replies

6. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

7. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies

8. Shell Programming and Scripting

Script to add a single line to middle of text file.

I've got a configuration file that is filled with xml text statements for example: <...../> <...../> <...../> <data id="java-options" value="-server -Djava.security.policy..../> <...../> <...../> <...../> I want to write a korn shell script that will go to this specific line and add a... (2 Replies)
Discussion started by: progkcp
2 Replies

9. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question