Sorting an address string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting an address string
# 1  
Old 09-13-2008
PHP 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"
}
Minimum
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract IPv6 address from string?

Hi All, Would anyone know how to modify the below, so only the IPv6 address (red) is printed, please? (in other words, what's between inet6 and the / sign) ipv6=`/sbin/ifconfig lo0:5 inet6 | grep 'inet6'` print $ipv6 Currently the output of the above script is: inet6... (7 Replies)
Discussion started by: chatguy
7 Replies

2. Shell Programming and Scripting

Ip address sorting

Hello folks, I am sorting ip address file i have file having ips like 192.168.1.1 192.168.2.2 192.168.1.3 192.168.1.2 192.168.11.1 192.168.1.4 192.168.2.1 I have tried below code i=1 while : do echo $i (8 Replies)
Discussion started by: learnbash
8 Replies

3. Shell Programming and Scripting

string sorting in unix

Hi I need how to sort string characters for Example i have a file that contains this data example string "fan" but i want to display "afn" contained words "afn" is in sorted format for fan. File data faty gafny gaifny dafan gafnniunt O/p gafny gafnniunt (3 Replies)
Discussion started by: polineni
3 Replies

4. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

5. Shell Programming and Scripting

Sorting string with date and number

Hi, We have files coming in the system and we want to sort it in ascending order with date and sequence. The file pattern are inbound_crp_date_sequence.xml example we have file as below: inbound_crp_20100422_10.xml inbound_crp_20100422_2.xml inbound_crp_20100422_3.xml... (2 Replies)
Discussion started by: sreejitnair123
2 Replies

6. Emergency UNIX and Linux Support

TCL scripting - searching for a IP address in a string

Hi All, Can anyone please help me with the regular expression/code snippet to search for an IP address in a string with Tcl scripting. Example string "OSPF_NBRUP OSPF neighbor 16.138.181.15 (realm ospf-v2 e1-0/0/0:37.0 area 0.0.0.0) state changed from Full to Down due to KillNbr" In the... (6 Replies)
Discussion started by: Mr. Zer0
6 Replies

7. UNIX for Dummies Questions & Answers

sorting ASCII string containing numbers

I have the following output where I need to sort the second column numerically (starting with IBMULT3580-TD10 and ending in IBMULT3580-TD123) Drv DriveName 0 IBMULT3580-TD13 1 IBMULT3580-TD18 2 IBMULT3580-TD14 3 IBMULT3580-TD10 4 IBMULT3580-TD11 5 IBMULT3580-TD17 ... (8 Replies)
Discussion started by: GKnight
8 Replies

8. Shell Programming and Scripting

Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^ it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort. Please help me create the sub to pass to the sort function. (7 Replies)
Discussion started by: dangral
7 Replies

9. Programming

parsing a string to check if it's an IP address

Hello everybody! I woul quickly need a function bool ParseIPString(char*) that parses a string to check if it's in IP format. thanks in advance for any help! best regards, nadiamihu (1 Reply)
Discussion started by: nadiamihu
1 Replies

10. UNIX for Advanced & Expert Users

Sorting a string

Hello all, I have some 20-digit strings - say 1234567890abcdefghij I want to remove the 3rd and then the 11-18th strings, but leave a space between the two resulting strings eg "3 abcdefgh" I only know how to use "cut", does anyone know any way I can do this? All these strings are in the same... (14 Replies)
Discussion started by: Khoomfire
14 Replies
Login or Register to Ask a Question