I am doing a shell script to be able to read some records from a file, then ask the user to input a name. The script should then be able to search the records and only print out the requested record.

I have figured out how to print the records but I do not know how to search and print only the requested data.

#5recordPrint
function print
{
clear
echo "Name, Position, Department, Salary"
echo "========================================"
awk 'BEGIN{FS=","; RS="\n"} {printf "%s, %s, %s, %s\n", $1, $2, $3, $4}' data.txt

}

Recommended Answers

All 5 Replies

http://www.ss64.com/bash/gawk.html

Eg.

awk -v find="Fred" 'BEGIN{FS=","; RS="\n"} 
        $1 == find {printf "%s, %s, %s, %s\n", $1, $2, $3, $4}' data.txt

Just replace the green code with whatever test you like.

thanks that helped alot

by the way, if i want to say delete a line of record, can i use the
tr -d?? Because I have been searching the Bash commands and havent found and delete command for shell scripts:(

This is a partial list of GNU text utilities
http://www.rtr.com/win95pak/textutils.htm

Then there is
grep / egrep / fgrep - file searching
sed - simple file manipulation
awk - more complex file manipulation
perl - the ultimate swiss army knife :)

ok so I tried this on my data file which contains records:
Name, Dept, Job, Pay
John, Finance, Accountant, $2000

read -p "Enter name of Employee to Remove?: " remEmp
tr -d "grep -n -i $remEmp $dataFile" >> $dataFile

Am I using the tr -d wrongly? Or do I just have some syntax errors? coz the script doesnt seem to work. I need to delete a whole line of record and the user just needs to enter a search parameter.

Yes, you're using tr wrongly.
tr just translates (or deletes) characters. Not lines, or things matching lines.

You could try

grep -v $remEmp $dataFile > temp
mv temp $dataFile

which finds everything EXCEPT (that's -v, invert) what matches the thing you want to remove.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.