awk for string manupulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk for string manupulation
# 1  
Old 12-04-2013
awk for string manupulation

Hi All,
I have one file with two columns separated by tab.

I need to search for second column value of this file in the 5 column of another file. If the match is found replace the 5th column of second file with entire row of the first file.

e.g.

file1
Code:
123    D.abc
234    D.rde
4563  D.wedr

file2
Code:
wqjksw     123213     dewdqwe    23123    D.abc    wswsw
wqjksw     123213    asasaasa     12345    D.abc
wqjksw     123213    asasaasa     12345    D.rde
wqjksw     123213     dewdqwe    23123    D.wedr    wswsw
wqjksw     123213     dewdqwe    23123    D.awec    wswsw

output file should be
Code:
wqjksw     123213     dewdqwe    23123    123    D.abc    wswsw
wqjksw     123213    asasaasa     12345    123    D.abc
wqjksw     123213    asasaasa     12345    234    D.rde
wqjksw     123213     dewdqwe    23123    4563  D.wedr    wswsw

Since the second file will be huge it would make sence if we first do search operation and the do replacement for each of the rows of first file.

I have writteh below code but its not working.

appreciate your help.

Code:
filearray=( `cat filter.txt | tr '\n'`)
 
rm temp1.txt outputfile.txt

for ((i=0; i<${#filearray[@]}; i++)) 
{
   awk ' if(${filearray[$i]} ~ /$5/) { print $0 }' datafile.txt >> temp1.txt
  awk ' {$5=${filearray[$i]} } temp1.txt >> outputfile.txt
}


Last edited by Franklin52; 12-04-2013 at 11:01 AM.. Reason: Please use code tags
# 2  
Old 12-04-2013
Code:
$ cat file1
123 D.abc
234 D.rde
4563 D.wedr

$ cat file2
wqjksw 123213 dewdqwe 23123 D.abc wswsw
wqjksw 123213 asasaasa 12345 D.abc
wqjksw 123213 asasaasa 12345 D.rde
wqjksw 123213 dewdqwe 23123 D.wedr wswsw
wqjksw 123213 dewdqwe 23123 D.awec wswsw

$ awk 'FNR==NR{A[$2]=$1;next}($5 in A){$4=$4 FS A[$5]}A[$5]' file1 file2
wqjksw 123213 dewdqwe 23123 123 D.abc wswsw
wqjksw 123213 asasaasa 12345 123 D.abc
wqjksw 123213 asasaasa 12345 234 D.rde
wqjksw 123213 dewdqwe 23123 4563 D.wedr wswsw

# 3  
Old 12-04-2013
Or try:
Code:
awk 'FNR==NR{A[$2]=$0; next} $5=A[$5]' file1 file2

or
Code:
awk 'FNR==NR{A[$2]=$1 FS $2; next} $5=A[$5]' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

4. Shell Programming and Scripting

How to do Date Manupulation in Korn Shell?

Hi All, I need to find the date 19days back from the current date: eg: if today is 17 March 2013 then the output should be : 26 Feb 2013 Can i do this using date command in Korn Shell? And also if i need future 15 days date from current date, how to that? Any help appreciated :) ... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

5. Shell Programming and Scripting

CDR manupulation

Hello Friends, I need to examine a huge CDR file according to a complex (at least for me) condition like below and i couldnt write anything :( In CDR file there are more than hundreds of fields, I need to print the rows which matches the below condition: while $13 field of subsequent... (9 Replies)
Discussion started by: EAGL€
9 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

8. AIX

Rows manupulation using AWK or sed

Hi Everyon, I am stuck in a script.I have a file named file1.txt as given below: It contains 2 columns-count and filename. cat file1.txt count filename 100 A_new.txt 1000 A_full.txt 1100 B_new.txt 2000 B_full.txt 1100 C_new.txt 2000 C_full.txt ................... ..................... (10 Replies)
Discussion started by: rajsharma
10 Replies

9. UNIX for Dummies Questions & Answers

records manupulation

I am sending the data in userfile and colfile from ksh script to pl/sql script linto an array with this command grep '' $userfile |awk '{print "my_user_id("FNR") := '$SQL_QUOTE'"$1"'$SQL_QUOTE';"}' >> $SQL_TEMP_FILE grep '^\{1,10\}$' $colfile | awk '{print "my_col_id("NR") := "$1";"}' >>... (0 Replies)
Discussion started by: pinky
0 Replies

10. Shell Programming and Scripting

Date Manupulation

HI all, I am relatively new to Unix Shell Scripts ... I want to know how u can calculate the differnece between the 2 dates. As if in Oracle by using SYSDATE u get current date and time .. How one can achieve it in Unix ? Thanks.. (1 Reply)
Discussion started by: dhananjaysk
1 Replies
Login or Register to Ask a Question