Computing.Net > Forums > Unix > Can we do this in UNIX?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Can we do this in UNIX?

Reply to Message Icon

Name: hawkeye
Date: January 6, 2003 at 13:15:48 Pacific
OS: Solaris
CPU/Ram: 450/512
Comment:

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/2003

2. Record B.txt
SerialNumber|AddedDate|Expiredate|Phone|Email
123456|01/06/2003|06/01/2003|456-1234|someone@where.com

Can 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



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: January 6, 2003 at 13:24:59 Pacific
Reply:

Yes, that can easily be done with the awk command. Would you like me to post an awk solution?


0

Response Number 2
Name: hawkeye
Date: January 6, 2003 at 15:00:09 Pacific
Reply:

Hi James,

Yes, please. So I learn from it.

Thanks.


0

Response Number 3
Name: James Boothe
Date: January 6, 2003 at 15:32:39 Pacific
Reply:

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 > Anew

if [ $? -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
fi

exit 0


0

Response Number 4
Name: hawkeye
Date: January 13, 2003 at 11:32:56 Pacific
Reply:

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.


0

Response Number 5
Name: paradice
Date: January 30, 2003 at 13:43:13 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: hawkeye
Date: February 27, 2003 at 10:47:56 Pacific
Reply:

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|18

Could you help me to write this script I have no idea can we do this in UNIX.
Thanks,


0

Sponsored Link
Ads by Google
Reply to Message Icon

ftp shell Sorting numbers in Unix



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Can we do this in UNIX?

Calculate field. Can we do this ? www.computing.net/answers/unix/calculate-field-can-we-do-this-/4652.html

pass 2 input files to awk script www.computing.net/answers/unix/pass-2-input-files-to-awk-script/4448.html

How to do data cleansing in unix www.computing.net/answers/unix/how-to-do-data-cleansing-in-unix/7068.html