S
S
Senseich2020-11-08 21:28:04
bash
Senseich, 2020-11-08 21:28:04

How to correctly replace paths in filenames?

Good people, help me solve such a problem, because. I don't understand bash at all.
I have a directory in which the photo, I found a script that writes the names of each to the file on a new line. They are obtained with the paths of my PC, I need to replace these paths with others and add array characters at the beginning and at the end.
Those. there is a list:

старый путь/файл_1.jpg
старый путь/файл_2.jpg


Gotta do
var arrIm = [
'новый путь/файл_1.jpg'
'новый путь/файл_2.jpg'
];


New ones must be in single quotes. I would be grateful if you help, I killed 2 hours until I mastered it)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2020-11-08
@Senseich

cat fileOldPath.txt | awk -F/ 'BEGIN{print "var arrIm = ["}{print "'\''новый путь/"$NF"'\''"}END{print "];"}' > fileNewPath.txt

S
sergey, 2020-11-09
kuzmin @sergueik

Senseich
here is your example with corrected errors
the only thing that I did not analyze is localized directory names ..
remove Russian on the way - name it in English letters

#!/bin/bash

FILE="/tmp/file2.txt"
B='var arrIm = ['
L=$(cat $FILE | sed -e "s/\\(..*\\)/'\\1'/g" | sed -e 's|C:\\Users\\SEN\\Desktop\\777\\|https://ohiyo.ru/uploads/posts/|g')
E="
];"

echo "$B$L$E"

cat $FILE
C:\Users\SEN\Desktop\777\XXX.TXT

./a.sh
var arrIm = ['https://ohiyo.ru/uploads/posts/XXX.TXT'
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question