search pattern present in second field

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search pattern present in second field
# 1  
Old 08-20-2009
search pattern present in second field

Hi All,

I have a file with following list.

example 1
cat 1.txt

Also i have another file 2.txt in which the data is in this format as shown:

cat 2.txt


Now i wrote script to take each pattern from 1.txt and grep in 2.txt and if the result matches then redirect to 3.txt

for i in `cat 1.txt`
  grep "$i" 2.txt >>3.txt

But my requirement is to grep for a given pattern which should be present in the second field of the 2.txt then only print it.

example 2


If you notice i placed a hypen --- at the start and end of the pattern to be searched.

I want this kind of result but not the one which is shown in my first example.

Your help is much appreciated.


Last edited by Franklin52; 08-20-2009 at 05:35 AM.. Reason: Please use code tags!
# 2  
Old 08-20-2009
something like this :

for i in `cat 1.txt`
awk -F"|" -v ser="$i" '$2 ~ ser { print }' 2.txt  >> 3.txt

# 3  
Old 08-20-2009
No it gives me the following error

awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 08-20-2009
Use nawk or gawk instead.
# 5  
Old 08-20-2009
Hi Panyam,

Thank you, Thank you, Thank you very much.
Yes, finally i got the expected output.

But, Please please please do let me know which books i refer from beginner, intermediate and advance level in scripting so that i can be more independent and help dumb ppl like me who are trying to convey that unix is more user friendly, fastest and reliable O/S.

Please do post your reply.

Iam eagerly waiting for your reply.

# 6  
Old 08-20-2009

Hope if you google you will get more and more links and resources.

I really don't have any idea on the best books , but for awk/sed these links

are useful.


# 7  
Old 08-20-2009
Hi Panyam,

Thanks for your time and suggestion for the awk/sed links.
Definitely i will try to improve my knowledge.

Once again thanks to you and all unix forum members.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

How to remove content present in between specific pattern ?

Hi, I have a file with following pattern. We are looking to filter out only specific content from this file. sample BLAdmins Server.* LinuxAdmins Server.* Policy Name: Recommended Default ACL Policy Everyone ACLPushJob.Read Everyone ACLTemplate.Read Everyone ... (9 Replies)
Discussion started by: Litu19
9 Replies

3. Shell Programming and Scripting

Search, and add if present

Dear All, I have to find a way to reorganize a table file according to the last column. The input file looks like this: cat Input1.txt: ID:12:23:00Q EU232 2342 234 123 231 aa1;ab2 ID:11:22:00E EU112 1232 211 112 233 ab2;ac3 ID:19:24:00S EU121 569 ... (7 Replies)
Discussion started by: loba
7 Replies

4. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

5. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

Check whether the pattern is present or not?

I want to determine whether a specific pattern is present within a line or not e.g. The whole line is in a varaible called VALUE VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)" i want to set a flag to 1 if i find the presence of ABC in the above variable. Please... (8 Replies)
Discussion started by: skyineyes
8 Replies
Login or Register to Ask a Question