gawk help for inserting a field of a .txt file in the same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gawk help for inserting a field of a .txt file in the same file
# 1  
Old 11-23-2009
CPU & Memory gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump:
Vouchers For Date :05/01/2009 *
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
ENTRY 4 13 FLDAIRY 20070038 GANDLA SIVA VARA PRASAD 9,400.00 GSD RRR
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total Debits For Date 05/01/2009 :
Total Credits : 9,400.00
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Vouchers For Date :06/01/2009
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
TR-INT 6 11 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
ENTRY 16 82 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
ENTRY 16 83 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 84.00 GSD BSR
ENTRY 9 46 FLDAIRY 20070022 DUDEKULA UMAYULU SB 7023 400.00 GSD BSR

i want the date part of 'Vouchers For Date :05/01/2009' to be inserted at the begining of each row till the next date comes..and so on...

i tried the following command:
BEGIN {i=0} NF>10 && NF<16{print $0 ; i++ } i==999 NEXT
which gave the data as under:
ENTRY 4 13 FLDAIRY 20070038 GANDLA SIVA VARA PRASAD 9,400.00 GSD RRR
TR-INT 6 11 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
ENTRY 16 82 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
ENTRY 16 83 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 84.00 GSD BSR

pl.. help by refining my above mentioned command
# 2  
Old 11-23-2009
Can you please provide an example of exactly how you want the output to look. Thanks.
# 3  
Old 11-23-2009
05/01/2009ENTRY 4 13 FLDAIRY 20070038 GANDLA SIVA VARA PRASAD 9,400.00 GSD RRR
06/01/2009TR-INT 6 11 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
06/01/2009ENTRY 16 82 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
06/01/2009ENTRY 16 83 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 84.00 GSD BSR

ON 5TH THERE IS ONLY ONE TXN AND HENCE ONE ROW WITH THE DATE 05/01/2009
OTHER TXNS ARE OF OTHER DAY HENCE NEXT ROWS HAD THE NEXT DAY.,TILL NEXT DATE TXNS COMES...
# 4  
Old 11-23-2009
input
Code:
$ cat vtest
Vouchers For Date :05/01/2009 *
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
ENTRY 4 13 FLDAIRY 20070038 GANDLA SIVA VARA PRASAD 9,400.00 GSD RRR
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total Debits For Date 05/01/2009 :
Total Credits : 9,400.00
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Vouchers For Date :06/01/2009
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
TR-INT 6 11 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
ENTRY 16 82 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
ENTRY 16 83 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 84.00 GSD BSR
ENTRY 9 46 FLDAIRY 20070022 DUDEKULA UMAYULU SB 7023 400.00 GSD BSR

awk file
Code:
$ cat vtest.awk
function f() { if(dat!="") {print dat > ("vout.txt")} }

/^Vouchers/   {
                f()
                dte=substr($0,(match($0,":")+1),10)
                dat=""
              }

/GSD/         {
                f()
                dat=dte $0
              }

END           { f() }

output
Code:
$ cat vout.txt
05/01/2009ENTRY 4 13 FLDAIRY 20070038 GANDLA SIVA VARA PRASAD 9,400.00 GSD RRR
06/01/2009TR-INT 6 11 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
06/01/2009ENTRY 16 82 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 3.00 GSD BSR
06/01/2009ENTRY 16 83 FLDAIRY 20070011 GANI DIBBAMMA SB 5662 84.00 GSD BSR
06/01/2009ENTRY 9 46 FLDAIRY 20070022 DUDEKULA UMAYULU SB 7023 400.00 GSD BSR

how to run
Code:
$ awk -f vtest.awk vtest

# 5  
Old 11-23-2009
A slightly simplified version: -

Code:
nawk '
  /^Vouchers/{date=substr($0,(match($0,":")+1),10)}
  /GSD/{print date $0}
' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting a blank line at the end of a .txt file?

Hi there, I am having this problem: a) I am uploading a txt file from windows (notepad) with some Gaussian 09 command lines; b) Gaussian needs in certain command files, that those files have a blank line at the end of the file! c) I open the command file with vi and no blank line at the of... (2 Replies)
Discussion started by: luismga
2 Replies

2. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

3. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

4. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

5. Shell Programming and Scripting

Inserting a new field inbetween two exisitng field

I have a '|' delimited file. My file looks like below 23|nationalhoilday|feb12||||||||||||||california|northdistrict|| In the same way, each record has 164 fields. I have to insert one more field after the 85th field. Expected output... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. Shell Programming and Scripting

how to add extra a field in a flat txt file ?

Hi all, I did not use UNIX for a long time, now i need to make a flat file with extra field, can you help me with the code ? 1. I create a last line of each log from each system and make it in a flat text file (seperate by a pipe |) mv current.log old tail -1 sanfrancisco.log > current.log... (5 Replies)
Discussion started by: britney
5 Replies

7. UNIX for Dummies Questions & Answers

Best method for inserting a field from a file into another file?

Hello, Hoping someone can help! I have a file (fileA) with a potentially different number of | delimited fields the file looks like: fileA A|B|C|D|E|F|G| A|B|C|D|E|F|G| This file could have 100+ fields and have 2million+ records I have another file (fileB) which contains an 8 digit... (3 Replies)
Discussion started by: dendright
3 Replies

8. Shell Programming and Scripting

ORA-01756 Error while inserting a file in CLOB field

Hi, Please guide me where i am doing wrong, i am getting ORA-01756:quoted string not properly terminated when i am trying to insert file into CLOB cloumn of Oracle DB. Please find below the code where log file variable is myLogFile. Please let me know where i am doing wrong. ... (0 Replies)
Discussion started by: rajeshorpu
0 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. Shell Programming and Scripting

Reindex or re-increment last field of txt file.

I am using a database text file with a field that increments +1 with each new entry, occasionally if a entry is deleted the unique sequence is disrupted. I am looking for a small script/function in sh and/or perl that would re index this. Example of db file: Name | Address | misc |number... (2 Replies)
Discussion started by: silenthands
2 Replies
Login or Register to Ask a Question