It is currently Tue, 07 Dec 2021 01:46:06 GMT



 
Author Message
 Need to add a \n carriage return after every occurance of a char
Hi All,

Need to add a carriage (\n) return after every occurance of "N:"

my file looks like this:

N:\edcc\rel\f66543|2350||1|1|0| | |N:\edcc\rel\f66544|2350||1|1|0|
||N:\edcc\rel\f66545|2352||2|SH1|1|0| | |N:\edcc...

this whole thing's a single line, size of file = 2megs.
Desired result:
N:\edcc\rel\f66543|2350||1|1|0| | |
N:\edcc\rel\f66544|2350||1|1|0| ||
N:\edcc\rel\f66545|2352||2|SH1|1|0| | |
N:\edcc\....

I wrote a C program for this that served the purpose, but not happy
with that. Felt sed or awk script will be more faster and efficient.
any help is appreciated.

Thanks in adv.,

NKris



 Wed, 13 Oct 2004 00:37:56 GMT   
 Need to add a \n carriage return after every occurance of a char

    From your example, I think you mean _before_ every instance...

    A C program will be much more efficient than sed or awk, and is
    the easiest way to do it when you have a file containing very long
    lines. It will be a much smaller executable that sed or awk, and
    it will not have to interpret a script.

    Most utilities will {*filter*}on very long lines, although if you have
    the GNU versions, you may be OK:

        sed 's/N:/\
        N:/g'

--
        Chris F.A. Johnson                              bq...@torfree.net
        =================================================================
        c.f.a.john...@rogers.com                 http://www.**-**.com/
        c...@freeshell.org         http://www.**-**.com/



 Wed, 13 Oct 2004 01:49:24 GMT   
 Need to add a \n carriage return after every occurance of a char
On 26 Apr 2002 09:37:56 -0700, meetnk...@rediffmail.com (NKris) wrote:

Hi,

You can change all the 'N' characters to newlines and then
process the resulting file, first adding the deleted 'N's
and then appending lines that don't start with 'N:' so
they get lumped back together.

The single line you want to process should end with a newline,
if it doesn't one should be added!

tr N '\12' < FILENAME |
  sed -n '
    1{/^$/d;}                 # first blank line ignored
    1!s/^/N/                  # get the 'N' back
    /^N:/{x                   # on N: line, put into buffer
          s/\n//gp            # print buffer previous line
          s/.*//g             # prefix with \n
          G
          h
          s/.//               # line is N: so next bit missed
          }
    /^N:/!H                   # non N: line, append to buffer
    ${g;s/\n//gp;}            # last line, flush buffer
    '

byefrom

--
laura fairhead  # la...@madonnaweb.com  http://lf.8k.com
                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492



 Wed, 13 Oct 2004 06:27:33 GMT   
 Need to add a \n carriage return after every occurance of a char
"Chris F.A. Johnson" <c.f.a.john...@rogers.com> wrote:

Perl doesn't {*filter*}on long lines, if you have the memory, so one
may use:

    perl -wpe 's/N:/\nN:/g' FILE

but if the file is too big to fit into memory, one may use "N:" as
the input record separator:

    perl -wpe 'BEGIN { $/ = "N:" } s/N:/\nN:/' FILE

Peter

--
Where do bit streams end?  In bit rivers?



 Wed, 13 Oct 2004 16:50:02 GMT   
 Need to add a \n carriage return after every occurance of a char

A couple more options.  :-)

perl -pe'y/\n//d;s/(?=N:)/\n/g' FILE

perl -FN: -nae'chomp@F;print join"\nN:",@F' FILE

John
--
use Perl;
program
fulfillment



 Wed, 13 Oct 2004 20:57:39 GMT   
 Need to add a \n carriage return after every occurance of a char
"John W. Krahn" <kra...@acm.org> wrote:

Why the chomp?  It only removes $/ which is still "\n".  But of
course, there might be a trailing "\n" on that one long input
line, but I don't think removing it was a requirement.

Just putting the join string into $, (which ideally should be done
in a BEGIN block):

    perl -FN: -nae'$,="\nN:";print@F' FILE

Peter

--
Where do bit streams end?  In bit rivers?



 Wed, 13 Oct 2004 21:42:44 GMT   
 Need to add a \n carriage return after every occurance of a char
Hi All,

Thanks for the help, I appreciate it.

the script "perl -wpe 's/N:/\nN:/g' FILE" worked just fine for me.

Kris

pjack...@online.no (Peter J. Acklam) wrote in message <news:4rhxv80m.fsf@online.no>...



 Mon, 18 Oct 2004 07:05:22 GMT   
 
   [ 7 post ] 

Similar Threads

1. carriage return char for HP 520???

2. Adding a carriage return.

3. How do I add carriage returns for printing to Deskjet

4. Help! Adding a carriage return

5. ADDING carriage return (Unix to DOS)

6. Adding a carriage return to a script

7. Need to Strip carriage returns from text file

8. Need To Insert Carriage Return

9. Need help reading single character without carriage return.

10. Adding return char and newline at end of file


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