Awk:substitution of characters between two file elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk:substitution of characters between two file elements
# 1  
Old 10-05-2013
Awk:substitution of characters between two file elements

Hi,

I have file1 like this:

Code:
a 64
b 66
c 67

and file2 like this:
Code:
@1234
1123
aabbcc
@5453
5543
ccba

I want to replace each letter of the third line in file2 with corresponding number in file1. So desired output is,
Code:
@1234
1123
646466666767
@5453
5543
67676664

I tried something like this but not working

Code:
awk 'NR==FNR{a[$1]=$2;next}{if(i in a) gsub(/i/,a[i]); print }' file1 file2

thanks in advance
# 2  
Old 10-05-2013
Try:
Code:
awk 'NR==FNR{a[$1]=$2;next}!(FNR%3){n=split($0,x,"");$0="";for (i=1;i<=n;i++)$0=$0""a[x[i]]}1' file1 file2

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-05-2013
Code:
awk 'NR==FNR{a[$1]=$2;next}{for (i in a) gsub(i,a[i]); print }' file1 file2

This User Gave Thanks to CarloM For This Post:
# 4  
Old 10-05-2013
Quote:
Originally Posted by bartus11
Try:
Code:
awk 'NR==FNR{a[$1]=$2;next}!(FNR%3){n=split($0,x,"");$0="";for (i=1;i<=n;i++)$0=$0""a[x[i]]}1' file1 file2

Thanks a lot, this is working. I didnt know how to use split function. Learned something today. Smilie

---------- Post updated at 08:09 PM ---------- Previous update was at 08:08 PM ----------

Quote:
Originally Posted by CarloM
Code:
awk 'NR==FNR{a[$1]=$2;next}{for (i in a) gsub(i,a[i]); print }' file1 file2

Some how this didnt work for me despite using backlashes after gsub. Thank you though.
# 5  
Old 10-06-2013
Note: CarloM's suggestion should work with your sample, however the first variable used in the gsub command gets interpreted as an entended regular expression (ERE). So if your actual "file1" contains characters that have a special meaning in an ERE then it will not work.

( If it did not work for you with your sample input, then what is your OS / version and what did you mean by backslashes ?)

You cannot solve it by using backslashes, since these can also be used to introduce a special meaning to an ordinary character (for example n and \n) ..

--
Bartus11's suggestion relies on a particular (non-POSIX) awk extension so that an empty field separator (FS) means that each individual character becomes a separate field (it is only done here only on every third line, I don't understand why)

--
Also using the special case of null-string FS:
Code:
awk 'NR==FNR{A[$1]=$2; next}{for(i=1; i<=NF; i++)if($i in A) $i=A[$i]}1' file FS= OFS= file2

for other awks, one could try:
Code:
awk 'NR==FNR{A[$1]=$2;next}{s=x; for(i=1; i<=length; i++){c=substr($0,i,1); s=s (c in A? A[c]:c) } print s}' file1 file2


Last edited by Scrutinizer; 10-06-2013 at 03:57 AM..
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: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. HP-UX

Replacing Hex Characters In A File Using awk?

Hi guys, First off, i'm a complete noob to UNIX and LINUX so apologies if I don't understand the basics! I have a file which contains a hex value of '0D' at the end of each line when I look at it in a hex viewer. I need to change it so it contains a hex value of '0D0A0A' I thought... (10 Replies)
Discussion started by: AndyBSG
10 Replies

3. Shell Programming and Scripting

Awk: Append new elements to an array

Hi all, I'm dealing with a bash script to merge the elements of a set of files and counting how many times each element is present. The last field is the file name. Sample files: head -5 *.tab==> 3J373_P15Ac1y2_01_LS.tab <== chr1 1956362 1956362 G A hom ... (7 Replies)
Discussion started by: lsantome
7 Replies

4. Shell Programming and Scripting

awk script to count characters in file 1 in file 2

I need a scripting AWK to compare 2 files. file 1 and 2 are list of keywords 1 is a b c d 2 is aa aaa b bb ccc d I want the AWK script to give us the number of times every keyword in file 1 occurs in file 2. output should be a 2 (7 Replies)
Discussion started by: anhtt
7 Replies

5. Shell Programming and Scripting

sed substitution or awk, need to direct change the file

I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB) eg: $(AA) something $(AA) $(BB) something (7 Replies)
Discussion started by: yanglei_fage
7 Replies

6. Shell Programming and Scripting

Looping within the elements of a file using awk

Hi all, I have a file containing 5000 rows and 4 columns. I need to do a loop within the rows based on the values of column 3. my sample data is formatted like the ones below: what i need to do is to make a loop that will allow me to plot the values of x,y,values corresponding to month 1 to month... (10 Replies)
Discussion started by: ida1215
10 Replies

7. Shell Programming and Scripting

printing array elements inside AWK

i just want to dump my array and see if it contains the values i am expecting. It should print as follows, ignore=345fht ignore=rthfg56 . . . ignore=49568g Here is the code. Is this even possible to do? please help termReport.pl < $4 | dos2ux | head -2000 | awk ' BEGIN... (0 Replies)
Discussion started by: usustarr
0 Replies

8. Shell Programming and Scripting

awk - array elements as condition

Hi, can I use array elements ( all ) in conditional statements? the problem is ,the total number of elements is not known. e.g A is an array with elements - 1,2,3 now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this if ( $1 ~... (1 Reply)
Discussion started by: shellwell
1 Replies

9. Shell Programming and Scripting

How to extract elements using Awk

Hi, I have this typical extraction problem in AWK. I have 3 input files.. i) First one is somehow like an oracle of:- foo 12,23,24 bla 11,34 car 35 ii)Second file is basically detailing the score for each of the second field of first file. Besides, for the first column, it is the... (3 Replies)
Discussion started by: ahjiefreak
3 Replies

10. Shell Programming and Scripting

How to transpose data elements in awk

Hi, I have an input data file :- Test4599,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,2,Rain Test90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,Not Rain etc.... I wanted to transpose these data to:-... (2 Replies)
Discussion started by: ahjiefreak
2 Replies
Login or Register to Ask a Question