replacing text in file1 with list from file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing text in file1 with list from file2
# 8  
Old 07-23-2008
The slashes are part of awk syntax and not related to the directory separator character of your platform. So no, no need to change them to backslashes.

In the sed script, the file name containing the tags is supposed to be in ..\replace so yes, there, you'd perhaps change the directory separator (although I understand DOS can cope with a forward slash as well).

awk 'script in awk syntax here' file1 file2 file3 ... is how you typically invoke an awk script. On DOS you might need to use different quote marks, or simply put the script in a file and run it from there (with awk -f scriptfile.awk assuming your version of awk supports that).

The above script with sed and awk in it is a shell script, so you need /bin/sh or bash or something like that to execute it.
# 9  
Old 07-23-2008
OK, i am REALLY lost now (I use SED for basic search/replace and AWK is a creature:

temp=`sed -n "${n}p" ../replace`

I change the ../replace with the name of the files with the IPs in it?
Lets say the files are named dns.txt and ips.txt
dns.txt = the file with the T????CLK????
ips.txt = the file with the pool of IPs to use as the substitutes
script.awk has the awk command from above in it so i would type "awk -f script.awk dns.txt ips.txt" at the command prompt?
# 10  
Old 07-24-2008
Sorry, no. It's still a shell script. You would do something like

Code:
n=0
for i in dns.txt
do
        temp=""
        # Grab the next line out of ips.txt
        n=`expr $n + 1`
        temp=`sed -n "${n}p" ips.txt`
        # Replace all occurrences in current file with value $temp
        nawk -v t="$temp" script.awk "$i" >tmp
       # Don't replace file if no change
       cmp "$i" tmp >/dev/null && continue
       mv "$i" "$i"~
       mv tmp "$i"
done
rm -f tmp

... and script.awk would contain the nawk script.

This is probably not directly useful, but shows where you would plug in the parts you had questions about.

The original script wanted to change all files in the current directory (that's * where now we have only dns.txt), and globally replace all occurrences of "this" in each file with a different value out of ../replace (now ips.txt), in a different directory because you were making substitutions in all files in the current directory (basically I imagine on the assumption that there is only one occurrence of "this" -- or actually T????CLK??? -- in each file, but a lot of files).

But what you seem to want is probably closer to

Code:
nawk 'NR == FNR { r[++i] = $0 ; next }
    { gsub (/T...CLK.../, r[++j]); print }' ips.txt dns.txt >dns.new

If there are multiple occurrences of T????CLK??? on a single line, they will both get the same value out of ips.txt -- if this is a problem, a slightly more complex script will be necessary. Also if there are more lines with matches in dns.txt than there are values in ips.txt the last ones in dns.txt will be replaced with an empty value. This is obviously not very newbie-friendly, but as long as you keep these constraints in mind, it's simple and understandable.

The output is stored in dns.new; you can move it to dns.txt when you're done. The earlier script has code for checking whether any substitutions took place, and only replacing the old file if the new file is different.

(Line wraps in the awk scripts are not significant; you can type it all on a single line if you like.)

Last edited by era; 07-24-2008 at 02:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 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. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Replacing first field of file2 with the second filed of file1 for matching cases

Dear All, Need your help..:D I am not regular on shell scripts..:( I have 2 files.. Content of file1 cellRef 4};"4038_2_MTNL_KALAMBOLI" cellRef 1020};"4112_3_RAINBOW_BLDG" cellRef 134};"4049_2_TATA_HOSPITAL" cellRef 1003};"4242_3_HITESH_CONSTRUCTION" cellRef... (6 Replies)
Discussion started by: ailnilanjan
6 Replies

5. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

6. Shell Programming and Scripting

Search within file1 numbers from list in file2

Hello to all, I hope somebody could help me with this: I have this File1 (real has 5 million of lines): Number Category --------------- -------------------------------------- 8734060355 3 8734060356 ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

9. Shell Programming and Scripting

append text from file1 to the end of each line in file2

hi; my file2.txt:portname=1;list=10.11;l- portname=2;list=10.12;l- portname=3;list=10.13;l- ... my file1.txt:;"{'sector=%27'}"\&> so; i want to see:portname=1;list=10.11;l-;"{'sector=%27'}"\&> portname=2;list=10.12;l-;"{'sector=%27'}"\&> portname=3;list=10.13;l-;"{'sector=%27'}"\&>... (4 Replies)
Discussion started by: gc_sw
4 Replies

10. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies
Login or Register to Ask a Question