sed: print out to first blank line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: print out to first blank line
# 1  
Old 12-15-2008
sed: print out to first blank line

I am trying to use wget and sed to extract a text based weather forecast for the Greater Victoria area only, this is one paragraph of many in a web page. I have tried /^$/ but that does not seem to work. So far I get mostly what I want with this:

wget -qO - http://www.weatheroffice.gc.ca/forecast/textforecast_e
.html?Bulletin=fpcn11.cwvr | sed -n "/Greater Victoria/,/Fraser/p
"

but that stops one line ahead of what I want by using /Fraser/ . How can I change the sed portion of above to stop at what appears to be a blank line in the text?

Larry
# 2  
Old 12-15-2008
Quote:
Originally Posted by lagagnon
How can I change the sed portion of above to stop at what appears to be a blank line in the text?
I remember searching for a solution to a similar problem some years ago. When i finally found the answer it stunned me because of its simplicity. The trick is to simply quit sed when encountering the line on which you want to stop. Consider the following file:

Code:
# some text
# some text
# some text

# some other text
# some other text

If you want to stop at the blank line (for the purpose of the example we suppose it is completely empty) issue the following sed script:

Code:
sed '/^$/q' infile > outfile

Btw: my problem was to extract "headers" from script files. Usually a script has one or several lines at the beginning that start with a octothorpe ("#"). After these come lines with normal script code and then maybe the one or other commentary line starting with an octothorpe again. I wanted to extract only the first "header" lines, not any commentary after the first noncommentary line. My solution was analogous.

I hope this helps.

bakunin
# 3  
Old 12-16-2008
Hi,

/1$/ won't do it as your text contains windows line-feeds (^M).
Try:

Code:
sed -n "/^Greater/,/^Fraser/{/^^M/{q};p}" F

To type a windows line-feed, press ctrl-v followd by ctrl-m.
Sed will stop printing when it encounters the first line starting with
a windows line-feed.

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print blank line between files

I have an AWK command that goes like this: awk '/from BOITE_AUX_LETTRES/,/end-procedure|END-PROCEDURE|end-select|END-SELECT/ {print FILENAME,FNR,$0}' *prp I get my results as follows: ZKELEXTV2.prp 16 from BOITE_AUX_LETTRES(#num_lettre) ZKELEXTV2.prp 23 LET $CONDEXTR = ' where... (2 Replies)
Discussion started by: mvalonso
2 Replies

2. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. Shell Programming and Scripting

awk until blank space and print next line

Hello and Happy New Year 2012! I have this example: 1,2,3 4,5,6 7,8,9 For that, I'm trying to get: 1,2,3 4,5,6 7,8,9 for that, I think this might work but doesnt work so far: awk '{for(i=1;i=NF;i++);sub(/\//,"",$i);print $i}' myfile (2 Replies)
Discussion started by: Gery
2 Replies

5. Shell Programming and Scripting

Print lines between a regExp & a blank line

Hi, I have a file, say files_list, as below (o/p of ls -R cmd) $ cat files_list /remote/dir/path/to/file: sub-dir1 sub-dir2 sub-dir3 ... /remote/dir/path/to/file/sub-dir1: remote_file1.csv.tgz <blank line 1> /remote/dir/path/to/file/sub-dir2: remote_file2.csv.tgz <blank... (3 Replies)
Discussion started by: dips_ag
3 Replies

6. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

8. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies

9. Shell Programming and Scripting

Remove blank line - SED

HI, I have this list of apps like so: DivX Products.app DivX Support.app Uninstall DivX for Mac.app Build Applet.app SpringBoard.app Interface.app MobileAddressBook.app MobileSafari.app MobileSlideShow.app Preferences.app Install Flash Player 8 OSX.app Yap.app check_afp.app ... (10 Replies)
Discussion started by: pcwiz
10 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question