LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2008, 03:17 PM   #1
jettachamp26
Member
 
Registered: Feb 2008
Location: Florida
Distribution: ubuntu
Posts: 30

Rep: Reputation: 15
using sed with an array


OK. Here's some background. I have one file that is a csv(tab separated) that has a url on each line ( ${array1} ). I have another file that has a single word per line ( ${array2} ).

I got to the point where I have both of these files stored into arrays.

What I am trying to do is replace every instance of a word in $array1 ( .* ) (yes it is actually that, not the regexp) with the first item in $array2 and then save that replacement to a new csv file, then do the same thing for the next item in $array2 until there is a file for each item in $array2. Is this possible?

I was thinking something like my pseudocode below, I just don't know if there is a syntax for something like that.

n=0
while [ $n -lt $array2count ]; do
replace every instance of .* in ${array1} with ${array2[$n]}
save textreplaced $array1 to ${array2[$n]}template.csv
bring ${array1} back to its original state
((n++))
done
 
Old 02-25-2008, 11:51 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
You don't provide example input and example output so it's hard to know what you want and what the limitations are. Also, I'd strongly suspect this isn't the right way to go about it, but I don't have a better suggestion at the moment. Is this anything like what you had in mind?
Code:
:cat file3
foo.*bar
baz.*mu

:cat file4
nerf
poit

:cat jettascript
#!/bin/sh
array1=( $(cat file3) )
array2=( $(cat file4) )
for (( i=0;i<${#array1[*]};i++ )); do
    sed -n "/${array1[$i]}/s/\.\*/${array2[$i]}/p" file3
done

:./jettascript
foonerfbar
bazpoitmu
This isn't at all robust (the input data here doesn't need to be quoted and file1 and file2 are the same length and so on) but might give you something you can work with.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH array / sed questions ParaDoX667 Programming 8 04-04-2007 05:12 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration