Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I have 2 flat files database:
1. Record A.txt
Name|SerialNumber|PartNumber|Location|Addeddate|Expiredate
hawkeye|123456|321-23-2345|H1|10/22/2002|01/05/20032. Record B.txt
SerialNumber|AddedDate|Expiredate|Phone|Email
123456|01/06/2003|06/01/2003|456-1234|someone@where.comCan we write the script to take all
today record (AddedDate) in Record B.txt and search for serial number in Record A.txt
and replace AddedDate|Expiredate fiield in Record A.txt with AddedDate|Expiredate in Record B.txt
Name|SerialNumber|PartNumber|Location|Addeddate|Expiredate
hawkeye|123456|321-23-2345|H1|01/06/2003|06/01/2003
Do you think we can do that in UNIX shell ??
Thanks

This awk script stores today B-fields in an array, then outputs a new A-file, rebuilding each A-record that has a matching array entry. Upon successful processing, the new A-file replaces the original A-file.
The "new awk" is required for the -v command line option to pass a variable. On some platforms you would have to run nawk. There are also other ways to feed a variable to awk.
To prevent this web page from losing my indentation (which helps readability), I have replaced all leading spaces with underscores. After you copy/paste the script, please change all underscores to spaces.
And, sorry, one more thing ...
My first attempt at posting this lost most of the awk code because of the way this web page handles left and right angle brackets. So you also need to change all three occurrences of LAB to the actual left angle bracket (the less-than sign).
#!/bin/sh
today=`date +%m/%d/%Y`
awk -F"|" -v today=$today 'BEGIN {\
getline LAB "B.txt" # Verify and bypass B header
if (NF!=5)
___{print "B text header is not 5 words as expected - abort"
____exit 1}
rc=getline LAB "B.txt"
while (rc==1)
__{if ($2==today)
_____{adddate[$1]=$2
______expdate[$1]=$3}
___rc=getline LAB "B.txt"}
getline # Process A header
if (NF!=6)
___{print "A text header is not 6 words as expected - abort"
____exit 1}
print # Output A header
}
{if ($2 in adddate)
____print $1 "|" $2 "|" $3 "|" $4 "|" adddate[$2] "|" expdate[$2]
else
____print
}' A.txt > Anewif [ $? -ne 0 ] ; then
___print "Error detected in awk (see Anew)"
___print "A.txt remains unchanged"
else
___print 'Moving A.txt to A.old ...'
___mv A.txt A.old
___print 'Moving Anew to A.txt ...'
___mv -i Anew A.txt
fiexit 0

Hi James,
How do it know to get the serial number in B.txt and search in A.txt to change for add date and expire date. I am lost.
Thanks for your help.

I was thinking of learning about UNIX, but iam not really sure what it can do. I know the internet is based on unix but is that all?
I know system administrators work with unix(some)to work with files and stuff, but is that all?
IS unix really fun? what are the cool stuffs a person can do with unix? if the internet is based on unix and i learn it what will i be able to do on the web? can you please answer me. Iam thinking about buying the book "the unix programming environment" by kernighan and pike, should I? what do you think? is there any book you can recomend to talk about unix and it abilities or what i can do with it internet wise and other stuff.thank you for your patients reading.
Please, anyone who knows, IAM TLKING TO YOU!

If I have a flat files:
Name|A|B|C|D|E|F
andy|2|3|4|5|6|3
judy|1|2|4|5|3|2
rick|4|0|1|2|5|6
I would like to run the script so it will write to a newdata.txt
Name|A|B|C|D|E|F|Total
andy|2|3|4|5|6|3|(2+3+4+5+6+3)=23
judy|1|2|4|5|3|2|12
rick|4|0|1|2|5|6|18Could you help me to write this script I have no idea can we do this in UNIX.
Thanks,

![]() |
ftp shell
|
Sorting numbers in Unix
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |