Operating System - Linux
1748136 Members
3774 Online
108758 Solutions
New Discussion юеВ

Re: Find the String-need help in Script

 
Karthick K S
Frequent Advisor

Find the String-need help in Script

HI all,

I have a file abc.txt,i want to search a word like abc in 5the column and 8th row,once i find that word,i should compare that string(find the string) to another file xyz.txt,once it match the abc.txt wil move to some other folder like /tmp --need script for help
7 REPLIES 7
Hein van den Heuvel
Honored Contributor

Re: Find the String-need help in Script


Here is how to get a list of files that contain a string found in an other file on a viven line, in a given column:


grep -l `awk '(NR==8){print substr($0,5,3)}' abc.txt` *.txt

I'm sure you can take it form there...
(Oh... and it will find abc.txt :-)

Hein.
Karthick K S
Frequent Advisor

Re: Find the String-need help in Script

Thanks for your script,

But I Once i find that abc word from abc.txt then i will find that word in xyz.txt to get the variable

for eg:
In xyz.txt
xyz=abc

i want to output abc
Hein van den Heuvel
Honored Contributor

Re: Find the String-need help in Script



Well , it is not enitrely clear to me what syntax/filenames you can rely on>
An answer to your last question might be:

grep `awk '(NR==8){print substr($0,5,3)}' abc.txt` xyz.txt | cut -f 2 -d=

Here is what that does for me:

> cat x
wergewrgwehrweh
rthwrthrwthrwth
etrrgertgwetrgw
122345XXX124253
fw34t23t2yt5425y
> cat y
erfer=1244
XXX=test
agrwe
> grep `awk '(NR==8){print substr($0,7,3)}' x` y | cut -f 2 -d=

test


If this does not get you going, then you may want to re-reply with (a text attachment with) a sample input file set and sample output action.


Hein.

Karthick K S
Frequent Advisor

Re: Find the String-need help in Script

Ok,Thanks one more loop i need to search
eg:

abc.txt
THe
Luck to the word
jl abc llk

xyz.txt
Land ...
abc = test

i want to take a string abc from abc.txt then if that string(abc) is present in xyz.txt then output of abc will diplay(Output:test) then the file abc.txt will move to /tmp folder
Muthukumar_5
Honored Contributor

Re: Find the String-need help in Script

You can do it as,

if [[ $(awk '(NR==8) { print $5; }' abc.txt) = "abc" ]]
then
awk -F"=" '/abc/ { print $2 }' xyz.txt
mv abc.txt /tmp
fi

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

Re: Find the String-need help in Script

====
i want to take a string abc from abc.txt then if that string(abc) is present in xyz.txt then output of abc will diplay(Output:test) then the file abc.txt will move to /tmp folder
====

If you want to search abc anywhere in abc.txt and print Ouput: test then,

awk -F"=" '/abc/ { print "Ouput:"$2 }' xyz.txt
mv abc.txt /tmp

hth.
Easy to suggest when don't know about the problem!
Karthick K S
Frequent Advisor

Re: Find the String-need help in Script

Thanks Everyone to give the solution my script is working