niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Sed question (insert newline at specific points)
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Jim Cornwall
*nix forums beginner


Joined: 20 Jul 2005
Posts: 5

PostPosted: Thu Jul 20, 2006 2:48 pm    Post subject: Sed question (insert newline at specific points) Reply with quote

Hi all. I have a file which I would like to make more human-readable
since I have to use it for a database review. I would like to insert a
blank line in front of every line int he file that starts with a "Z".

Here's a severely trimmed-down sample of input, command, and results:

The input file (approx 26,000 lines, each "Z card" indicates a new
station's records, H / N / Y records are site info, followed by 1 to
many data records):

{jcorn} readdnelnc: % more peak
Z05010000 USGS
H05010000 4859501134050003030035SW1001000174.80 4500

N05010000 Belly River at International Boundary
Y05010000
305010000 19480617 1810 5.52
305010000 19490528 972 4.22
Z05010500 USGS
H05010500 4859201134550003030035SW1001000110.10 5100

N05010500 North Fork Belly River at International Bndry
Y05010500
305010500 19480616 386 4.23
305010500 19490528 146 3.22
Z05011500 USGS
H05011500 4857201135400003030035SW1001000161.00 4200

N05011500 Waterton River nr International Bndry
Y05011500
305011500 19480523 2510 6.30
305011500 19490528 1890 5.61


Command issued (borrowed from another ongoing thread on inserting
newlines at a "*"):

{jcorn} readdnelnc: % sed -e 's/^Z/\nZ/g' < peak > peak.edited


The results:

{jcorn} readdnelnc: % more peak.edited
nZ05010000 USGS
H05010000 4859501134050003030035SW1001000174.80 4500

N05010000 Belly River at International Boundary
Y05010000
305010000 19480617 1810 5.52
305010000 19490528 972 4.22
nZ05010500 USGS
H05010500 4859201134550003030035SW1001000110.10 5100

N05010500 North Fork Belly River at International Bndry
Y05010500
305010500 19480616 386 4.23
305010500 19490528 146 3.22
nZ05011500 USGS
H05011500 4857201135400003030035SW1001000161.00 4200

N05011500 Waterton River nr International Bndry
Y05011500
305011500 19480523 2510 6.30
305011500 19490528 1890 5.61

As you can see, I got an 'n' inserted before the 'Z', not a newline.
What did I do wrong in the sed command?

Thanks in advance,

Jim Cornwall
(USGS Water Resources, speaking for myself not the US Gov)
Back to top
jue
*nix forums beginner


Joined: 05 Jul 2006
Posts: 4

PostPosted: Thu Jul 20, 2006 3:17 pm    Post subject: Re: Sed question (insert newline at specific points) Reply with quote

Jim Cornwall schrieb:
Quote:
Hi all. I have a file which I would like to make more human-readable
since I have to use it for a database review. I would like to insert a
blank line in front of every line int he file that starts with a "Z".

Here's a severely trimmed-down sample of input, command, and results:

The input file (approx 26,000 lines, each "Z card" indicates a new
station's records, H / N / Y records are site info, followed by 1 to
many data records):

....

{jcorn} readdnelnc: % sed -e 's/^Z/\nZ/g' < peak > peak.edited

....
As you can see, I got an 'n' inserted before the 'Z', not a newline.
What did I do wrong in the sed command?


no solution for sed, but:

perl -pe 's/^Z/\nZ/g;' < peak > peak.edited

Thomas

ps.
I am sure there is a solution for your sed-snipped, but:
perl will give you much more power to extract the interesting data...
but that's not for comp.unix.shell
Back to top
Radoulov, Dimitre
*nix forums beginner


Joined: 14 Dec 2005
Posts: 26

PostPosted: Thu Jul 20, 2006 3:27 pm    Post subject: Re: Sed question (insert newline at specific points) Reply with quote

Quote:
Hi all. I have a file which I would like to make more human-readable
since I have to use it for a database review. I would like to insert a
blank line in front of every line int he file that starts with a "Z".
[...]



$ sed 's/Z/\
Quote:
Z/g' file


Dimitre
Back to top
Jim Cornwall
*nix forums beginner


Joined: 20 Jul 2005
Posts: 5

PostPosted: Thu Jul 20, 2006 4:09 pm    Post subject: Re: Sed question (insert newline at specific points) Reply with quote

Thomas J. wrote:
Quote:
Jim Cornwall schrieb:
Hi all. I have a file which I would like to make more human-readable
since I have to use it for a database review. I would like to insert a
blank line in front of every line int he file that starts with a "Z".

Here's a severely trimmed-down sample of input, command, and results:

The input file (approx 26,000 lines, each "Z card" indicates a new
station's records, H / N / Y records are site info, followed by 1 to
many data records):

...
{jcorn} readdnelnc: % sed -e 's/^Z/\nZ/g' < peak > peak.edited

...
As you can see, I got an 'n' inserted before the 'Z', not a newline.
What did I do wrong in the sed command?


no solution for sed, but:

perl -pe 's/^Z/\nZ/g;' < peak > peak.edited

Thomas

ps.
I am sure there is a solution for your sed-snipped, but:
perl will give you much more power to extract the interesting data...
but that's not for comp.unix.shell


Thanks, that did the trick. No need to extract anything else, I was
just looking to separate site records. I now have what I needed...


Jim
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Mon Dec 01, 2008 10:03 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Postfix relay to specific domain and to everybody else golgotha Postfix 0 Thu Oct 23, 2008 11:11 pm
No new posts Insert header and footer in every page served by proxy ehmedk Apache 0 Tue Apr 22, 2008 6:27 pm
No new posts Insert header and footer in every page served by squid ehmedk Squid 0 Tue Apr 22, 2008 6:16 pm
No new posts Relay for specific recipients jvernice Postfix 0 Mon Sep 17, 2007 6:21 pm
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm

Mobile Phone | Online Advertising | Loans | Advertising | Project cars for sale
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2733s ][ Queries: 16 (0.1842s) ][ GZIP on - Debug on ]