2

I have this this String

22<>22

And i have this list

test
dev
too

The output should be like

22test22
22dev22
etc..

This is the commands i use for do that

cat list | sed 's/$/22/g' | sed 's/^/22/g'

edit

The command i use for do that

cat list | sed 's/$/22/' | sed 's/^/22/'

But it doesn't helpful Because i have big list of

22<>22
3<>33
134423<>4
2
  • Take a look at sed's key -f Commented Sep 26, 2022 at 0:13
  • About "I have big list of" , are those strings defined in a file too? Commented Sep 26, 2022 at 0:39

3 Answers 3

1

You can assign to an array the list of strings that will replace to <>. In a file called list.txt you should have:

test
dev
too

And in a file called data.txt you should have:

22<>22
3<>33
134423<>4

Solution 1: Reading the file list.txt and assign its content to an array.

Read array using bash

export IFS=$'\n'
readarray array < list.txt

Read array using zsh

array=("${(@f)"$(<list.txt)"}") 

Finally you have to iterate over array variable to get each element of the list and replace with sed:

for i in ${array[@]}; do 
sed "s/<>/$i/g" data.txt
done

Note: The for loop will print the text to the stdout (in your terminal). But if you want to redirect the output to a file, you can use > after done keyword:

for i in ${array[@]}; do 
sed "s/<>/$i/g" data.txt
done > final.txt

This solution will produce an output like this:

22test22
3test33
134423test4
22dev22
3dev33
134423dev4
22too22
3too33
134423too4

Solution 2: First you will have to read the file list.txt and assign its content to an array. Read array using bash

export IFS=$'\n'
readarray array < list.txt

Read array using zsh

array=("${(@f)"$(<list.txt)"}") 

After that you have to iterate over each line of file data.txt and inside that loop read each item from the array to apply the sed command:

while read line ; do 
for i in ${array[@]}; do 
   sed "s/<>/$i/g" <<< "$line"
done
done < data.txt

Or redirecting the stdout to a file:

while read line ; do 
for i in ${array[@]}; do 
   sed "s/<>/$i/g" <<< "$line"
done
done < data.txt > final.txt

This solution will produce the following output:

22test22
22dev22
22too22
3test33
3dev33
3too33
134423test4
134423dev4
134423too4
1
  • 1
    Thank you for you solution Commented Sep 26, 2022 at 11:37
1

change <> to one character | delimiter:

$ sed -s 's/<>/|/g' asdf1 > asdf2
$ cat asdf2
22|22
3|33
134423|4

combine 2 files into one file, using same one character | delimiter:

$ paste -d"|" asdf asdf2 > combined
$ cat combined
test|22|22
dev|3|33
too|134423|4

split with |, and print in order:

$ awk -F'|' '{print $2 $1 $3}'  combined
22test22
3dev33
134423too4

So, in summary:

sed -s 's/<>/|/g' asdf1 > asdf2
paste -d"|" asdf asdf2 > combined
awk -F'|' '{print $3 $1 $2}'  combined

But this will only works, if there is only 1 <>placeholder in each line.

1

With zsh, assuming list doesn't contain $, ` nor \ characters:

$ cat list
22<>22
3<>33
134423<>4
$ cat data
test
dev
too
$ () {print -rC1 -- ${(e)${(f)"$(<list)"}//'<>'/'$^@'}; } ${(f)"$(<data)"}
22test22
22dev22
22too22
3test33
3dev33
3too33
134423test4
134423dev4
134423too4

Where we pass the non-empty lines of data as arguments to an anonymous function. Within that function, we replace <> with $^@ in the lines of list, so we end up with 22$^@22 3$^@33, and with the expand parameter expansion flag, the expansions in there are performed, where $^@ is like $@ (the list of arguments) except that the array expansion is rc/fish-like instead of csh-like (with a=(1 2 3), before${^a}after expands to before1after before2after before3after instead of before1 2 3after with before${a}after).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.