Problem with the awk in searching the contents in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with the awk in searching the contents in a file
# 1  
Old 04-04-2012
Problem with the awk in searching the contents in a file

Hello all,

I am a newbie in awk. I am struggling in this problem for a long.Actually I have two files, filea and fileb. File a is actually a search key through it I have to find the corresponding japanese tag from file b.

filea contains the data like this:
Code:
sm982882  sm1893548
sm2420025 sm2420128
sm2420025 sm1654558
sm1715919 sm3504435
sm1050729 sm1049371
sm1200230 sm1783839

and fileb contains the data like this:
Code:
バーストリミット    147    sm1864839;sm1878115;sm1950540;sm2106734;sm2178096;sm2183582;sm2415525;sm982882;sm2512481;sm2554063;sm2652509;sm1893548;sm2723097;sm2785830;sm2898082;
2ちゃんねる    513    sm479;sm36902;sm41429;sm43160;sm68724;sm72831;sm1654558;sm153858;sm155799;sm157569;sm162299;sm168973;sm171423;sm174780;sm176263;sm181722;sm185763;sm210482;sm215266;sm218312;sm224020;sm232251;

The result of output should be the like this:

Code:
バーストリミット  sm982882  sm1893548

I use the following way but it does not produce any result:
Code:
awk ' {col1=$1; col2=$2;} END{if($3~/col1/&&/col2/) print $1 col1,col2;}' filea fileb

.

Any help in this will be highly appreciable.

Best,
MohanSmilie

Moderator's Comments:
Mod Comment Hello Welcome to UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-04-2012 at 05:18 AM..
# 2  
Old 04-04-2012
Hi, you were on your way, but you need to distinguish the phases in which you are processing filea or fileb. When NR is equal to FNR, awk is processing the first file which in this case is fileb. There is no input processing in the END section.
Code:
awk 'NR==FNR{A[$3]=$1;next}{for(i in A)if(i~$1"\;" && i~$2"\;") print A[i],$0 }' fileb filea

Code:
バーストリミット sm982882  sm1893548


Last edited by Scrutinizer; 04-04-2012 at 03:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-04-2012
Code:
# awk -F'[ ;]' 'NR==FNR{a[$1]=$2;next}{for(i=1;i<=NF;i++)if(($i in a))for(j in a){print $1,j,a[j];next}}' filea fileb
バーストリミット sm982882 sm1893548

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 4  
Old 04-04-2012
Thank you both Scrutinizer and ygemici, my problem is solved :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem to grep contents from a file

hi, I'm trying to grep content from one file in another file. The file that I'm searching into is large and hence I need to temporarily unzip it first. gzip -dc ALL.chr2.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz | grep '169997024\|190670539\|128051369' >... (2 Replies)
Discussion started by: janshamsani
2 Replies

2. Shell Programming and Scripting

Searching the pattern and accordingly changing the contents using shell

HI, I have File1 which contains :- admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal nonadmins = read-only @admins = rw @nonadmins = r One shell script, using that I want to change the File1 as per user input (let's say $1) which have value as 'John', so now I want to... (6 Replies)
Discussion started by: Vishal Mishra
6 Replies

3. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

4. Shell Programming and Scripting

Problem with searching and then editing a file through shell.

Hi, I have searched through this forum as there are many similar entries but could'nt get one of them to work, either that or they were just different to what I needed. Basically I have a file, recordsDatabase. In this file are a few different fields. There is a unique identifier eg 001... (5 Replies)
Discussion started by: U_C_Dispatj
5 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

Searching a file using awk or sed

I want to search a file in a specific location and I don't want to use find command. I want to give the path also where the file is for searching it. Pls help (3 Replies)
Discussion started by: maitree
3 Replies

7. Shell Programming and Scripting

problem while searching in a file by 'AWK'

Hi , i am writing a script in which i am using following command to grep some string and then storing it's output to v, as like below :- v=$(awk -F, '{ if ( $NF ~ /DEV/ ) print $0 "_BLD01";else print $0 "_RC01" }' mytest) Here i am facing following issues:- 1. it is searcing DEV in the... (1 Reply)
Discussion started by: inderpunj
1 Replies

8. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

9. Shell Programming and Scripting

searching and editing file contents

Can you please help me to edit parts of a file and write into a new file. ===================================== Suppose I have a huge data dump in a file I need to search for a tag in that and cut few lines around that tag in the file. Is there a way to keep track of line numbers and operate on... (18 Replies)
Discussion started by: jayana
18 Replies

10. UNIX for Advanced & Expert Users

Searching contents of a file

Is there a way a command or a combination through which i can check the contents of a all files in a directory and get the return as the file names which contains the partiuclar string. (2 Replies)
Discussion started by: thepitzaboy
2 Replies
Login or Register to Ask a Question