awk: creating a fixed-width single file from 2 different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: creating a fixed-width single file from 2 different files
# 1  
Old 02-24-2010
awk: creating a fixed-width single file from 2 different files

I have to create a single file from three files, Please see below for samples:

day.txt
20090101
20090102

item.txt
123456789101
12345678910209
1234567891

str.txt
1
12
123

output.txt
20090101123456789101 1 0
2009010112345678910209 12 0
200901011234567891 123 0
20090101123456789101 1 0
2009010112345678910209 12 0
200901011234567891 123 0
20090101123456789101 1 0
2009010112345678910209 12 0
200901011234567891 123 0
20090102123456789101 1 0
2009010212345678910209 12 0
200901021234567891 123 0
20090102123456789101 1 0
2009010212345678910209 12 0
200901021234567891 123 0
20090102123456789101 1 0
2009010212345678910209 12 0
200901021234567891 123 0

output should be:
1.Cartesian join of all item stores day
2.Date would occupy characters 1-8.
2.Item would occupy characters 9-28.If the characters are not enough to make 20 characters, pad it with space.
3.Str would occupy character 29-48.If the characters are not enough to make 20 characters, pad it with space.
4.The 49th character of the line would always be zero.

---------- Post updated at 05:30 PM ---------- Previous update was at 05:18 PM ----------

the output file was not diplayed correctly, the white spaces between the fields was truncated to a single space.

Last edited by tamahomekarasu; 02-24-2010 at 04:46 AM..
# 2  
Old 02-24-2010
day.txt has only 2 records!?

try if it works,

Code:
/home->cat day.txt 
20090101
20090102
20090102
/home->cat item.txt 
123456789101
12345678910209
1234567891
/home->cat str.txt 
1
12
123
/home->while read line; do printf "%-20d\n" $line;done< item.txt > item.tmp
/home->while read line; do printf "%-20d 0\n" $line;done< str.txt > str.tmp
/home->paste -d '' day.txt item.tmp | paste -d ' ' - str.tmp > outfile.txt
/home->rm item.tmp str.tmp
/home->cat outfile.txt
20090101123456789101         1                    0
2009010212345678910209       12                   0
200901021234567891           123                  0
/home->

I expect an awk solution which would be simpler.

Last edited by clx; 02-24-2010 at 07:06 AM.. Reason: added "> outfile.txt"
# 3  
Old 02-24-2010
Code:
#!/usr/bin/perl
use warnings;
my (@day,@item,@str);

open(DAY,"day.txt") || die "can't open file:$!";
open(ITEM,"item.txt") || die "can't open file:$!";
open(STR,"str.txt") || die "can't open file:$!";

@day=<DAY>;
@item=<ITEM>;
@str=<STR>;
map(chomp,@day);
map(chomp,@item);
map(chomp,@str);
my $dlen=@day;
my $ilen=@item;
my $slen=@str;
close(DAY);
close(ITEM);
close(STR);

print "$dlen:$ilen:$slen\n";

for($i=0;$i<$dlen;$i++)
 {
   for($j=0;$j<$ilen;$j++)
    {
    for ($k=0;$k<$slen;$k++)
      {
       printf("%-8s%20s%20s%3s\n",$day[$i],$item[$j],$str[$k],"0");
      }
   }
 }


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Joining fixed width files

Hi All, I need to join fixed width files on a column which is position 1 to 3 and need to have all the records from file1 file1.txt Cu1nullL1L2 Cu2nullL1L2 Cu3nullL1L2 file2.txt Cu1B1B2 Cu3B1B2 output.txt Cu1L1B1L2B2 Cu2L1L2 Cu3L1B1L2B3 I tried but not getting the expected... (12 Replies)
Discussion started by: shash
12 Replies

2. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

3. Shell Programming and Scripting

Replace using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

4. Shell Programming and Scripting

Print column details from fixed width file using awk command

hi, i have a fixed width file with multiple columns and need to print data using awk command. i use: awk -F "|" '($5 == BH) {print $1,$2,$3}' <non_AIM target>.txt for a delimiter file. but now i have a fixed width file like below: 7518 8269511BH 20141224951050N8262 11148 8269511BH... (5 Replies)
Discussion started by: kcdg859
5 Replies

5. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

6. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

7. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies

8. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

9. UNIX Desktop Questions & Answers

Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated. I... (4 Replies)
Discussion started by: sate911
4 Replies

10. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "©" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies
Login or Register to Ask a Question