Add substring in a file containing fixed length record.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add substring in a file containing fixed length record.
# 1  
Old 03-30-2012
Java 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 the records.I have written a script as below but not getting the output. Any help would be highly appreciated.

Sample record is:
0180300000000001204500502096007322012033008084435571802577250789100000082102096508800000000000000000 0000000DFE02E05000000000000000000000000D37304F41366904036roaming.skysundi.com mnc005.mcc450.gert 121000000000000000000000000B4878544c0e501690130393039062000409C600000804F407020000080000000000000000 0000000000000000000006000000003623
# 2  
Old 03-30-2012
Missing the script? Smilie
# 3  
Old 03-30-2012
Requesting more information

Can you please give some example. It is difficult to understand the problem you are facing from current explanation.

Thanks
# 4  
Old 03-30-2012
Thanks for the reply..

The script:
Code:
awk 'BEGIN {sum_a=0;sum_b=0;a=substr($0,389,3);b=substr($0,399,3) }
{
printf"%d\t\t%d",a,b;
sum_a = sum_a+a;
sum_b = sum_b+b;
}
END {printf"%d\t\t%d",sum_a,sum_b}

'

=> The sample fixed length srting in the question is a continuous string containing spaces.

Last edited by Franklin52; 03-31-2012 at 02:41 PM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 03-30-2012
You're attempting to extract the substrings before you've read any records.
Code:
awk 'BEGIN {sum_a=0;sum_b=0; }
{
a=substr($0,389,3);b=substr($0,399,3)
printf"%d\t\t%d",a,b;
sum_a = sum_a+a;
sum_b = sum_b+b;
}
END {printf"%d\t\t%d",sum_a,sum_b}'

This User Gave Thanks to CarloM For This Post:
# 6  
Old 03-30-2012
Thanks CarloM it working fine now, thanks arun.ijhs.klm for your time.
can you tell me the best way to debug any shell script in general
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

Fixed Length file from a SQL script

Hi, I have a DB2 UDB 9.7 SQL script, as follows: I need to pass the script into Unix and generate a fixed length file from this. Can someone kindly provide a script to achieve it? SELECT CAST(COALESCE(CL_ID,'000000000') AS CHAR(9)) AS CL_ID ,STATUS... (5 Replies)
Discussion started by: ebsus
5 Replies

3. 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

4. Shell Programming and Scripting

Add character based on record length

All, I can't seem to find exactly what I'm looking for, and haven't had any luck patching things together. I need to look through a file, and if the record length is not 874, then add 'E' in position 778. Your help is greatly appreciated. (4 Replies)
Discussion started by: CutNPaste
4 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

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

8. 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

9. Shell Programming and Scripting

fixed record length

hello! I have a file with fixed record length... format: 123445asdfg 4343777 sfgg I wanna convert it to 123445,asdfg ,4343,777 ,sfgg is there any way to do it? sed/grep/awk?? at the moment I use sed -e 's_ \(\)_,\1_g' but it works only if there are spaces between... (16 Replies)
Discussion started by: george_
16 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