Creating new file with new record length


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating new file with new record length
# 1  
Old 07-21-2008
Creating new file with new record length

Hi there, using the command below

> cat file1 | tr "\001" " " > tmp2

how can I alter the record length of tmp2 to fix length of 350 bytes ?
Thanks a lot!
# 2  
Old 07-21-2008
Hi.

I assume that you mean line length when you speak of record length. Here is an example that takes a long line and breaks it into a few pieces:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate feature of fold on long lines.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) fold
set -o nounset
echo

FILE=${1-data1}

echo " Characteristics of data file $FILE:"
wc $FILE

echo
echo " Results of folding:"
fold -w 350 $FILE >t1
wc t1

echo
echo " Characteristics of first line:"
head -1 t1 | wc

echo
echo " Last few characters of file:"
od -Ad -bc t1 | tail -3

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
fold (coreutils) 5.2.1

 Characteristics of data file data1:
   1    1 1002 data1

 Results of folding:
   3    3 1004 t1

 Characteristics of first line:
      1       1     351

 Last few characters of file:
0000992 163 061 060 060 163 065 066 067 070 071 060 012
          s   1   0   0   s   5   6   7   8   9   0  \n
0001004

See man fold for details -- you may wish to use -b for bytes ... cheers, drl
# 3  
Old 07-21-2008
Change file Line Length

Hi there, thanks for the answer. I didnt make my question clear. so here it is.
My file have 304bytes per line (line length). I want to pad spaces at the end of column 304 up to colum 350. So the record length is changed from 304 to 350 bytes.
I tried variations of unix command it doesnt work. Please help!
thanks a lot !!
# 4  
Old 07-21-2008
Try this:

Code:
awk 'BEGIN{for(i=305;i<350;i++){s=s" "}}{print $0 s}' file > newfile

# 5  
Old 07-21-2008
Code:
awk '{printf("%-350s\n",$0)}' file


Last edited by radoulov; 07-21-2008 at 06:33 PM.. Reason: removed some wrong suggestions
# 6  
Old 07-23-2008
updating data in a file automatically

Thanks guys! it worked!
I have followup, I need to update the file by putting a 12byte data on each line (from column 305 through 317) and output it to a file with fix line length of 350 (as the previous post solution). I am doing this manually and i wonder if it can be automated using the awk programming.
Thanks a lot !!! you guys rock!
# 7  
Old 07-23-2008
Quote:
Originally Posted by mrjunsy
I need to update the file by putting a 12byte data on each line
What kind of data, please give an exmple of the desired output.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

Creating repeating record in between file through script

apprecieate your help to resove this. My source file looke like 1001 000 HEADER 1001 001 RAJESH 1001 002 100 1001 002 200 1001 002 500 1001 006 FOOTER 1002 000 HEADER 1002 001 RAMESH 1002 002 100 1002 002 200 1002 002 500 1002 006 FOOTER my... (8 Replies)
Discussion started by: Ganesh L
8 Replies

3. Shell Programming and Scripting

Add substring in a file containing fixed length record.

I am new to awk and writing a script using awk. I have file containing fixed length records, I wish to extract 2 substring(each substring is padded with zeros on left e.g 000000003623) and add each substring respectively for every record in the file to get total sum of respective substring for all... (5 Replies)
Discussion started by: Devesh5683
5 Replies

4. Shell Programming and Scripting

sort file specifying record length

I've been searching high and low for this...but, maybe I'm just missing something. I have a file to be sorted that, unfortunately, contains binary data at the end of the line. As you may guess, this binary data may contain a newline character, which messes up the sort. I think I could resolve this... (5 Replies)
Discussion started by: jcagle
5 Replies

5. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

7. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

8. UNIX for Dummies Questions & Answers

Creating a file in variable length format

Hi all, Does anyone know a technique for creating a download file in variable length format? I have looked around but haven't found any resources on this (or, maybe I'm not sure what to Google for :) ) Thanks in advance! (2 Replies)
Discussion started by: Sarahb29
2 Replies

9. Shell Programming and Scripting

Pivot variable record length file and change delimiter

Hi experts. I got a file (500mb max) and need to pivot it (loading into ORCL) and change BLANK delimiter to PIPE |. Sometimes there are multipel BLANKS (as a particular value may be BLANK, or simply two BLANKS instead of one BLANK). thanks for your input! Cheers, Layout... (3 Replies)
Discussion started by: thomasr
3 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question