扩展此问题:How to Create several files from a list in a text file?
摘要:
cat file_full_of_files_names | tr ' \t' '\n\n' | while read filename; do
if test -f "$filename"; then
echo "Skipping \"$filename\", it already exists"
else
cp -i initial_content "$filename"
fi
done
非常适合我想要的东西,但我想扩展它。以下内容是'initial_content'中的内容
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/com_aac_cachemate_demo_cachemate" /> </selector>
我想改变
@drawable/"XYZ"
XYZ变量到使用
中的内容创建的文件的名称initial_content
但是XYZ变量填入了来自
的文件名file_full_of_file_names
含量
任何脚本小子?抨击?谢谢你的帮助!
答案 0 :(得分:1)
AWK
代替cp
:cat file_full_of_files_names | tr ' \t' '\n\n' | while read filename; do
if test -f "$filename"; then
echo "Skipping \"$filename\", it already exists"
else
awk -F"/" -v OFS="/" -v name="$filename" '/@drawable/{sub(/.*/,name"\"",$2);print;next}1' < initial_content > "$filename"
fi
done
jaypal:~/Temp] cat file # Sample File
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/com_aac_cachemate_demo_cachemate" /> </selector>
[jaypal:~/Temp] echo $filename # Variable Initialization
name
[jaypal:~/Temp] awk -F"/" -v OFS="/" -v name="$filename" '/@drawable/{sub(/.*/,name"\"",$2);print;next}1' file
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/name"/> </selector>
[jaypal:~/Temp]
[jaypal:~/Temp] filename="jaypal" # Re-initializing variable
[jaypal:~/Temp] awk -F"/" -v OFS="/" -v name="$filename" '/@drawable/{sub(/.*/,name"\"",$2);print;next}1' file
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/jaypal"/> </selector>