O
O
oleww052019-02-21 23:59:24
bash
oleww05, 2019-02-21 23:59:24

How to do substitution in bash?

There are two text files that contain let.

Содержимое первого
/test1/file 1.txt
/test1/file 2.txt
/test1/file 3.txt
Содержимое второго
/test2/papka 1
/test2/papka 2
/test2/papka 3

I use this script
#!/bin/bash
a=$(cat text01.txt)
b=$(cat text02.txt)
VAR=$(paste -d " " <(echo "$a") <(echo "$b"))
echo "$VAR"

Получается
/test1/file 1.txt /test2/papka 1
/test1/file 2.txt /test2/papka 2
/test1/file 3.txt /test2/papka 3

How now to substitute each line in the mv move command?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2019-02-22
@oleww05

Are you sure there are spaces in the filenames?
If not, then you can do this:
or this

sed "s/.*/mv /" 1.txt >text03.txt
paste text03.txt text02.txt text01.txt

If there are, they must first be shielded.
sed -i "s/ /\\\ /g" text01.txt text02.txt
paste 1.txt 2.txt|xargs -n2 mv

R
Ruslan Fedoseev, 2019-02-22
@martin74ua

`mv $VAR`
string at the top in backticks

Q
q2zoff, 2019-02-22
@q27off

a=$(cat text01.txt)
b=$(cat text02.txt)
VAR=$(paste -d "\n" <(echo "$a") <(echo "$b"))

xargs -d "\n" -n 2 mv <<< "$VAR"

while read -r file
do
    files[$((i++))]="$file"
done < text01.txt

while read -r dir
do
    mv "${files[$((j++))]}" "$dir"
done < text02.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question