0

Hi everyone I have a file called Halls Engines in the directory Halls.

So the directory structure looks something like

Halls/Halls Engines

The file Halls Engines contains the following information:

Brown,John,24000

Brown,Susan,26000

Smith,Jill,24000

I'm trying to write a script that takes in two parameters - family name and file name which isn't working too well.

The final code should print out an initial line containing the file name and family name and the given name and salary of all those with the specified family name.

This is the code I've written so far:

cd Halls
for filename in $2
do
echo "Subsidiary=$2 Family Name=$1"
awk -F, '$1 ~/$1/{print $1 " has a salary of " $3}'
done

So this is how I would run the script (saved as ss1):

./ss1 Brown "Halls Engines"

This should return something:

Brown has a salary of £24000

Brown has a salary of £26000

But I get no output past the echo statement. Any advice on how I can sort this problem out?

5 Answers 5

3

In awk you need to define a variable and reference it to the bash variable. Something like this -

#!/bin/bash

name="$1"
shift

for filename in "$@"
do 
awk -v FS=',' -v gname="$name" '$1~gname{print $1 " has a salary of "$3}' "$filename"
done

Test:

[jaypal:~/Temp] cat data
Brown,John,24000
Brown,Susan,26000
Smith,Jill,24000

[jaypal:~/Temp] cat data1
Brown,Charlie,64000
Brown,Sam,46000
Smith,Jill,24000

[jaypal:~/Temp] ./s.awk Brown data data1
Brown has a salary of 24000
Brown has a salary of 26000
Brown has a salary of 64000
Brown has a salary of 46000
Sign up to request clarification or add additional context in comments.

Comments

3

Or you could also use the one liner

grep $1 "Halls/""$2" | awk -F , '{print $1 " has a salary " $3}'

In action:

shadyabhi@archlinux /tmp $ cat Halls/Halls\ Engines 
Brown,John,24000
Brown,Susan,26000
Smith,Jill,24000
shadyabhi@archlinux /tmp $ ./ssl.sh Brown "Halls Engines"
Brown has a salary £24000
Brown has a salary £26000
shadyabhi@archlinux /tmp $ 

Comments

2
# Your awk line:
#   awk -F, '$1 ~/$1/{print $1 " has a salary of " $3}'
#            ^    ^- You want this one to be interpreted, but it's single-quoted.
#            ^- Correctly not interpreted.

# Either escape the $s that awk should see:
awk -F, "\$1 ~/$1/{print \$1 " has a salary of " \$3}" "Halls/$2"

# Or change to double quotes for them.
awk -F, '$1 == "'"$1"'"{print $1 " has a salary of " $3}' "Halls/$2"

Comments

2

Is it a typo, or did you leave out the filename as input to awk?

edit OK, now $1 from shell is different than $1 from awk. See changes below.

cd Halls
for filename in $2
do
   echo "Subsidiary=$2 Family Name=$1"
   awk -F, '$1 ~ /'"$1"'/{print $1 " has a salary of " $3}' $filename
# ---------------^^^^^^^------------------------------------^^^^^^^^^^^
done

I hope this helps.

3 Comments

cd Halls for filename in $2 do echo "Subsidiary=$2 Family Name=$1" fname=$1 awk -F, '$1 ~ /$fname/{print $1 " has a salary of " $3}' $filename done I've typed this in and keep getting errors!
@methuselah : See edit. In the future, edit your question to include the code you are running AND the error messages you are getting. Good luck!
methuselah : Thanks for the accept check mark, but I'm pretty sure @Kevin had it right in his post originally. I had the first answer, but I definitely missed the '"$"' thing on my fist version of the answer. (I didn't notice his 2nd answer was almost identical to my fixed answer until after I had fixed mine). Please restore the chk-mark to Kevin. Good luck to all!
1

Try this one, hope I've got your task right:

#!/bin/bash

while read line; do
  echo "$1,$line" | awk -F, '{ if ($1 == $2) print $2 " has a salary of " $4 }'
done < $2

I've made a file "halls_e" with those three entries of yours and runned it:

~# ./halls.sh Brown "halls_e"
Brown has a salary of 24000
Brown has a salary of 26000
~# ./halls.sh Smith "halls_e"
Smith has a salary of 24000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.