It is currently Tue, 07 Dec 2021 02:35:54 GMT



 
Author Message
 sed: splitting word over several lines
System: Red Hat 5.2
Shell:     bash 1.14.7(1)
Prog:     sed 3.02      

A file, datefile1, contains a single line, a date in the form :dd/mm/yyyy.
Using the hexdump utility it is: 0000000     1   2    /   1  2   /  1  9  9
 9   \n

I am attempting to use a sed command sequence to replace each '/' character
with the newline character so that each date component is placed on a
seperate line.

I am using the following sed command sequence to achieve this:

sed 's/\(..\)\//\1^N/g' datefile1 > datefile2

Note: The '^N' character was obtained via the Ctrl-V Ctrl-N key
combination.

Instead of obtaining, as expected, an output of:
0000000     1   2    \n   1  2   \n  1  9  9  9   \n

I get:
0000000     1   2    016   1  2   016  1  9  9  9   \n

and the date components remain on the same line.

Can anyone see what the problem is and suggest a solution ?

Anthony Borla



 Mon, 27 Aug 2001 03:00:00 GMT   
 sed: splitting word over several lines

Being that I am in the evil empire's software, I cannot test but I will have a
go anyway.

sed 's:\\:\r:g'



 Mon, 27 Aug 2001 03:00:00 GMT   
 sed: splitting word over several lines

OK this will work:

$ sed 's:/:\

At least it worked for me.



 Tue, 28 Aug 2001 03:00:00 GMT   
 sed: splitting word over several lines
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

    Anthony> System: Red Hat 5.2 Shell: bash 1.14.7(1) Prog: sed 3.02

    Anthony> A file, datefile1, contains a single line, a date in the
    Anthony> form :dd/mm/yyyy.  Using the hexdump utility it is:
    Anthony> 0000000 1 2 / 1 2 / 1 9 9 9 \n

    Anthony> I am attempting to use a sed command sequence to replace
    Anthony> each '/' character with the newline character so that
    Anthony> each date component is placed on a seperate line.

I don't believe you can do this, even with GNU sed.  The only usage of
`\n' (aka ^M) in a substitution is in the regexp to be matched.  IOW,
you can match a newline and replace it with something else, but you
can't insert one in the way you are attempting.

AFAIK, you can't import a partial line into the pattern space.  There
might be a way to put a part of the line (the matched part) into the
hold space; and then there is a command for putting a newline into the
hold space.  I suppose in theory you could delete the contents of the
hold space & then add the newline and print it.  Incredibly
complicated, it seems to me.

You'd probably be better off using awk.  But if you want to pursue
sed, I'd suggest posting to comp.unix.shell, where Al Aab (moderator
of the sed mailing list) and Ken Pizzini (maintainer of GNU sed) hang
out.

mp

- --
Michael Powe                                          Portland, Oregon USA
           mich...@trollope.org    http://www.trollope.org
  "Three hours a day will produce as much as a man ought to write."
                         -- Anthony Trollope

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v0.9.0 (GNU/Linux)
Comment: Encrypted with Mailcrypt 3.5.1 and GNU Privacy Guard

iD8DBQE27M8Y755rgEMD+T8RAiuuAKC9WofJLmIo5ZryDGmhit4dssT1QQCdEQ4F
rLFHXwoXt9QnyEEajW+ohLE=
=oTrv
-----END PGP SIGNATURE-----



 Fri, 31 Aug 2001 03:00:00 GMT   
 sed: splitting word over several lines
Michael Powe <michael+g...@trollope.org> scribed to us that -

<snip>

<snip>

The man page for the version of SED that I have (it's fairly recent, I
downloaded it around a week ago) states -

"This version of Sed supports a \<newline> sequence in ... the
replacement part of a substitute (s) sommand..."

The following little script does what was intended, I believe

#!/bin/bash
sed 's+/+\
+g' <datefile >results

Where datefile is the original file containing a date like 11/12/1999
and results is the output which will look like

11
12
1999

It does work, as I've just tested it myself, but it may involve
upgrading the version of Sed...
--
Mist.



 Fri, 31 Aug 2001 03:00:00 GMT   
 
   [ 5 post ] 

Similar Threads

1. word splitting splits words wrongly?

2. Line splitting with word-wrap.

3. : split lines in long 'sed'-pattern ?

4. how to split lines using sed et altera

5. use sed to split tokens into lines

6. Can sed split lines?

7. Replace several lines by a line

8. Line splitting long lines

9. Read line at at time and split up that line

10. How to split a single line into multiple lines of 10 characters each


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software