The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 07:11 AM
parsing a string to check if it's an IP address nadiamihu High Level Programming 1 03-22-2007 01:50 PM
Sorting a string Khoomfire UNIX for Advanced & Expert Users 14 01-18-2006 04:34 AM
How to Achive IP address through MAC(Ethernet) address krishnacins IP Networking 3 08-29-2005 09:45 PM
network address and broadcast address? pnxi UNIX for Dummies Questions & Answers 7 11-10-2003 11:29 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-13-2008
Minimum
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Thumbs down Sorting an address string

I'm in an introduction to Unix class and well I'm kind of stuck on one part of the lab for this week or shell scripts. Basically we're given a file named address.data and we're supposed to create a script to sort it according to zip code, last name, and first name (not at the same time of course). The guidelines for the finished script is:

1. Ask the user for the name of the data file.
2. Verify that the file exists and is readable.
3. If the file does not exist or is not readable, issue an error message and exit.
4. Sort the input file according to the instructions given above.
5. Use awk to print the sorted file according to the instructions given above.


Now I'm having trouble getting the user input to be verified since when I input address.data none of the echos display and it jumps to my case. The case doesn't send anything to the awk so that comes up blank as well but when I run a separate sh without the user input part as ./address.sh address.data the case works fine and sends the information to awk and it's displayed correctly. So my question is how do I get the user's input to work correctly at the start and to pass the data to the awk. Any ideas? My scripts are below.. and please excuse the lengthy post.

sortaddress.sh
#!/bin/sh
#
# This script was written by .
# The purpose of this script is to sort a
# file containing a single line address
# by the zip code, last name, and first
# name and then format it properly.
#
echo "Please enter a file to sort: \c"
read filename

for file in $2
do
if test -f $file
then
echo "$file is a file."
else
echo "$file is not a file."
fi
if test -r $file
then
echo "$file is readable"
else
echo "$file is not readable."
fi
done

echo "Please enter one of four of the follow selections:\n"
echo "1 to sort by zip code."
echo "2 to sort by last name."
echo "3 to sort by first name."
read choice

case $choice in
1) sort -k8 $1 | nawk -f sortadd.awk
exit;;
2) sort -k1 $1 | nawk -f sortadd.awk
exit;;
3) sort -k2 $1 | nawk -f sortadd.awk
exit;;
*) echo "Please enter a valid choice."
esac

sortadd.awk
BEGIN {
printf ("Your desired address sort is as follows: \n\n")
}

{
print $2 " " $1 " \n" $3 " " $4 " " $5 " \n" $6 " "$7 " " $8 " " $9 " \n\n"
}
Received Warning
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0