Running sed from a script query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running sed from a script query
# 1  
Old 07-25-2013
Running sed from a script query

Hello!

I'm trying to run this code to print the body of an html document (all text in between <body> and </body>) from a script but am unsure how to call it from the command line interface.

Code:
/<body>/,/<\/body>/
1s/.*<body>//
$s/<\/body>.*//p

I have tried to call it using this:

Code:
sed -n -f sedscript1.sed test.txt

text.txt being where the html text is stored.
I get the error message:
Code:
sed: file sedscript1.sed line 2: unknown command: `
'

when trying to run it though :/

What am I doing wrong!?

Thanks for your help!
# 2  
Old 07-25-2013
1) Your first line is missing a command to do. 'for all lines between <body> and </body>" -- do something, but "something" is missing. /<body>/,/<\/body>/ p is more complete, p to print.

2) It probably won't work. It will match all lines between <body> and </body>, including parts of the line before and after these tags. If the HTML is one giant line it will print everything.

You can do similar things in awk, but you get to tell it what a 'line' is, which is useful for matching one tag per 'line'.

This will match tags more properly, and also split each tag onto a line:

Code:
awk -v RS="<" '/^[bB][oO][dD][yY]/,/^\/[bB][oO][dD][yY]/ { $1="<"$1 ; print }' file.html

# 3  
Old 07-25-2013
Thanks for the help! But when I use it like this (from the command line):

Code:
sed -n '/<body>/,/<\/body>/p' test.txt | sed -e '1s/.*<body>//' -e '$s/<\/body>.*//'

it will take the input:

Code:
<!DOCTYPE html><html lang="en">
<head><title>Images</title></head><body><ul>
<li><a href="IMG_1389.JPG">IMG_1389.JPG<\a> (1.7M)<\li>
<li><a href="IMG_1390.JPG">IMG_1390.JPG<\a> (1.5M)<\li>
<li><a href="IMG_1391.JPG">IMG_1391.JPG<\a> (1.4M)<\li>
</ul></body></html>

and output exactly what I need:

Code:
<ul>
<li><a href="IMG_1389.JPG">IMG_1389.JPG<\a> (1.7M)<\li>
<li><a href="IMG_1390.JPG">IMG_1390.JPG<\a> (1.5M)<\li>
<li><a href="IMG_1391.JPG">IMG_1391.JPG<\a> (1.4M)<\li>
</ul>

But I need to be able to call it from a script...
# 4  
Old 07-25-2013
Well, you could paste that command into a script?
# 5  
Old 07-25-2013
Trouble is, it doesn't accept the commas and I thought each expression had to be written on a new line?
# 6  
Old 07-25-2013
No, I mean, the whole line you gave, into a script file. Otherwise you're going to need more than one file to feed all those sed | sed | sed.

Code:
awk '/^[uU][lL]/,/^\/[uU][lL]/ { $1="<"$1 ; print }; END { printf("\n"); }' RS="<" ORS="" FS="" OFS="" inputfile

# 7  
Old 07-25-2013
Try this in your sed file:
Code:
s/.*<body>//
s/<\/body>.*//
/<ul>/,/<\/ul>/p

Beware that if your HTML changes slightly, it will break down.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regarding a query on making changes to a running script

Hello All, Greetings !! I have a query here to all is as follows: Question: Let's say we are running a script in a UNIX box and we have opened an another session and then made changes in script of some statements NOT to be print some values(just an example) so when I am monitoring the... (5 Replies)
Discussion started by: RavinderSingh13
5 Replies

2. UNIX for Dummies Questions & Answers

Extract only the data from ksh script running netezza query

Hi I searched this forum before posting the question, but couldnt find it, the issue i'm facing is, i'm trying to select a column from a netezza table from a korn shell script, but the query runs var=$(nzodbcsql -q "select MAX(millcount) from table1";) echo $var it returns the value like... (10 Replies)
Discussion started by: maximus_jack
10 Replies

3. Shell Programming and Scripting

Error in running DB query by script

Hi I was trying to fetch data from database. But the number of rows exported were huge so i got the error. Experts please advice. Thanks a lot for your supprt. #: ./script.sh ./script.sh: xmalloc: subst.c:3076: cannot allocate 1401346369 bytes (0 bytes allocated) (2 Replies)
Discussion started by: brij123
2 Replies

4. UNIX for Dummies Questions & Answers

Running a SQL Query from UNIX

Hi, Could you please help me on this. I have bulk of queries written in text file. I want to use those queries and want to execute from UNIX. I don't want to run this file as a sql file as this file will change every week. I want to run it in my environment as bulk sql statement from text... (1 Reply)
Discussion started by: abhii
1 Replies

5. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

6. AIX

Query on running script with nohup

Hi, I'm trying to run database restore script with the nohup command as it will run for long hours since if I run it normally, the putty session will become inactive and the restore gets terminated. The command I use is nohup db2 -tvf FBR_NODE0000.scr -z FBR_NODE0000.log & But the problem is... (2 Replies)
Discussion started by: vkcool.17
2 Replies

7. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

8. Shell Programming and Scripting

Running script from script query

Hi I have a question Suppose i have three scripts & i have created a top level script to run these. eg. leaf1.csh leaf2.csh leaf3.csh top.csh Now in top.csh i have written #!/bin/csh leaf1.csh leaf2.csh leaf3.csh Now my question is when i run this top level script when it... (1 Reply)
Discussion started by: sarbjit
1 Replies

9. Shell Programming and Scripting

error ORA-06512 while running query in script

1 #!/bin/ksh 2 ################################################################ 3 # Written by Johnson 12/03/2008 4 # Version 1.0 5 # This script executes some SQL to provide Spike Check Report to TNS team. 6 ... (3 Replies)
Discussion started by: shivanete
3 Replies

10. UNIX for Dummies Questions & Answers

running sed inside script file

i am substituting some text in the xml file using sed, on shell directly it works fine, but when i run it inside script file, it say, the function cant be parsed, i think the prob is due to xml file, kindly help (4 Replies)
Discussion started by: bajaj111
4 Replies
Login or Register to Ask a Question