Cat writing only one record in the output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat writing only one record in the output file
# 1  
Old 02-07-2013
Cat writing only one record in the output file

Hi All,

I have an input file containing data as below:

Input.DAT
Code:
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).

Code:
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:
Code:
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.

Last edited by Scrutinizer; 02-07-2013 at 08:31 PM.. Reason: code tags
# 2  
Old 02-07-2013
Hi, there is $rowcount vs $rcnt and $nfnam vs. $newfilename
# 3  
Old 02-07-2013
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, ...)
Code:
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[i]) > "OUTPUT_" i ".DAT"}'

# 4  
Old 02-07-2013
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.
# 5  
Old 02-08-2013
When I try your script with the variable modifications, it is producing the result that you described. What result do you get?
# 6  
Old 02-08-2013
Hi Scrutinizer,

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

Sagar.
# 7  
Old 02-08-2013
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:
Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong output when writing to file

Hello, I am having problem while redirecting output to a file where as on console output is proper. for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; echo; done > output.txt Output of above command is coming in single line but when i am redirecting output to a file, single line i... (10 Replies)
Discussion started by: Manoj Rajput
10 Replies

2. Shell Programming and Scripting

Ssh cat file output into a file on local computer

Hello, I'm on a remote computer by SSH. How can I get the output of "cat file" into a file on the local computer? I cannot use scp, because it's blocked. something like: ssh root@remote_maschine "cat /file" > /locale_machine/file :rolleyes: (2 Replies)
Discussion started by: borsti007
2 Replies

3. Shell Programming and Scripting

Getting output with sed without writing to a file

HI I am trying to grep 3 characters from hostname and append a character at the end. I tried as in the following: root@abag3:~# hostname | cut -c1-3 hyu Now I am trying to append "g" at the end of this output as in the following. root@abag3:~# hostname | cut -c1-3 | sed -s... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

4. Shell Programming and Scripting

Writing output to a file in columns

Hi I am trying to write output to a file in columns I have file in the follwoing: # cat file abc def # I am trying to write next output as like # cat file abc 123 def 345 # :mad: (6 Replies)
Discussion started by: Priya Amaresh
6 Replies

5. UNIX for Dummies Questions & Answers

Cat Input & Output to a Log file

Team, we use below command to store the contents in a logfile. cat a.txt > a.log a.txt content is 123 345 Is there any options available to store the command used also? for eg a.log may show as cat a.txt 123 345 (5 Replies)
Discussion started by: sid2013
5 Replies

6. Shell Programming and Scripting

Getting output from a file similar to cat output

I have a file # cat /root/llll 11 22 33 44 When I cat this file content to a variable inside a shell script and echo that shell script, it does not show up as separate lines. I need echo output similar to cat. cat /root/shell_script.sh #!/bin/bash var=`cat /root/llll` echo $var (2 Replies)
Discussion started by: anil510
2 Replies

7. UNIX for Dummies Questions & Answers

Cat command drops lines in output file

I use the cat command to concatenate text files, but one of the rows I was expecting doesn't display in the output file. Is there a verbose mode\logging mechanism for the cat command to help me investigate where the lines I was expecting are going?? cat 7760-001_1_*_06_*.txt | grep -v... (1 Reply)
Discussion started by: Xin Xin
1 Replies

8. Shell Programming and Scripting

writing the output of SQL into one file

Hi All, Please help me writing the below script. I have two sql queries. 1. Select count(1),Client_id from TABLE_A group by Client_id; 2. Select count(1),Client_id from TABLE_B group by Client_id; I need the output of above two sql queries in a single file. The output 2nd query should be... (4 Replies)
Discussion started by: 46019
4 Replies

9. Shell Programming and Scripting

cat file and parse output

Hello, I'm new to shell scripting and did a search on the forum to what I want to do but couldn't find anything. I have about 9 routers that outputs to 1 syslog file daily named cisco.year.mo.date.log ex: cisco.2009.05.11.log My goal is to make a parsing script that cats today's syslog... (2 Replies)
Discussion started by: jjrambar
2 Replies

10. Shell Programming and Scripting

Writing given value into a file in particular record and column

Hi All, Could you ppl plz help me in writing into a file , plz find the example below. Ex: Name1|092387|Address1 Name2||Address2 After executing command/script the file should look like Name1|092387|Address1 Name2|+91900236|Address2 plz let me know of you have some solution... (4 Replies)
Discussion started by: shreekrishnagd
4 Replies
Login or Register to Ask a Question