If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Insert a carriage return in a loooong record?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #2 (permalink)  
Old
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 5,398
Re: Insert a carriage return in a loooong record?

Quote:
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.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
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
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Nov 2003
Posts: 3
Thanks for the replies.

The last split command worked great.
Reply With Quote
  #6 (permalink)  
Old
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 5,398
Quote:
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.
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
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,
Reply With Quote
  #8 (permalink)  
Old
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
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
Reply With Quote
  #9 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #10 (permalink)  
Old
Padawan
 
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
Reply With Quote
  #11 (permalink)  
Old
Registered User
 
Join Date: Sep 2003
Posts: 71
Yes i get same output as your's.

hust FYI i am using KORN shell on HP-UX
Reply With Quote
  #12 (permalink)  
Old
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
And what about this?

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

Do you get...

abc
def
g
Reply With Quote
  #13 (permalink)  
Old
Padawan
 
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?
Reply With Quote
  #14 (permalink)  
Old
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On