SED/AWK question

SED/AWK question

Post by Peter J. Kokta » Wed, 10 Aug 1994 03:04:55



I have a text file that is ~1 meg of data with no carrage returns
in it.  I want to insert a return every N characters.  How
do I do this?
 
 
 

SED/AWK question

Post by David Y » Wed, 10 Aug 1994 16:10:32




:)I have a text file that is ~1 meg of data with no carrage returns
:)in it.  I want to insert a return every N characters.  How
:)do I do this?
:)
:)

{
 for ( i = 1; i <= length($0) ; i+=N )
     print substr( $0, i, N )

Quote:}

The above awk code should do what you want...
Correct me if I am wrong.

Regards,
David

 
 
 

SED/AWK question

Post by Peter J. Kokta » Thu, 11 Aug 1994 04:02:17



Quote:>I have a text file that is ~1 meg of data with no carrage returns
>in it.  I want to insert a return every N characters.  How
>do I do this?

I do not think that the posted solution will work in your problem as
your file has no line breaks. In fact, unless you can define a
consistent line separation character, I do not believe that you will be
able to use sed or awk to handle your problem due to a finite line
length limit.

Your solution would probably involve perl or C. Since I do not grok perl
that well, I submit a C solution that _should_ work. Very quick and
dirty.

------------------------------------------------------------------------
#include <stdio.h>

int main(int arg, char *argv[])

{

#define LENGTH 72   /* Adjust this number for your purpose */

  int current, /* Current input character */
      inc;     /* Loop counter */

  do {

    for (inc = 0; inc < LENGTH; inc++) {
      if ((current = getchar()) == EOF)
        break;

      putchar(current);
    }

    putchar('\n');
  } while (!feof(stdin));

  return 0;

Quote:}

------------------------------------------------------------------------

     Scott Kajihara
--
Scott Akira Kajihara
... when you are really strange, you go into physics

 
 
 

SED/AWK question

Post by Peter J. Kokta » Thu, 11 Aug 1994 04:04:17


So is there a way around the array length problem in sed or awk?

I might not have access to a c compiler.

-pjk

 
 
 

SED/AWK question

Post by David Y » Thu, 11 Aug 1994 07:58:03




:)

:)>I have a text file that is ~1 meg of data with no carrage returns
:)>in it.  I want to insert a return every N characters.  How
:)>do I do this?
:)>
:)>
:)
:)I do not think that the posted solution will work in your problem as
:)your file has no line breaks. In fact, unless you can define a
:)consistent line separation character, I do not believe that you will be
:)able to use sed or awk to handle your problem due to a finite line
:)length limit.

Oh, sorry that I didn't see the 1 meg of data that you mentioned.  Though
there's probably a variable of line length limit in different versions of
awk, I don't think any of them can handle 1,000,000 characters in a line
either.

Here's another try in C:

#include <stdio.h>
#define N /*whatever your N is*/
main(){
  int c, i=0;
  while ((c=getchar())!=EOF) {
    putchar(c);
    if (++i == N) {
      i = 0; putchar('\n');
    }

Quote:}

call it with "a.out < inputfile > outputfile"

Again, correct me if I'm wrong.

David

 
 
 

SED/AWK question

Post by Dan Merc » Fri, 12 Aug 1994 06:43:55



: I have a text file that is ~1 meg of data with no carrage returns
: in it.  I want to insert a return every N characters.  How
: do I do this?

A simple chore for dd,  particularly if you use it in a non-obvious
way:  simply convert your data to ebcdic and back again:

dd if=<infile> conv=ebcdic|\
dd cbs=<blocking factor> of=<ofile> conv=ascii,notrunc

One warning: don't try this in an hpterm at HP-UX 9.01.  dd acts very
bizarrely under those circumstances (for one thing,  it picks up stuff
from the hpterm buffer).  Apparently there's a fix for hpterm at a
later release.  Big question - why is hpterm interfacing with dd at
all?

--
Dan Mercer                                            Applications + Plus

======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients,  in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied.  Follow at your own risk.
Objects in the mirror are closer than they appear.

 
 
 

SED/AWK question

Post by Hemant Sh » Thu, 11 Aug 1994 23:56:59



->So is there a way around the array length problem in sed or awk?

->I might not have access to a c compiler.

->-pjk

Whoops, I forgot to change one parameter in my previous script, it will
add newline character every 3 characters not 80 characters, herre the one
that will insert newline every 80 characters. Again I have not tested this
on a line that is 1MB long.

#!/bin/ksh

typeset -L1 OneChar
typeset -i Counter
typeset -i Length
typeset -i MaxChar

while read Line
do
   Length=${#Line}
   Counter=0
   MaxChar=0
   while (( Counter < Length ));
   do
      OneChar=$Line
      Line=${Line#?}
      print -n "$OneChar"
      (( Counter = Counter + 1 ))
      (( MaxChar = MaxChar + 1 ))
      if (( MaxChar == 80 ));
      then
        print ""
        MaxChar=0
      fi
   done
   print ""
done

--
                                                             \\|//
                                                             (O-O)
---------------------------------------------------------oOO--(_)--OOo-----
Hemant Shah                      |  I haven't lost my mind,
LIDP, Inc.                       |  it's backed up on tape somewhere.
Voice: (708) 960 0133 Ext: 64    |

--------------------------------------------------------------------------

 
 
 

SED/AWK question

Post by Hemant Sh » Thu, 11 Aug 1994 23:54:31



->So is there a way around the array length problem in sed or awk?

->I might not have access to a c compiler.

->-pjk

If you have access to Korn Shell, here is the Koen Shell script that will
insert newline character after every 80 characters.

I have not tested on a line that is 1MB long. Hope it works on such big line.

#!/bin/ksh

typeset -L1 OneChar
typeset -i Counter
typeset -i Length
typeset -i MaxChar
typeset Line

while read Line
do
   Length=${#Line}
   Counter=0
   MaxChar=0
   while (( Counter < Length ));
   do
      OneChar=$Line  
      Line=${Line#?}
      print -n "$OneChar"
      (( Counter = Counter + 1 ))
      (( MaxChar = MaxChar + 1 ))
      if (( MaxChar == 3 ));
      then
        print ""
        MaxChar=0
      fi
   done
   print ""
done

--
                                                             \\|//
                                                             (O-O)
---------------------------------------------------------oOO--(_)--OOo-----
Hemant Shah                      |  I haven't lost my mind,
LIDP, Inc.                       |  it's backed up on tape somewhere.
Voice: (708) 960 0133 Ext: 64    |

--------------------------------------------------------------------------

 
 
 

1. sed/awk question

Hi,

I'm trying to do some text editting with sed/awk where I need to remove everything between
and including some comment delimiters.  The delimiters are "--[" for the beginning of the comment
and "--]" for the end of the comment.  These delimiters can appear anywhere on a line and
can span multiple lines (no upper limit).  If "--[" began in the middle of a line, the text that precedes
it must be preserved (including the spacing).  Same goes for "--]".  The problems I 'm having with sed/awk are as follows:

1) sed - It seems like sed is more of a line editor and will give you trouble when the begin comment
delimiter starts in the middle of the line.  Hence, the command:

% sed -e '/--\[/, /--\]/d' *.a

would remove everything from the line where "--[" was found even if "--[" started in the middle
of the line.  All the text that preceded "--[" on that line would also be deleted.  Same problem for
the end comment delimiter too.

2) awk - I could parse up each line into fields for more control over the contents of each line, but
I would have a difficult time in preserving the spaces between the words.

Any ideas?  I don't know how to use perl, so hopefully a solution can be found in sed or awk.

Thanks,
Ben

 ------------------------------------------------------------------------------

 Motorola GEG Communications Division        Phone   : (602) 441-8268
 8201 E. McDowell Road H8120
 Scottsdale, AZ 85252
 -----------------------------------------------------------------------------

2. sendmail & DNS

3. shell - sed - awk question

4. LEX = flex

5. sed/awk question

6. Some old fashoned Linux advocacy

7. sed /awk question

8. bc and converting hex to decimal

9. Basic shell/sed/awk question - float truncation and comparison

10. simple (not so?) sed/awk question

11. Sed / awk question

12. sed , awk question??

13. sed/awk question : delete first blank in every line