- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Newbie>>>How write a script to alter the data ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2002 11:18 AM
03-04-2002 11:18 AM
Newbie>>>How write a script to alter the data in a file??
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2002 11:25 AM
03-04-2002 11:25 AM
Re: Newbie>>>How write a script to alter the data in a file??
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2002 11:35 AM
03-04-2002 11:35 AM
Re: Newbie>>>How write a script to alter the data in a file??
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2002 01:31 PM
03-04-2002 01:31 PM
Re: Newbie>>>How write a script to alter the data in a file??
while read line
do
NAME=`echo "$line" | cut -c11-31`
printf "%-10s%-21s%-10s\n" "111111111" "$NAME" "2222222222"
done
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2002 12:12 AM
03-05-2002 12:12 AM
Re: Newbie>>>How write a script to alter the data in a file??
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2002 11:53 AM
03-05-2002 11:53 AM
Re: Newbie>>>How write a script to alter the data in a file??
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 ;)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2002 01:27 PM
03-05-2002 01:27 PM
Re: Newbie>>>How write a script to alter the data in a file??
$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
# 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
You could add some if statements to use $SSN or log an error in case the Alias_SSN wasn't found.
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2002 06:12 PM
03-05-2002 06:12 PM
Re: Newbie>>>How write a script to alter the data in a file??
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 05:35 AM
03-13-2002 05:35 AM
Re: Newbie>>>How write a script to alter the data in a file??
me I can accomplish what I'm trying to do using a NAWK script. Sure. Like I know what THAT is.
Advice?
Bobbi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 06:07 AM
03-13-2002 06:07 AM
Re: Newbie>>>How write a script to alter the data in a file??
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 06:14 AM
03-13-2002 06:14 AM
Re: Newbie>>>How write a script to alter the data in a file??
Thanks for the reply, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 06:19 AM
03-13-2002 06:19 AM
Re: Newbie>>>How write a script to alter the data in a file??
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 06:34 AM
03-13-2002 06:34 AM
Re: Newbie>>>How write a script to alter the data in a file??
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 07:17 AM
03-13-2002 07:17 AM
Re: Newbie>>>How write a script to alter the data in a file??
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2002 10:32 AM
03-13-2002 10:32 AM
Re: Newbie>>>How write a script to alter the data in a file??
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