Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2003
    Posts
    3

    Question Insert a carriage return in a loooong record?

    Hello all.

    I have a file with several records (all of which are 94 characters in length). The problem is that there are no carriage returns/line feeds in the file...sooooo when you view the file it appears as one long record.

    Is there a way to insert a carriage return line feed behind every 94th character, then output it to a table/file as several, 94 character records? sed? awk?

    Thanks in advance,
    JW

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,457

    Re: Insert a carriage return in a loooong record?

    Originally posted by jewilson2
    Hello all.

    I have a file with several records (all of which are 94 characters in length). The problem is that there are no carriage returns/line feeds in the file...sooooo when you view the file it appears as one long record.

    Is there a way to insert a carriage return line feed behind every 94th character, then output it to a table/file as several, 94 character records? sed? awk?

    Thanks in advance,
    JW
    split -b 94 sourcefile "94." && cat 94.* > newfile
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    Nov 2003
    Posts
    3
    Thanks for the reply. I put this in a shell script and executed....it's output was the same as it's input. What am I missing?

    split -b 94 /s0b0/tmp/achtape "94." && cat 94.* > /s0b0/tmp/achtape.split

    Thanks again,
    JW

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by jewilson2
    Thanks for the reply. I put this in a shell script and executed....it's output was the same as it's input. What am I missing?

    split -b 94 /s0b0/tmp/achtape "94." && cat 94.* > /s0b0/tmp/achtape.split

    Thanks again,
    JW
    The above command is splitting the file into 94 byte (character) chunks, which are output to individual files and then concatanated together again into a newfile. You need to introdue a newline character to the end of each file.

    You could do this as below...

    split -b 94 yourFile "94." && for splitFile in 94.*
    do
    echo "$(cat $splitFile)"
    done > newFile

    ...or use an editor like sed...

    sed 's/.\{94\}/&\
    /g' yourFile > newFile

    Damian

  5. #5
    Join Date
    Nov 2003
    Posts
    3
    Thanks for the replies.

    The last split command worked great.

  6. #6
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,457
    Originally posted by jewilson2
    Thanks for the reply. I put this in a shell script and executed....it's output was the same as it's input. What am I missing?

    split -b 94 /s0b0/tmp/achtape "94." && cat 94.* > /s0b0/tmp/achtape.split

    Sorry, my bad... I should have tried it before posting - I would see my mistake. Mr. Ibbotson's solution is correct.
    ---
    "It does not work" is not a valid problem statement.

  7. #7
    Join Date
    Sep 2003
    Posts
    71
    Hi Damian,

    I am using given sed command for a 184byte fixed length file (without Newline)

    Its not producing file with newlines

    sed 's/.\{184\}/&\
    /g' jeffdavis > jdavis184

    am I misssing something?. also can you please explain replace portion
    in sed commad.

    Thanks,

  8. #8
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by skd

    I am using given sed command for a 184byte fixed length file (without Newline)

    Its not producing file with newlines

    sed 's/.\{184\}/&\
    /g' jeffdavis > jdavis184

    am I misssing something?. also can you please explain replace portion
    in sed commad.
    What is it producing?

    Make sure that the '\\' character at the end of the first line does not have any following characters (other than the obvious newline!).

    .\{184\} is matching 184 of the wildcard character '.' (i.e. any 184 character string). The & in the replace portion represents the previous matched pattern (i.e. the 184 character string).

    HTH

  9. #9
    Join Date
    Sep 2003
    Posts
    71
    Just creating 0 byte size file jdavis184.

    Following is octal dump of sed command

    0000000 s e d ' s / . \ { 1 8 4 \ } /
    163 145 144 040 047 163 057 056 134 173 061 070 064 134 175 057
    0000020 & \ \n / g ' j e f f d a v i s
    046 134 012 057 147 047 040 152 145 146 146 144 141 166 151 163
    0000040 > j e f f 1 8 4 \n
    040 076 040 152 145 146 146 061 070 064 012
    0000053

  10. #10
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    What does the following output?

    sed 's/./&\
    /g' << !!
    abcdefg
    !!

    Do you get...

    a
    b
    c
    d
    e
    f
    g

  11. #11
    Join Date
    Sep 2003
    Posts
    71
    Yes i get same output as your's.

    hust FYI i am using KORN shell on HP-UX

  12. #12
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    And what about this?

    sed 's/.\{3\}/&\
    /g' << !!
    abcdefg
    !!

    Do you get...

    abc
    def
    g

  13. #13
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Does your input file contain any newline characters at all? From memory, sed identifies each input line using the new line character ( or CR or similar).

    e.g.

    echo "foo" | sed 's/foo/bar/' # outputs 'bar'

    echo "foo\c" | sed 's/foo/bar/' # has no output

    You also stated that your file is of 184 byte fixed width, so what exactly is it that you are attempting?

  14. #14
    Join Date
    Sep 2003
    Posts
    71
    I normally use our inhouse C/utility for a addnewline

    i saw sed command you mentioned , that made me curious to test for doing it another way. but just recalled from your reply that sed need file ending with \n char as stdin.

    Thanks for detail explanation and all of your help

    Your are unix guru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •