Operating System - HP-UX
1833159 Members
2998 Online
110051 Solutions
New Discussion

How to grep a string in particular colom?

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

How to grep a string in particular colom?

Hi,

I am having two files.

Files1 is having entries like:

Region
"101"
"102"
"103"
and so on. Having unique records.

Other file:

File2:

Region site description
"101","ddd","this is region 101"
"101","ddc","this was region"
"102","dda","this gg region102"
"102","ddn","this oo region"
and so on(Having dulicate region values)".


I want to take record one by one from file 1 and grep only in first fileld of file2 and output the results in other file.

Can some suggest how to do this?

Thanks


11 REPLIES 11
Delcho Tuhchiev
Frequent Advisor

Re: How to grep a string in particular colom?

Hi,

you can use "awk" to take only the first column from the file.
Use something like this:

awk -F"," '{ print $1 }' path_to_file2

where "," is the separator between the columns
Dennis Handly
Acclaimed Contributor
Solution

Re: How to grep a string in particular colom?

You can do the following. It will be slow if file1 has lots of records.
while read file1; do
echo "Looking for: $file1"
awk -F, -v PAT="$file1" '
$1 ~ PAT { print $0 }' File2
done < File1

If you have lots of records, you need to do vector/quantum methods.
N Gopal
Frequent Advisor

Re: How to grep a string in particular colom?

Hi,

I am agin simplyfying my question.

My code should be like.

while read line

do

grep "$line" file2 >> output.txt

done < file1

i want to something like that. But i want to grep region value in first field of file 2 (because that string may be present in some other colom also that i do not want to take.)and out complte line corresponding to that in out put file.

We can say files are of normal size then can you suggest how to do.

out file file should have record like :
"101","ddd","this is region 101"
"101","ddc","this was region"
"102","dda","this gg region102"

Thanks
Dennis Handly
Acclaimed Contributor

Re: How to grep a string in particular colom?

>We can say files are of normal size then can you suggest how to do.

I suggested how to do it. If you don't want the title you can do:
awk -F, -v PAT="$file1" '
BEGIN { getline }
$1 ~ PAT { print $0 }' File2

You should leave out that debugging "echo".
N Gopal
Frequent Advisor

Re: How to grep a string in particular colom?

Many thanks Dennis,

It is working fine.
Can i redirect the out put to output file as i want this?

while read file1; do
echo "Looking for: $file1"
awk -F, -v PAT="$file1" '
$1 ~ PAT { print $0 }' File2 >> output.txt
done < File1

Thanks
N Gopal
Frequent Advisor

Re: How to grep a string in particular colom?

Hi Dennis,

Applogies one more clarification.
In this case i am greping string in first field in File2. If would be 3field in File2 then where i have to make changes.

Thanks
James R. Ferguson
Acclaimed Contributor

Re: How to grep a string in particular colom?

Hi:

> In this case i am greping string in first field in File2. If would be 3field in File2 then where i have to make changes.

Then, change:

$1 ~ PAT { print $0 }' File2 >> output.txt

To:

$3 ~ PAT { print $0 }' File2 >> output.txt

'awk' numbers fields counting from one (1) with '$0' representing the whole line (record).

Regards!

...JRF...

Sandman!
Honored Contributor

Re: How to grep a string in particular colom?

If your files are sorted then you can simply use join(1) to get the desired output.

# join -t, -j1 1 -j2 1 -o 2.1 2.2 2.3 file1 file2 > output.txt

...otherwise sort file1 and file2 before using them in a join(1) viz.,

# sort -k1,1 file1 > srtd1
# sort -t, -k1,1 file2 > srtd2
# join -t, -j1 1 -j2 1 -o 2.1 2.2 2.3 srtd1 srtd2 > output.txt
Dennis Handly
Acclaimed Contributor

Re: How to grep a string in particular colom?

>Can i redirect the out put to output file as i want this?

while read file1; do
...
done < File1 > output.txt

You can redirect the whole "while" output rather than opening the file once per loop.

>If would be 3 field in File2 then where i have to make changes.

You would have problems because of the quotes. They would have to be removed:
BEGIN { getline; gsub("\"", "", PAT) }



N Gopal
Frequent Advisor

Re: How to grep a string in particular colom?

Hi All,

Thanks a lot for your valuable replies. I got my answer.

Thanks again.

N Gopal
Frequent Advisor

Re: How to grep a string in particular colom?

I got satisfactory answer for my query.

Thanks a lot for help.