linux - Apply shell script to strings inside a file and replace string inside quotes -
suppose have file greeting.txt
(double quotes inside file, 1 line):
"hello world!"
and file names.txt
(no double quotes, many lines, show 2 here example):
tom mary
then want create bash script create files greeting_to_tom.txt
:
"hello tom!"
and greeting_to_mary.txt
:
"hello mary!"
i'm quite newbie in shell script, after piece searched, tried:
greetingtoall.sh
:
#!/bin/bash filename="greeting_to_"$1".txt" cp greeting.txt $filename sed -i 's/world/$1/' $filename
and @ command line type:
cat names.txt | xargs ./greetingtoall.sh
but tom
recognized , it's wrongly replaced $1
. can me task?
if create greetingtoall
below:
greeting=$(cat greeting.txt) while read -r name; echo "$greeting" | sed "s/world/$name/" > "greeting_to_$name.txt" done < "$1"
you can call as:
./greetingtoall names.txt
depending on you're doing, might better idea parameterise script take greeting file choose:
greeting=$(cat "$1") while read -r name; echo "$greeting" | sed "s/world/$name/" > "greeting_to_$name.txt" done < "$2"
then can call as:
./greetingtoall greeting.txt names.txt
Comments
Post a Comment