Search replace with awk using 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search replace with awk using 2 files
# 1  
Old 10-16-2014
Search replace with awk using 2 files

I have a bit of a complex problem that I would like to solve with
Code:
awk

. It is essentially a 2-part problem.

I have a large directory of files with the same format, each with 266 lines.
The first 206 lines of each file are filled with attribute information.
Then the following 60 lines consist of 202 values separated by commas.
The first position in each of these sixty lines is a word (string value), and the last position in each of these sixty lines is a number (1 or 0).
Is it possible to change the last slot ($202) numeric value of lines that contain certain strings that are indicated in a separate file?

To visualize the problem.
My data file looks like this:

Code:
@RELATION relationData

@ATTRIBUTE att0 STRING
@ATTRIBUTE att1 NUMERIC
@ATTRIBUTE att2 NUMERIC
@ATTRIBUTE att3 NUMERIC
....
@ATTRIBUTE att200 NUMERIC

@ATTRIBUTE class {1,0}

@DATA
hall,1,2,3,...,201,0
cat,1,2,3,...,201,1
dog,1,2,3,...,201,1
feather,1,2,3,...,201,1

I have a second file with a list of words (1 per line):

Code:
cat
feather

I want to change the final numeric value on those lines that contain a word in the second file to 0, so that my file result is:

Code:
@RELATION relationData

@ATTRIBUTE att0 STRING
@ATTRIBUTE att1 NUMERIC
@ATTRIBUTE att2 NUMERIC
@ATTRIBUTE att3 NUMERIC
....
@ATTRIBUTE att200 NUMERIC

@ATTRIBUTE class {1,0}

@DATA
hall,1,2,3,...,201,0
cat,1,2,3,...,201,0
dog,1,2,3,...,201,1
feather,1,2,3,...,201,0

Is this possible to do with
Code:
awk

? Any suggestions of how to tackle this problem?

Perhaps something like this:
Code:
awk -v ip1="$INPUT1" -v ip2="$INPUT2" '{gsub( /String1/, ip1);gsub( /String2/, ip2);print}' file

which I found HERE can be modified?

Last edited by owwow14; 10-16-2014 at 07:09 AM.. Reason: formatting - add information
# 2  
Old 10-16-2014
words to search
Code:
[akshay@nio tmp]$ cat word 
cat
feather

Data file
Code:
[akshay@nio tmp]$ cat myfile 
@RELATION relationData

@ATTRIBUTE att0 STRING
@ATTRIBUTE att1 NUMERIC
@ATTRIBUTE att2 NUMERIC
@ATTRIBUTE att3 NUMERIC
....
@ATTRIBUTE att200 NUMERIC

@ATTRIBUTE class {1,0}

@DATA
hall,1,2,3,...,201,0
cat,1,2,3,...,201,1
dog,1,2,3,...,201,1
feather,1,2,3,...,201,1


Code executed
Code:
[akshay@nio tmp]$ awk 'FNR==NR{A[$1];next}($1 in A){$NF=0}1' word FS=',' OFS=',' myfile

Output
Code:
@RELATION relationData

@ATTRIBUTE att0 STRING
@ATTRIBUTE att1 NUMERIC
@ATTRIBUTE att2 NUMERIC
@ATTRIBUTE att3 NUMERIC
....
@ATTRIBUTE att200 NUMERIC

@ATTRIBUTE class {1,0}

@DATA
hall,1,2,3,...,201,0
cat,1,2,3,... 201,0
dog,1,2,3,...,201,1
feather,1,2,3,... 201,0


Last edited by Akshay Hegde; 10-16-2014 at 08:30 AM.. Reason: formating....
# 3  
Old 10-16-2014
Thank you Akshay, However, there are 2 input files.
Is there a specific order I should put the
Code:
List file

and the
Code:
Data file

?
# 4  
Old 10-16-2014
Quote:
Originally Posted by owwow14
Thank you Akshay, However, there are 2 input files.
Is there a specific order I should put the
Code:
List file

and the
Code:
Data file

?
Yes .
# 5  
Old 10-16-2014
Quote:
Originally Posted by owwow14
Thank you Akshay, However, there are 2 input files.
Is there a specific order I should put the
Code:
List file

and the
Code:
Data file

?
Hello owwow14,

Following may help you in same. Using Akshay's approach but changing the file names to be more understandable format.

Code:
awk 'FNR==NR{A[$1];next}($1 in A){$NF=0}1' list FS=',' OFS=',' data

EDIT: yes order of files matters here, as condition FNR==NR will be true only when first file is getting read.


Thanks,
R. Singh

Last edited by RavinderSingh13; 10-16-2014 at 10:50 AM.. Reason: Added reason for files place
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace with awk

Hi Shell Tigers, I am trying to acheive search and replace strings within a setup file. Help much appreciated. test.setup ORACLE_HOME=/oracle/product/11.0.0/home01 PATH1=/perm_loc/3222/FTP/cfg1 PATH2=/perm_loc/3222/FTP/cfg2/bin PATH3=/perm_loc/3222/FTP/cfg3/bin So... (3 Replies)
Discussion started by: jville
3 Replies

2. Shell Programming and Scripting

Awk: search and replace

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. going forward, search for another keyword. 3. Replace this line with user supplied input. for e.g., my file has the following... (6 Replies)
Discussion started by: chingupt
6 Replies

3. Shell Programming and Scripting

Search & Replace content of files using gsub in awk

Hi,I have 2 files master.txt & reference.txt as shown below & i require o/p as mentioned in file 3 using awk but content is not replacing properlymaster.txt:... (15 Replies)
Discussion started by: siramitsharma
15 Replies

4. Shell Programming and Scripting

Exact Search and Replace using AWK

Hello. I have written the following script to search and replace from one file into another. #awk script to search and replace from file a in file b NR == FNR { A=$2; next } { for( a in A ) sub(a, A)}1 file2 file1 While the function works pretty well, I want a. The word in File 2 to... (8 Replies)
Discussion started by: gimley
8 Replies

5. Shell Programming and Scripting

awk search and replace field

I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed. There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk.... (2 Replies)
Discussion started by: bluejayek
2 Replies

6. Shell Programming and Scripting

search and replace with AWK

Suchen und Ersetzen mit AWK hello, i'm not good in scripting and asking for your help. With this script i change some text parts in diffent datafiles. It works without problems, but i need to get some informations about what changes in wich datafiles happend. This could be in character of a... (3 Replies)
Discussion started by: ruffi
3 Replies

7. Shell Programming and Scripting

Search and Replace using awk

Hi, I am really confused with this problem that I am facing . I have a file that contains entries in the form : option1 Value1 option2 Value2 option3 Value3 option4 Value4 . . . I want to search for the keyword "option4" and replace "value4" by another value, say "value5". My main... (2 Replies)
Discussion started by: harry0812
2 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

Search and replace using awk

Dear All, I want to search and replace the text in file using awk. but facing hard luck in that. Please help me out!!!! > grep Abc.De.ync.rate /tmp/sdosanjh.txt Abc.De.ync.rate 6 write Now, I want to replace the "6" with value say "2". I... (5 Replies)
Discussion started by: sdosanjh
5 Replies

10. Shell Programming and Scripting

search and replace using awk with variables

Hi, I have been trying to use awk with variables that needs to search for a pattern and replace it with another pattern, the patterns are supplied in a variable. I have tried several different ways without success and hope that someone can help me. Here are the details echo $UPC 07007457809... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question