09-13-2008
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"
}
10 More Discussions You Might Find Interesting
1. UNIX for Advanced & Expert Users
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
2. Programming
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
3. Shell Programming and Scripting
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
4. UNIX for Dummies Questions & Answers
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
5. Emergency UNIX and Linux Support
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
6. Shell Programming and Scripting
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
10. Shell Programming and Scripting
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
LEARN ABOUT MOJAVE
escape
escape(1) Mail Avenger 0.8.3 escape(1)
NAME
escape - escape shell special characters in a string
SYNOPSIS
escape string
DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result.
EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string:
$ var='; echo gotcha!'
$ eval echo hi $var
hi
gotcha!
$
Using escape, one can avoid executing the contents of $var:
$ eval echo hi `escape "$var"`
hi ; echo gotcha!
$
A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For
example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient:
#!/bin/sh
formail -x to -x cc -x resent-to -x resent-cc
| fgrep "$1" > /dev/null
&& exit 0
echo "<$1>.. address does not accept blind carbon copies"
exit 100
To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt
script:
bodytest reject_bcc `escape "$RECIPIENT"`
SEE ALSO
avenger(1),
The Mail Avenger home page: <http://www.mailavenger.org/>.
BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells.
AUTHOR
David Mazieres
Mail Avenger 0.8.3 2012-04-05 escape(1)