Brat 20 x 1000 32rf
Pour 15 p 1621 05pr
Dart 10 z 1111 22xx
My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour"
I currently have this line but it is not giving an output.
---------- Post updated 07-31-11 at 12:23 PM ---------- Previous update was 07-30-11 at 10:59 PM ----------
I have a new problem regarding reading a specific line and a specific field. But now I want to replace the first two fields. I tried this because it looked reasonable:
sed -i "s/$searchName\ $2/$name\ $lname/g" database.txt
This works for me assuming that searchName == John, $2 == Wall. If you've invoked your script with just John on the command line, $2 will be null, then you'll end up with the results that you posted. The small script I wrote to test your code includes some error checking that might be beneficial in your case so I've posted it as an example.
#!/usr/bin/env ksh
if [[ $# != 4 ]]
then
echo "error: missing parms. expected: name lastname newname new-lastname"
exit 1
fi
searchName=$1
# $2 is not assigned and used directly later
name=$3
lname=$4
sed "s/$searchName\ $2/$name\ $lname/g" test.data
I would recommend assigning $2 to a named variable; while it is fine the way it is, it makes your script easier to read if it were something like this:
sed "s/$searchName\ $searchLastname/$name\ $lname/g" test.data
I also don't believe that you need to escape the spaces. This should work:
sed "s/$searchName $2/$name $lname/g" test.data
and again makes the code easier to read.
---------- Post updated at 13:13 ---------- Previous update was at 13:10 ----------
Thought I would also note that I dropped the -i from my testing because the version of sed that I prefer does not implement it. It should work just fine if you prefer to use that option.