Need to select some column from file1 if condition match.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to select some column from file1 if condition match.
# 1  
Old 07-04-2013
Need to select some column from file1 if condition match.

I have two files file1.txt and file2.txt.

want to print some column(number,status,date1,date2,description(descrption column end before category column start with'Oracle Services') and assigned_to column from file1.txt ...If the assigned_to name in file2.txt matches with assinged_to name in file1.

Code:
file1.txt
----------

number status  date1  date2     description                                              category                              type                 service  assigned_to     reported_to  envioment 
=====  ====   ===      =====    =============                                             =============                        =====             =======  =========     ======== ======
34567 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading init Oracle Services - Trouble Tk Ticket, Auto ‚àö DBA_SERVICE chadda,Deepak kumar  DBA PROD
45678 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Sizing Oracle Services - Trouble Tk Ticket, Auto ‚àö DBA_Ticket chadda,Deepak kumar  DBA TEST
43567 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info Oracle Services - Trouble Tk Ticket, Auto ‚àö  DBA_Group chadda,Deepak kumar  DBA QUALITY
24578 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn Oracle Services - Trouble Tk Ticket, Auto ‚àö  DBA_GrouP Sharma,Vijay Kumar  DBA PROD
45890 open 28/06/13 28/06/13  nl21a00is-centerdb001:testingQA:FSFO has configuration errors Oracle Services - Trouble Tk Ticket, Auto ‚àö  SA_SERVICE Reddy,Ajay rao   DBA PROD
43599 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info Oracle Services - Trouble Tk Ticket, Auto ‚àö  DBA_Group Agarwal,ravi kumar  DBA QUALITY
25578 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn Oracle Services - Trouble Tk Ticket, Auto ‚àö  DBA_GrouP Pandit,Ashok Kumar  DBA PROD
51890 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading init Oracle Services - Trouble Tk Ticket, Auto ‚àö  SA_SERVICE Reddy,Ajay rao   DBA PROD

Code:
file2.txt
----------
Assigned_to
===========
Sharma,Vijay Kumar
Soni,Amit Kumar
Rajput,Anupam 
chadda,Deepak kumar
Thakral,Raj Singhalese
Sahu,chanchal ram
Reddy,Ajay rao

Code:
desirous output 

34567 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading init chadda,Deepak kumar 
45678 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Sizing chadda,Deepak kumar 
43567 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info chadda,Deepak kumar 
24578 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn Sharma,Vijay Kumar 
51890 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading init Reddy,Ajay rao

# 2  
Old 07-04-2013
Your input file is really badly delimited... Anyway try this:
Code:
awk 'NR==FNR&&NR>2{a[$0]=1}NR!=FNR{for (i in a) if ($0~i) {x=match($0,"Oracle Services");print substr($0,0,x-2),i}}' file2.txt file1.txt

# 3  
Old 07-04-2013
Quote:
Originally Posted by bartus11
Your input file is really badly delimited... Anyway try this:
Code:
awk 'NR==FNR&&NR>2{a[$0]=1}NR!=FNR{for (i in a) if ($0~i) {x=match($0,"Oracle Services");print substr($0,0,x-2),i}}' file2.txt file1.txt



This looks good. can you please explain me this code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Match part of string in file2 based on column in file1

I have a file containing texts and indexes. I need the text between (and including ) INDEX and number "1" alone in line. I have managed this: awk '/INDEX/,/1$/{if (!/1$/)print}' file1.txt It works for all indexes. And then I have second file with years and indexes per year, one per line... (3 Replies)
Discussion started by: phoebus
3 Replies

4. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

5. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Match one column of file1 with that of file2

Hi, I have file1 like this aaa ggg ddd vvv eeeand file2 aaa 2 aaa 443 xxx 76 aaa 34 ggg 33 wee 99 ggg 33 ddd 1 ddd 10 ddd 98 sds 23 (4 Replies)
Discussion started by: polsum
4 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

how to match a numers from file1 to file 2??Ps help me..

Hi firends, I need to write a program in perl for following task, I have a data like following numbers in file1.txt 1061338 1159781 1161717 1920355 1924487 2996733 3121872 3124510 I have to search the matching numbers in file2,my file2 is have a multiple columns like this ... (6 Replies)
Discussion started by: sureshraj
6 Replies

9. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question