Operating System - HP-UX
1832647 Members
2752 Online
110043 Solutions
New Discussion

Re: Newbie>>>How write a script to alter the data in a file??

 
Bobbi Rawlings
New Member

Newbie>>>How write a script to alter the data in a file??

I have a fixed width file, 20 records, that has employee data in it including the Social Security number and Name. I want to write a script that will allow me to read this file, and change the Social Security Number and Name to bogus information. I was thinking of doing this by creating another file that would have the original_SSN, alias_SSN and Alias_Name. I would use this file to substitute the original SSN and NAMES with the aliases and save the results in another file. Or it may be easier to just alter the original file. I don't know.

Original File:
SSN NAME TITLE OTHER JUNK
999999999 Jim Dandy Engineer 0000111222
888888888 Jane Dandy Accountant0000222333

Alias File
Orig_SSN Alias_SSN Alias_name
999999999 555555555 John Doe
888888888 444444444 Jane Doe

Resulting File:
Alias_SSN Alias_Name Title OtherJunk
555555555 John Doe Engineer 0000111222
444444444 Jane Doe Accountant0000222333

P.S. The file has to retain the fixed width format of the original :)

O.K. HEEEELLLLLPPPPP!!!!!

Thanks
14 REPLIES 14
harry d brown jr
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

If's its only going to have 20 records in it and it's not going to change much, then "vi" would be 1000 times faster.

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Sort the two files, ORIG and ALIAS, then use "join":

join -o 2.2,1.2,1.3,1.4,1.5,1.6 file_orig file_alias > file_NEW


live free or die
harry
Live Free or Die
Darrell Allen
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Does it matter what SSN and OTHER JUNK are substituted with? Can the substitution be the same for each record? If so you can do something like:

while read line
do
NAME=`echo "$line" | cut -c11-31`
printf "%-10s%-21s%-10s\n" "111111111" "$NAME" "2222222222"
done file.new

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Robin Wakefield
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Hi,

or try this script:

===========================================
#!/bin/ksh

if [ $# -ne 2 ] ; then
echo
echo Syntax error: $0 old-file alias-file
echo
exit 1
fi

cat $1 | while read line ; do
echo $line | read ssn name1 name2 title junk
grep $ssn $2 | read ssn ssn_alias name1_alias name2_alias
if [ -n "$ssn_alias" ] ; then
printf "%-10s%-10s%-10s%-15s%-20s\n" $ssn_alias $name1_alias $name2_alias $title $junk
else
echo $line
fi
done > $1.new

echo Results stored in $1.new
===========================================

you'll have to adjust the formatting to your original values.

Rgds, Robin.
Bobbi Rawlings
New Member

Re: Newbie>>>How write a script to alter the data in a file??

Reply to Harry, Response 1: I could "vi" the file, but I'd have to do this multiple times for multiple db instances. So that would be a lot of typing!

Reply to Harry, Response 2: What do the 2.2, 1.2, etc mean?

Reply to Darrell: The ssn's and names do have to be different. Sorry.

Reply to Robin: The column names that I supplied in my description of my data are not actually a part of the data. I'm not a shell expert, but it seems that the ssn and the name will have to pulled somehow. Maybe as a substring? Thoughts?

Reply to All: Thanks so much, and please forgive my ignorance ;)



Darrell Allen
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Okay, here's 2 approaches...

$cat file
123456789 Jim Dandy Engineer 1234567890
111111111 Jim Dandy Accountant 1234567890

$cat alias
987654321 Jim Dandy Engineer 1234567890
222222222 Jim Dandy Accountant 1234567890

$cat alias2
123456789 987654321
111111111 222222222

# substitute Alias_SSN for SSN based on matching on NAME
while read line
do
SSN=`echo "$line" | cut -c1-9`
NAME=`echo "$line" | cut -c11-31`
OTHER=`echo "$line" | cut -c32-42`
AL_SSN=`grep "$NAME" alias | cut -c1-9`
printf "%-10s%-21s%-10s\n" $AL_SSN "$NAME" $OTHER
done file2

# substitute Alias_SSN for SSN based on matching on SSN
while read line
do
SSN=`echo "$line" | cut -c1-9`
NAME=`echo "$line" | cut -c11-31`
OTHER=`echo "$line" | cut -c32-42`
AL_SSN=`grep "$SSN" alias2 | cut -c11-19`
printf "%-10s%-21s%-10s\n" $AL_SSN "$NAME" $OTHER
done file2

You could add some if statements to use $SSN or log an error in case the Alias_SSN wasn't found.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
harry d brown jr
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Bobbi,

Sorry it took so long to get back, but with the site being down most of the day, oh well...

The JOIN command I posted:

join -o 2.2,1.2,1.3,1.4,1.5,1.6 file_orig file_alias > file_NEW

^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^
cmd---^ |---Output fields-----| |file 1-| |-file 2-| |outputfile|


Joins the two files, on the first field of each file, by matching them up. So when the SSN in "file_orig" matches the SSN in the "file_alias", then the "OUTPUT" options are taken. Those OUTPUT options 2.2, says this:

The "2" before the "." (period) is the file name, in this case "file_alias".

The "2" AFTER the "." (period) says output the SECOND field of "file_alias", which is the NEW SSN number to use.

The 1.2,1.3,1.4,1.5,1.6 say, output fields "2-6" from "file_orig".

I hope I made it somewhat clear?

live free or die
harry
Live Free or Die
Bobbi Rawlings
New Member

Re: Newbie>>>How write a script to alter the data in a file??

I really appreciate the help, but the suggestions are working for me. Someone told
me I can accomplish what I'm trying to do using a NAWK script. Sure. Like I know what THAT is.

Advice?
Bobbi
Robin Wakefield
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Hi Bobbi,

nawk is just a POSIX-standard (N)ew AWK, it's got extra functionality, including:

user-defined functions

more built-in functions

reading input from any file

various new array operations (testing membership, deleting elements, multi-dimensional arrays)

Whether these will help you is debatable, and you may well have the newer version installed anyway.

Your last post said things were working - is this correct, or are you still having problems.

Rgds, Robin
Bobbi Rawlings
New Member

Re: Newbie>>>How write a script to alter the data in a file??

I must have been dreaming if I said my script was working. But I found out why it wasn't. Apparently, my .profile is lacking the info it needs to be able to run ksh scripts. I don't have a problem running cshrc scripts though.

Thanks for the reply, Robin.
Darrell Allen
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Hi Bobbi,

I had hoped you could get enough ideas from the suggestions to put together what you need. Here's a script that will do what you ask:

#!/usr/bin/sh
echo "Alias_SSN Alias_Name Title Other Junk"
tail +2 file | while read line
do
SSN=`echo "$line" | cut -c1-9`
NAME=`echo "$line" | cut -c11-22`
TITLE=`echo "$line" | cut -c23-32`
OTHER=`echo "$line" | cut -c33-42`
AL_SSN=`grep "$SSN" alias | cut -c11-19`
AL_NAME=`grep "$SSN" alias | cut -c21-`
printf "%-10s%-12s%-10s%-10s\n" "$AL_SSN" "$AL_NAME" "$TITLE" "$OTHER"
done

I'll leave it to you to determine what everything is doing and what formatting changes you need to make for your actual input files. Your objective should be more than simply getting a script written for you. It should be to learn from the experience as well!

Happy scripting!
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Bobbi Rawlings
New Member

Re: Newbie>>>How write a script to alter the data in a file??

Thanks Darrell,
I'll try what you sent. I'm in a position where I HAVE to learn to do shell scripting. Any good book recommendations? So far I've been told O'Reilly has some really good books.
Darrell Allen
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Hi again,

Note the next to last line of my script should be at the end of the preceeding line (word wrap problem with this small reply window).

As far as books go, there are a number of threads on that topic. Search the ITRC (the link is at the top of this page on the left). Searching for "script books" should suffice.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Darrell Allen
Honored Contributor

Re: Newbie>>>How write a script to alter the data in a file??

Hi Bobbi,

Just looked at your forum profile and see that this was your first question posted. So...

Welcome to the forums! It's an invaluable resource for me. I expect it will be for you as well!

Now please don't take the following wrong :-)

I don't know if you've read any other postings but allow me to make a request. Once you solve your issue, add a note to this thread telling what worked for you. And if suggestions made helped you, please assign points to them (based of the relative level of help). There could even be multiple 10s if there were multiple solutions that worked.

People who search the forums later for the same issue will see which suggestions worked for you. It's also a nice way to say "thanks for helping" or even "thanks for trying" to those who took some time to try to help.

Again, welcome to the forums!

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)