Append value(batch number) to start of records


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Append value(batch number) to start of records
# 1  
Old 04-16-2008
Append value(batch number) to start of records

Hi all,
I am new to unix shell scripting and I am trying to append batch number that comes in Trailer record to the detailed record.

TR|20080312|22881 |000000005|20080319|2202
LN|20080312|077777722220 |0000100000017|ABS
LN|20080312|000799439326 |0000709943937|AA
TR|20080313|22897 |000000008|20080319|2202
LN|20080313|077777722220 |0000100000017|BCG
LN|20080313|603497005222 |0000600070057|ADXC
TR|20080314|22898 |000000011|20080319|2202
LN|20080314|077777722220 |0000100000017|kKAS
LN|20080314|603497005222 |0000600070057|adfras

This is how my data would be. I want to pull out Batch number i.e 4th column in the record that starts with TR and append it to first column of LN. This is one single file.

Filename=$1
grep '^TR' $Filename>A.txt
grep '^LN' $Filename>B.txt

#join -t"|" -1 2 -2 2 -0 1.4 A.txt B.txt >C.txt

cat B.txt | while read line
do
var=`echo $line | cut -d"|" -f2`
cat A.txt | while read value
do
val=`echo $value | cut -d"|" -f2`
val1=`echo $value | cut -d"|" -f4`
if [[ $var == $val ]]
then
line=$val1"|"$line
echo $line>>Apple.txt
fi
done
done

I tried the above code and its very very slow. The volume of records is from 2Mb to 60 MB.

Please adive. I think awk is faster but not sure how to do it in awk

Thanks
Kiran
# 2  
Old 04-16-2008
You are looping over the entire file for every record? Whew!

You could at least optimize it to read only the matching lines.

Code:
grep "|$var|" A.txt | while read ...

But anyway, this is simple to do in awk, without any temporary file.

Code:
awk -F '|' '/^TR/{t = $4 }
/^LN/{printf "%s|%s\n", t, $0 }' "$1"

I hope I captured your requirements correctly. This assumes the TR always comes before the corresponding LN and that the LNs with the same number will always be immediately after the corresponding TR, and that there will be no LNs with other numbers before the next TR. It prints only the LNs, with field 4 from the previous TR tacked on at the front.
# 3  
Old 04-16-2008
Hi
I would really appreciate it if you could tell me where to pass the file name. I have no clue.

Thanks
Kiran
# 4  
Old 04-16-2008
I put $1 just like you had. Sorry, that's obviously pretty obscure. Replace that with the actual file name.
# 5  
Old 04-16-2008
Hi,
Thanks for quick reponses.
I tried

awk -F '|' '/^TR/{t = $4 }
/^LN/{printf "%s|%s\n", t, $0 }' "temp.txt"

I am getting the following error.

awk: syntax error near line 1
awk: bailing out near line 1

Do you know what that means?

Thanks
Kiran
# 6  
Old 04-16-2008
Hi,
I got it. I am using Sun solaris and I have to use nawk. It solved the issue.
your code worked like magic. Thanks a Bunch. Is there any way to remove unwanted spaces in that file?

Thanks
Kiran
# 7  
Old 04-17-2008
Where there is a will, there is a way.

I assume you want the spaces before the separators out?

Code:
nawk -F '|' '/^TR/{t = $4 }
/^LN/{gsub (" +\|","|"); printf "%s|%s\n", t, $0 }' filename

Edit: silly me, there are no other spaces.

Just to recap, this captures $4 into t if this is a line starting with "TR". If it starts with "LN", it substitutes spaces followed by separator with just a separator, then prints the value of t followed by the whole current line.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Start process on X number of files and then wait for the next batch

Thanks for RudiC for his extraordinary help on organizing files in a batch of 10 using below code. FL=($(ls)); for ((i=0;i<=${#FL};i++)); do for j in ${FL:$i:10}; do $batch ${j} ${j}.txt done; echo "Pausing for next iteration"; echo... (6 Replies)
Discussion started by: busyboy
6 Replies

2. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

5. Windows & DOS: Issues & Discussions

How to start a vbs from a windows batch file?

Morning, I'm trying to execute a vbs from a .bat file. Can someone tell me what the difference is between these statements: start c:\lib\runit.vbc c:\lib\runit.vbs When I run the batch with the 'start' parameter it doesn't seem to do anything. (1 Reply)
Discussion started by: Grueben
1 Replies

6. UNIX for Dummies Questions & Answers

pull date from header and append to all records

I did some searches, but couldn't really find what I'm looking for. I have a file formatted as below: BOF ABC CO - XYZ COMM DATA OF 07/05/2011 EBA00000001 sdfa rtyus uyml EBB00000001 54682 984w3 EBA00000002 mkiyuasdf 98234 I want to pull the date from the header record and add it... (4 Replies)
Discussion started by: keeferb
4 Replies

7. UNIX for Dummies Questions & Answers

Append symbol at the start of each line

hi, i have some values in excel sheet as in below format: 122 144 222 555 666 etc.... i need to get the output in the below manner.. £122 £144 £222 £555 £666 (1 Reply)
Discussion started by: arunmanas
1 Replies

8. Shell Programming and Scripting

Append string at start of line

Hi, I want to append # at the start of line wherever keyword xyz is found through stream editor? Is it possible? (18 Replies)
Discussion started by: db2cap
18 Replies

9. Shell Programming and Scripting

How to append records to a file using PERL

Hi All, Great Forum and Great help. Keep up the good work. My question is what is the command and it's syntax to append a record to an output file using PERL. Please provide the command syntax. In regular shell you can use the '>>' to append. Basically, I am creating a small report... (1 Reply)
Discussion started by: nurani
1 Replies

10. AIX

Script to start a remote batch job on another server

Hi , I am trying to execute one script residing on server B from server A and in automated way but with a trigger. My main quetion are 1) How I will login to the remote server automatically with user name and password. ( rsh or any other way ?) 2) Once logged in I need to execute... (2 Replies)
Discussion started by: agent47
2 Replies
Login or Register to Ask a Question