我有一個input.txt包含以下值的輸入檔案:
# time(t) Temperature Pressure Velocity(u, v, w)
t T P u v w
0 T0 P0 (u0 v0 w0)
0.0015 T1 P1 (u1 v1 w1)
0.0021 T2 P2 (u2 v2 w2)
0.0028 T3 P3 (u3 v3 w3)
0.0031 T4 P4 (u4 v4 w4)
0.0041 T5 P5 (u5 v5 w5)
... ... ... ... ...
... ... ... ... ...
1.5001 TN PN (uN vN wN)
其中Ti,Pi,ui,vi,和wi對i = 0向N的浮點數。
另一方面,我有一些與時代相對應的目錄:
0 # this is a directory
0.0015 # this is a directory also
0.0021 # ...etc.
0.0028
0.0031
...
...
我有一個如下所示的模板myTemplate.txt檔案:
# This is my template file
The time of the simulation is: {%TIME%}
The Temperature is {%T%}
The pressure is {%P%}
The velocity vector is: ({%U%} {%V%} {%W%})
我的目標是output.txt使用模板檔案在每個時間目錄下創建一個檔案myTemplate.txt并填充輸入檔案中的值input.txt。
我嘗試了以下方法:
# assume the name of the directory perfectly matches the time in input file
inputfile="input.txt"
times = $(find . -maxdepth 1 -type d)
for eachTime in $times
do
line=$(sed -n "/^$eachTime/p" $inputfile)
T=$(echo "$line" cut -f2 ) # get temperature
P=$(echo "$line" | cut -f3 ) # get pressure
U=$(echo "$line" | cut -f4 | tr -d '(') # remove '('
V=$(echo "$line" | cut -f5 )
W=$(echo "$line" | cut -f6 | tr -d ')' ) # remove ')'
# I am stuck here, How can I generate a file output.txt from
# the template and save it under the directory.
done
我被困在需要填充模板檔案中的值并output.txt在每個目錄下生成一個檔案的步驟。
如何做到這一點,也可以通過建議的有效方式來完成使用Linux的標準工具,如該任務的任何幫助sed,awk非常感謝。
uj5u.com熱心網友回復:
我已經修改了您的 bash 腳本,其中包含多個拼寫錯誤/錯誤。這不是完成此任務的最有效方法,但我已經在您的資料上對其進行了測驗,并且可以正常作業:
創建腳本檔案generate.sh:
#!/bin/bash
timedir=$(find * -maxdepth 1 -type d) # use * to get rid of ./ at the beginning
templateFile='./myTemplate.txt' # the path to your template file
for eachTime in $timedir
do
# use bash substitution to replace . with \. in times
# in order to avoid unexpected matches
line="$(grep -m 1 -e '^'${eachTime//./\.} input.txt)"
if [ -z "$line" ]
then
echo "***Error***: Data at time: $eachTime were not found!" >&2
exit 1
fi
# the line below is redundant since time is already known
# replace tabs/and spaces with a single space
Time=$(echo "$line" | tr -s '[:blank:]' ' ' | cut -d' ' -f1 )
Temperature=$(echo "$line" | tr -s '[:blank:]' ' ' | cut -d' ' -f2 )
Pressure=$(echo "$line" | tr -s '[:blank:]' ' ' | cut -d' ' -f3 )
U=$(echo "$line" | tr -s '[:blank:]' ' ' | tr -d '()' | cut -d' ' -f4 )
V=$(echo "$line" | tr -s '[:blank:]' ' ' | tr -d '()' | cut -d' ' -f5 )
W=$(echo "$line" | tr -s '[:blank:]' ' ' | tr -d '()' | cut -d' ' -f6 )
# Create a temporary file
buff_file="$(mktemp)"
# Copy the template to that file
cp "$templateFile" "$buff_file"
# Use sed to replace the values
sed -i "s/{%TIME%\}/$eachTime/g" "$buff_file"
sed -i "s/{%T%}/$Temperature/g" "$buff_file"
sed -i "s/{%P%}/$Pressure/g" "$buff_file"
sed -i "s/{%U%}/$U/g" "$buff_file"
sed -i "s/{%V%}/$V/g" "$buff_file"
sed -i "s/{%W%}/$W/g" "$buff_file"
# Copy that temporary file under the time directory
cp "$buff_file" "$eachTime"/output.txt
# delete the temporary file
rm "$buff_file"
done
echo "Done!"
運行腳本:
chmod x generate.sh
./generate.sh
我已經檢查過output.txt每個時間目錄下都創建了一個檔案,并且包含來自input.txt. 如果找不到時間,腳本也應該引發錯誤。
uj5u.com熱心網友回復:
這是一個作業原型,請注意,沒有對丟失目錄或錯誤輸入格式等的錯誤處理。
$ awk 'NR==FNR {temp=temp sep $0; sep=ORS;next}
FNR==2 {for(i=1;i<=NF;i ) h[$i]=i}
FNR>3 {text=temp;
sub("{%TIME%}", $h["t"] ,text);
# add other sub(..., text) substitutions!
print text > ($1 "/output.txt")}' template.txt input.txt
這只會替換時間,但您可以為其他變數重復相同的模式。
讀取模板檔案并保存在變數 temp 中。讀取輸入檔案并捕獲標題名稱以便于參考 array h。對每個資料行進行替換并保存到相應目錄(假設存在)。
這應該很容易閱讀:
sub("{%TIME%}", $h["t"], text)替換{%TIME%}為$h["t"]in 變數的值text。
$h["t"]表示 index 處的值h["t"],我們將其索引t放在標題行中,即 1。因此,$1我們可以撰寫而不是寫入,$h["t"]以便我們參考的變數被記錄到位。您將再次使用名稱“T”、“P”等參考的另一個變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/341778.html
