Compare file to array, replace with corresponding second array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare file to array, replace with corresponding second array
# 1  
Old 08-13-2012
Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays.

I need to be able to create a loop that will find ${ARRAY1[1] in the text doc, and replace it with ${ARRAY2[1] then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2 arrays (they will always be an equal # and will always match instances in the target text file)

So, here is what I have so far

Quote:
LOOP=${#READ[@]}
for i in $(eval echo {0..$LOOP})
do
sed -e 's/'${ARRAY1[$i]}'/'${ARRAY2[$i]}''/'g' ${FILE}.txt
done
This is doing what I need it to do. The problem tho, is that, obviously it is printing ${FILE}.txt over and over and in each instance it is replacing the corresponding array value.

I need do the find/replace across all arrays then output the final result to a new file that contains all of the new data.

I tried:

Quote:
cp $FILE.txt ${FILE}_renamed.txt

LOOP=${#READ[@]}

for i in $(eval echo {0..$LOOP})
do
sed -e 's/'${ARRAY1[$i]}'/'${ARRAY2[$i]}''/'g' ${FILE}_renamed.txt > ${FILE}_renamed.txt
done
thinking, logically that would do the find and replace, open the new version containing the first find and replace do the next, etc until it resulted in what I need.

I end up with a blank document.

can somebody please tell me where I'm going wrong here.

I'm so close, I just don't know where to go from here!

Last edited by gentlefury; 08-13-2012 at 09:39 PM..
# 2  
Old 08-13-2012
Try this:
Code:
sed -i.bak 's/'${ARRAY1[$i]}'/'${ARRAY2[$i]}''/'g' ${FILE}.txt

# 3  
Old 08-14-2012
That worked! You are a master!!

---------- Post updated at 10:21 PM ---------- Previous update was at 07:33 PM ----------

The next problem I'm getting now is it seems to be operating across not exact strings.

so what this is doing is replacing a series of lines names "Read#" with lines "[filename]"

Quote:
name Read31
name Read10
name Read42
name Read43
name Read2
name Read4
name Read22
name Read23
name Read24
name Read25
name Read26
name Dot59
name Read27
name Read28
name Read29
name Read30
name Read48
name Read32
name Read14
name Read15
name Read7
name Read8
name Read13
name Dot27
name Read11
name Read9
name Read17
name Read20
name Read49
name Read12
name Read16
name Read18
name Read5
name Read6
name Read21
name Read33
name Read3
name Read37
name Read38
name Read36
name Read1
name Read34
name Read35
name Read39
name Read19
name Read41
name Read45
name Read40
name Read44
name Read47
name Read46
the problem is, the targets are getting replaced in bulk and ruining the entire script. So rather than replace Read1 with [file in applicable array slot] it is replacing Read1. Read10, Read11 etc.

Quote:
name Read31
name 058_PA_007_CMP_V0024_0000000_L0
name Read42
name Read43
name Read2
name Read4
name Read22
name Read23
name Read24
name Read25
name Read26
name Dot59
name Read27
name Read28
name Read29
name Read30
name Read48
name Read32
name 058_PA_007_CMP_V0024_0000000_L4
name 058_PA_007_CMP_V0024_0000000_L5
name Read7
name Read8
name 058_PA_007_CMP_V0024_0000000_L3
name Dot27
name 058_PA_007_CMP_V0024_0000000_L1
name Read9
name 058_PA_007_CMP_V0024_0000000_L7
name Read20
name Read49
name 058_PA_007_CMP_V0024_0000000_L2
name 058_PA_007_CMP_V0024_0000000_L6
name 058_PA_007_CMP_V0024_0000000_L8
name Read5
name Read6
name Read21
name Read33
name Read3
name Read37
name Read38
name Read36
name 058_PA_007_CMP_V0024_0000000_L
name Read34
name Read35
name Read39
name 058_PA_007_CMP_V0024_0000000_L9
name Read41
name Read45
name Read40
name Read44
name Read47
name Read46
The only one I want to change in that instance is

Quote:
name 058_PA_007_CMP_V0024_0000000_L
the rest are just residual from it replacing all instances instead of the exact match.

I have tried

Quote:
sed -i.bak 's/'\<${ARRAY1[$i]}\>'/'${ARRAY2[$i]}''/'g' ${FILE}.txt
but that results in nothing changing for some reason. If anyone has a way to get this to work it would be a great help! Thanks!

---------- Post updated at 11:37 PM ---------- Previous update was at 10:21 PM ----------

is it possible to use a carriage return \n as a search string? like

Quote:
sed -i.bak 's/${ARRAY1[$i]}\n/'${ARRAY2[$i]}''/'g' ${FILE}.txt
or something like that?

---------- Post updated 08-14-12 at 09:35 AM ---------- Previous update was 08-13-12 at 11:37 PM ----------

Quote:
sed s/${ARRAY1[$i]}$/${ARRAY2[$i]}/ ${FILE}.txt
Looks like that should work, I will try when I get in to work today.


##edit, that was it!
---------- Post updated at 12:30 PM ---------- Previous update was at 09:35 AM ----------

Last edited by gentlefury; 08-14-2012 at 06:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to use if command to compare in an array?

Hi all, Currently i am facing issue using if command to compare to array value. can someone help me? if }" -gt "${ZDATE_1}" ] then do echo "red" else echo "green" fi done i have checked array is giving values fine but there seems some issue with if command. (2 Replies)
Discussion started by: amyt1234
2 Replies

2. Shell Programming and Scripting

Ksh: how compare content of a file with an other array

Hi, I created a skript in ksh which generate a file with semicolon as separator, this is an example of the file a created: example content file: hello;AAAA;2014-08-17 hello;BBBB;2014-08-17 hello;CCCC;2014-08-17 I would need to compare the content in of the second column of this file... (3 Replies)
Discussion started by: jmartin
3 Replies

3. Shell Programming and Scripting

compare two array

Hi firnds, I am having two arrays eg. @a=qw(1 2 3 4) and @b=qw(1 3 6 9) Now, I want to compare two arrays on first index of both arrays and if they matched, it should output remaining indexes Pseudo-code if ($a == $b) { print "remaining indexes";} How can i do this using perl? (1 Reply)
Discussion started by: Renesh
1 Replies

4. Shell Programming and Scripting

compare two value in array - Perl

Hi, I just wondering if I want to compare previous value to next value if it same count + 1 if not doesn't count how do we compare in Perl? Thank (2 Replies)
Discussion started by: guidely
2 Replies

5. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

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

7. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

8. Shell Programming and Scripting

Compare Array results

Hi, In a kshell , i need to compare the results of two array . each Non-match value should copy to a new array. For example: ========== Array one contain the following values: A B C Array two contain the following values: C F A E After comparing this arrays , a new array should... (4 Replies)
Discussion started by: yoavbe
4 Replies

9. UNIX for Dummies Questions & Answers

Compare array values

# tail myprocesses.txt 178 processes at Tue Oct 21 14:33:01 IST 2008 16 MySQL processes at Tue Oct 21 14:33:01 IST 2008 175 processes at Tue Oct 21 14:36:01 IST 2008 60 MySQL processes at Tue Oct 21 14:36:01 IST 2008 192 processes at Tue Oct 21 14:39:01 IST 2008 64 MySQL processes at Tue Oct... (2 Replies)
Discussion started by: shantanuo
2 Replies

10. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies
Login or Register to Ask a Question