Problem while creating "trailer"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem while creating "trailer"
# 1  
Old 02-11-2008
Java Problem while creating "trailer"

I've 3 data files as below,
In each file first line-first letter 'H' indicates the header,
'0' and '9' indicates the data record in which 0 for credit and 9 for debit.
Also from the position 5-6 we have this credit/debit amount values.

In last line, 'T' indicates the 'trailer record'. second field indicates the
number of debit 'data record' and third field indicates the the number of
credit 'data record'. fourth field indicates 'sum value of debit' and last field
indicates 'sum value of credit'.

file1.txt

H0000000000001
9abc20gfhxyzer
0mnw10dcspreer
9lst70axrstrqw
9lkd20trqeretr
0dre60abcwerip
0ter20kdjjfkjk
T 03 03 110 90

file2.txt

H0000000000001
9abc20gfhxyzer
0mnw10dcspreer
9lst70axrstrqw
9lkd20trqeretr
0dre60abcwerip
T 03 02 110 70

file3.txt

H0000000000001
9abc20gfhxyzer
0mnw10dcspreer
0lst70axrstrqw
9lkd20trqeretr
0dre60abcwerip
T 02 03 40 140


From the above files Iam creating one out file "outfile.txt" as below:

printf"H0000000000001" > outfile_header.txt ##Header record

## Selecting the data record
nawk '
/^[0-9]/ { print $0 }
' $cur_file >> outfile_data.txt ##Data records

Now the problem Iam facing is to create the trailer for the 'output.txt'.
How to calculate the sum/total of debit and credit from the created 'outfile_data.txt'.


please help me / Lokiman
# 2  
Old 02-11-2008
Could you post the desired result,
given the content of file*.txt you posted?
# 3  
Old 02-11-2008
outfile.txt should be:

H0000000000001 ##Header
9abc20gfhxyzer
0mnw10dcspreer
9lst70axrstrqw
9lkd20trqeretr
0dre60abcwerip
0ter20kdjjfkjk
9abc20gfhxyzer
0mnw10dcspreer
9lst70axrstrqw
9lkd20trqeretr
0dre60abcwerip
9abc20gfhxyzer
0mnw10dcspreer
0lst70axrstrqw
9lkd20trqeretr
0dre60abcwerip
T 08 08260 300 # Trailer

It should include all the data records from the above 3 files.

The trailer should contain, number of debit records (08)- starting with '9',
number of credit records (08)- starting with '0' , total for debit (260) and credit (300) records.

Also I've to compare (validate) the sum of all trailer record values for the above with the 'output.txt'
trailer values.
# 4  
Old 02-11-2008
Code:
awk '
END { printf "T %02d %02d %02d %02d\n", dc, cc, d, c 
if ((dc FS cc FS d FS c) == (dct FS cct FS dt FS ct))
  print "Trailers are correct." > "/dev/stderr"
else
  print "Trailers are wrong!" > "/dev/stderr" }
/^T / { split($0,t," "); dct += t[2]; cct += t[3]; dt += t[4]; ct += t[5]; next }
/^9/ { dc ++; d += $2 } /^0/ { cc ++; c += $2 } NR == 1 || FNR > 1
' FS="[a-z][a-z][a-z]" file* > output.txt

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 02-11-2008
Marvelous Genius

Thanks a crore Genius,

Also special thanks for your interest on such a fresher like us.

With Regards / Lokiman
# 6  
Old 02-12-2008
Error reading awk variable values from outside

One important requirement. I want to read the variable values which are declared inside the 'awk' script from the outside.

pls consider the below script:

#!/usr/bin/ksh

nawk '
BEGIN{
v_num_zero=0;
v_sum_zero=0;
v_num_nine=0;
v_sum_nine=0;
}

{
}

/^0/ {
v_num_zero++;
cur_zero=substr($0,5,2);
v_sum_zero=v_sum_zero+cur_zero;
}

/^9/ {
v_num_nine++;
cur_nine=substr($0,5,2);
v_sum_nine=v_sum_nine+cur_nine;
}

END {
printf "Number of Zeros are: %d\n",v_num_zero;
printf "Sum of Zeros are : %d\n",v_sum_zero;
printf "Number of Nines are: %d\n",v_num_nine;
printf "Sum of Nines are : %d\n",v_sum_nine;
} ' a.txt

echo $v_num_zero
echo $v_sum_zero
echo $v_num_nine
echo $v_sum_nine


I want to read the variable values 'v_num_zero', 'v_sum_zero', 'v_num_nine',
'v_sum_nine' for future use.

can you suggest some idea on this...?
# 7  
Old 02-12-2008
Hm, something ugly like this:

Code:
eval "$(nawk '
/^0/ {
v_num_zero++;
v_sum_zero+=$2;
}
/^9/ {
v_num_nine++;
v_sum_nine+=$2;
}
END {
print"v_num_zero="v_num_zero,"v_sum_zero="v_sum_zero,
"v_num_nine="v_num_nine,"v_sum_nine="v_sum_nine
}' FS="[a-z][a-z][a-z]" file*)"

For example:

Code:
$ eval $(nawk '
/^0/ {
v_num_zero++;
v_sum_zero+=$2;
}
/^9/ {
v_num_nine++;
v_sum_nine+=$2;
}
END {
print"v_num_zero="v_num_zero,"v_sum_zero="v_sum_zero,
"v_num_nine="v_num_nine,"v_sum_nine="v_sum_nine
}' FS="[a-z][a-z][a-z]" file*)
$ set|fgrep v_
_=v_sum_nine=260
v_num_nine=8
v_num_zero=8
v_sum_nine=260
v_sum_zero=300

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Creating a Shortcut (to just type "l" but it runs "ls -lah")

How do I create shortcuts? For example: I just want to type one key "l" and have it output the command of "ls -lah" I believe it's creating a file called l with 755 permissions but I'm not sure where to put the file. *if it matters, I'm on a shared hosting web server using cPanel with... (2 Replies)
Discussion started by: ijustsawmars
2 Replies

6. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

7. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Unix "at" / "Cron" Command New Problem...Need help

Hi All, I am trying to schedule a one time job using the at command with the help of shell script for my project. The shell script should take a parameter as a command line argument from the at command itself. Is it possible to take a command line parameter for a shell script in the command... (3 Replies)
Discussion started by: Mohanraj
3 Replies
Login or Register to Ask a Question