Easily search file and get line - little problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Easily search file and get line - little problem
# 1  
Old 10-27-2010
Data Easily search file and get line - little problem

hi,

file1.txt:
Code:
cell101 1 20.24.1.1 10
cell101 2 20.24.1.2 20
cell101 3 20.24.1.3 30
cell327 1 20.24.1.4 40
cell327 2 20.24.1.5 50
cell327 3 20.24.1.5 60
...

file2.txt:
Code:
cell101 1
cell327 1
cell327 3
...

according to values in file2.txt, the script should search lines and get values. what I want to see is:
Code:
IP is: 20.24.1.1 'sector 1 value 10'
IP is: 20.24.1.4 'sector 1 value 40'
IP is: 20.24.1.5 'sector 3 value 60'
...

and my code for this job is:
Code:
nawk 'NR==FNR{a[$1,$2]=$3;next} a[$1,$2]{print "IP is: "a[$1,$2]" '"'"'sector"$2" value "$4"'"'"'"}' /gc_sw/file1.txt /gc_sw/file2.txt

but I am seeing:
Code:
IP is: 20.24.1.1 'sector 1 value '
IP is: 20.24.1.4 'sector 1 value '
IP is: 20.24.1.5 'sector 3 value '
...

the values are not coming! Smilie $4 doesnot working in my code?? how can i do it?

thanks
# 2  
Old 10-27-2010
Try this,

Code:
awk 'NR==FNR{a[$1,$2]=$3;b[$1,$2]=$4;next} a[$1,$2]{print "IP is: " a[$1,$2] "'" '"'sector " $2 " value " b[$1,$2]"'"'"'"}' /gc_sw/file1.txt /gc_sw/file2.txt

OR
Code:
awk 'NR==FNR {a[$1$2]++;next} a[$1$2] { print "IP is: " $3  "'" '"'sector " $2 " value " $4 "'"'"'"}' /gc_sw/file2.txt /gc_sw/file1.txt

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 10-27-2010
pravin27, thanks dude. your second solution fails but first solution works well Smilie thanks for your assist
# 4  
Old 10-27-2010
Hi,

In my second solution I am reading file2 first and then file1. What error are you recieving ?
Code:
nawk 'NR==FNR {a[$1$2]++;next} a[$1$2] { print "IP is: " $3  "'" '"'sector " $2 " value " $4 "'"'"'"}' file2 file1

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 10-27-2010
Quote:
Originally Posted by pravin27
Hi,

In my second solution I am reading file2 first and then file1. What error are you recieving ?
Code:
nawk 'NR==FNR {a[$1$2]++;next} a[$1$2] { print "IP is: " $3  "'" '"'sector " $2 " value " $4 "'"'"'"}' file2 file1

oh pravin, you are right! it is ok now Smilie working perfectly!! thanks dude
# 6  
Old 10-27-2010
Code:
while read line ; do grep "$line" file1.txt | read a a b c ; eval echo "IP is: $b 'sector $a value $c'" ; done<file2.txt

This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 10-27-2010
Code:
awk 'NR==FNR && NF{a[$1$2];next}$1$2 in a{printf "IP is: %s %csector %d  value  %d%c\n",$3,x,$2,$4,x}' x=039 file2 file1

This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

4. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

5. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. UNIX for Advanced & Expert Users

How Can I Easily Determine If A File Has been Added to a Directory

I am uploading files that need to be processed prior to uploading. I will put the files in a directory. My question is how can I write an easy process to kick off a script once a file has been added? Is there an easy way to determine if a file has been added to a directory? Thanks (7 Replies)
Discussion started by: goodmis
7 Replies

9. UNIX for Dummies Questions & Answers

How Can I Easily Determine If A File Has been Added to a Directory

I am uploading files that need to be processed prior to uploading. I will put the files in a directory. My question is how can I write an easy process to kick off a script once a file has been added? Is there an easy way to determine if a file has been added to a directory? Thanks (1 Reply)
Discussion started by: goodmis
1 Replies

10. Shell Programming and Scripting

How to generate a configuration file easily and effectively?

Hello Unix Gurus, I have a query , like how to generate a configuration file easily using unix shell scripts?, I have set of parameters (based on the test that user wishes to run) for which user needs enter their input, so that a config file gets generated based on their input before running... (0 Replies)
Discussion started by: sarathytcs
0 Replies
Login or Register to Ask a Question