1832218 Members
1850 Online
110041 Solutions
New Discussion

script query

 
Ravinder Singh Gill
Regular Advisor

script query

fullnames are in file1. In file2 I have shortnames. What I am trying to do is match the shortnames in file2 to the first six characters of fullname in file1. I want to output the matched ones to file3. Why does the following not work?



while read fullname
do
shortname=$ echo $fullname | cut -c1-5
grep $shortname file2

if
[ $? = 0 ]
then
echo $fullname >> file3
fi

done < file1
13 REPLIES 13
Patrick Wallek
Honored Contributor

Re: script query

This line:
shortname=$ echo $fullname | cut -c1-5

Should be this:
shortname=$(echo $fullname | cut -c1-5)

Your 'if [ $? = 0 ]' needs to all be on one line as well. I suspect it is in your script, but the Forums screwed it up when you posted it.

john korterman
Honored Contributor

Re: script query

Hi,

try this:

while read fullname
do
shortname=$( echo $fullname | cut -c1-5)
grep -q $shortname file2

if [ $? = 0 ]
then
echo $fullname >>file3
fi

done < file1


regards,
John K.
it would be nice if you always got a second chance
Sandman!
Honored Contributor

Re: script query

IMHO...you can reverse the algorithm inside your script, search the shortnames in file2 to the fullnames in file1 w/o taking the the first six characters of file1 into account. Something like...

# cat file2 | while read shortname
> do
> grep "^$shortname" file1 > file3
> done

thanks!
Muthukumar_5
Honored Contributor

Re: script query

Use as,

# awk '{ var=substr($1,0,6);print var; }' file1 | while read sname;
do
grep $sname file2 >> file3
done

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script query

Sandman,

Your logic of,

# cat file2 | while read shortname
> do
> grep "^$shortname" file1 > file3
> done

is good. However with problem.

# Problem 1:

grep "^$shortname" file1 > file3
Change this to append mode as,
grep "^$shortname" file1 >> file3

# Problem 2:
grep "^$shortname" file1
It will log file1 contents to file3. We can change as,

grep -q '^$shortname" file1
[[ $? -eq 0 ]] && echo $shortname >> file3

# Problem 3:

Change cat file2 to as,

while ..
do
..
done < file

Will be more good.

# Use as,

rm -f file3
while read shortname
do
grep -q "^$shortname" file1
[[ $? -eq 0 ]] && echo "$shortname" >> file3
done < file2

hth.

Easy to suggest when don't know about the problem!
Ravinder Singh Gill
Regular Advisor

Re: script query

I tried Muthukumar's suggestion but file three was coming out blank.

I tried Patrick Wallek's suggestion but no file3 was produced.
Ravinder Singh Gill
Regular Advisor

Re: script query


Muthukumar, before I use your following script:

# awk '{ var=substr($1,0,6);print var; }' file1 | while read sname;
do
grep $sname file2 >> file3
done

can you please explain what the awk line is exactly doing? I am new to scripting and have no experience of awk.

Thanks
Frank de Vries
Respected Contributor

Re: script query

Try and keep it simple and short:

while read fullname
do
shortname=$(echo $fullname | cut -c1-5)
if (grep -i $shortname file2)
then
echo $shortname >> file3
fi
done < file1

I supposed you wanted to output shortname to file3 and not fullname to file3 , as these
are already in file1 right !
Look before you leap
Ravinder Singh Gill
Regular Advisor

Re: script query

I want to output fullname to file3, but only those fullnames which have a matching shortname existing in file2.
Ravinder Singh Gill
Regular Advisor

Re: script query

Every shortname has a fullname, but not every fullname has a shortname, hence I wish to identify the appropriate fullnames for the shortnames (a few thousand) and put them in file3.
john korterman
Honored Contributor

Re: script query

Hi,

since none of your suggestions have produced the correct result, you should perhaps supply some authentic input material: one problem could be that a shortname, equal to the first 6 chars of a fullname, will have more than a single match in fullname, e.g.:

file_of_fullnames
Stevens, Shaking
Stevenson, Robert Louis

The shortname for both, "Steve" will have two hits - and they are a bit apart, literally speaking...

Are the shortnames unique?

regards,
John K.


it would be nice if you always got a second chance
Sandman!
Honored Contributor

Re: script query

Hello Ravinder,

Have you tried my suggestion? It does exactly what you're looking for:

# cat file2 | while read shortname
> do
> grep "^$shortname" file1 >> file3
> done

This will output only matching longnames into file3.

hope it helps!!!
Frank de Vries
Respected Contributor

Re: script query

Ok,
I see what you mean:
This will do the trick for you:

while read fullname
do
shortname=$(echo $fullname | cut -c1-5)
if (grep -i $shortname file2)
then
echo $fullname >> file3
fi
done < file1

good luck

Look before you leap