Cat writing only one record in the output file

Hi All,

I have an input file containing data as below:

Input.DAT

XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|YY|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111
XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|AA|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111

I am executing below series of commands by validating 5th character of the input file (i.e. B in the said example).

rcnt=$(awk '{n++} END {print n}' Input.DAT)
fcnt=$(printf "%010d" $rowcount)
fnam1="OUTPUT"
fnam2="_B"
fnam3=".DAT"
nfnam=$(echo $fnam1$fnam2$fnam3)
touch $nfnam
echo "HEADER|$nfnam" > $nfnam
cat Input.DAT >> $nfnam
echo "TRAILER|$fcnt" >> $newfilename

The ouput file OUTPUT_B.DAT should contain as below:

HEADER|OUTPUT_B.DAT
XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|YY|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111
XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|AA|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111
TRAILER|0000000002

But, I am getting only one detailed record in the ouput file.

Can anyone please advise.
Thanks a lot in advance.
Sagar.

Hi, there is $rowcount vs $rcnt and $nfnam vs. $newfilename

The below AWK code does what (I think) you want to have done. It will also work in case of varying characters in field 5 (A, B, C, etc) and print the input lines into corresponding output files (OUTPUT_A.DAT, OUTPUT_B.DAT, OUTPUT_C.DAT, ...)

awk -F'|'  '{
outfile = "OUTPUT_" $5 ".DAT"
if (!a[$5]++) 
  print "HEADER" FS outfile > outfile
print $0 > outfile}
END { 
for (i in a)  
  print "TRAILER" FS sprintf("%010d", a) > "OUTPUT_" i ".DAT"}'

Hi Scrutinizer,

yes, rowcount should be replaced by rcnt and newfilename should be replaced by nfnam in my above script.

Hi user8,

Thanks a lot for quick response. But, I tried to run each individual command at command line and it is working fine. Is there any other simples way of doing this by modifying my script.

Sagar.

When I try your script with the variable modifications, it is producing the result that you described. What result do you get?

Hi Scrutinizer,

I am getting only one record even though the input file has more than one record.

Sagar.

Your script seems to only add a header and a trailer, the part that that puts the records into the file is the cat command:

cat Input.DAT >> $nfnam
  • are you saying it does not cat the entire content into the output file?

-- hmmm ----

  • That seems unlikely, do you mean $rcnt is only set to "1"?
  • Could you post the actual output you are getting?

-- otherwise --

  • What shell are you using, on what OS and version?
  • How do you running the script?
  • Could you copy and past the input file from the sample you posted here into a new file and test with that input file?

Hi Scrutinizer,

I am using below sequence of commands and I am getting only one record in the ouput file:

echo "HEADER|$nfnam" > $nfnam
cat Input.DAT >> $nfnam
echo "TRAILER|$fcnt" >> $newfilename

Sagar

I notice the variables $nfnam vs. $newfilename again. Also, could you answer the questions in my previous post?

Hi Scrutinizer,

Below is series of commands I am executing in a shell script:

echo "HEADER|$nfnam" > $nfnam
cat Input.DAT >> $nfnam
echo "TRAILER|$fcnt" >> $nfnam

I am getting as below:

HEADER|OUTPUT_B.DAT
XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|YY|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111
TRAILER|0000000002

What shell are you using, on what OS and version? --------- ksh
How do you running the script? --------- by giving name of shell script in the command line.

Could you copy and past the input file from the sample you posted here into a new file and test with that input file?

XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|YY|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111
XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|AA|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111
 

Sagar

What OS and version
How exactly are you running the script?
With copy and paste I mean the other way around, copy it from this website and paste it into a file on which you can test your script..

also
what happens if there are 4 lines in your inout file?

Pls post a binary or hex listing of your input file. Use e.g. od or hexdump .