如何在 shell 腳本中轉義符號?

如何在 shell 腳本中轉義符號?

我想使用 shell 腳本將文字寫入其他文件echo,但資料並未按照我的意圖以準確的格式寫入

echo paste B01_reprojected_subset_comma_updated.csv  B02_reprojected_subset_comma_updated.csv B03_reprojected_subset_comma_updated.csv B04_reprojected_subset_comma_updated.csv B05_reprojected_subset_comma_updated.csv B06_reprojected_subset_comma_updated.csv B07_reprojected_subset_comma_updated.csv B08_reprojected_subset_comma_updated.csv B8A_reprojected_subset_comma_updated.csv B09_reprojected_subset_comma_updated.csv B10_reprojected_subset_comma_updated.csv B11_reprojected_subset_comma_updated.csv B12_reprojected_subset_comma_updated.csv |awk -F",||\t" '{print  $1","$2","$3","$6","$9","$12","$15","$18","$21","$24","$27","$30","$33","$36","$39}'>allbands.csv >> jobstext.sh

但我在 jobstext.sh 中收到這樣的文本

paste B01_reprojected_subset_comma_updated.csv B02_reprojected_subset_comma_updated.csv B03_reprojected_subset_comma_updated.csv B04_reprojected_subset_comma_updated.csv B05_reprojected_subset_comma_updated.csv B06_reprojected_subset_comma_updated.csv B07_reprojected_subset_comma_updated.csv B08_reprojected_subset_comma_updated.csv B8A_reprojected_subset_comma_updated.csv B09_reprojected_subset_comma_updated.csv B10_reprojected_subset_comma_updated.csv B11_reprojected_subset_comma_updated.csv B12_reprojected_subset_comma_updated.csv,,,,,,,,,,,,,,

我試圖轉義特殊符號,但數據仍然不是準確的格式。

有人可以幫我做這個嗎?

paste B01_reprojected_subset_comma_updated.csv B02_reprojected_subset_comma_updated.csv B03_reprojected_subset_comma_updated.csv B04_reprojected_subset_comma_updated.csv B05_reprojected_subset_comma_updated.csv B06_reprojected_subset_comma_updated.csv B07_reprojected_subset_comma_updated.csv B08_reprojected_subset_comma_updated.csv B8A_reprojected_subset_comma_updated.csv B09_reprojected_subset_comma_updated.csv B10_reprojected_subset_comma_updated.csv B11_reprojected_subset_comma_updated.csv B12_reprojected_subset_comma_updated.csv |awk -F",||\t" '{print  $1","$2","$3","$6","$9","$12","$15","$18","$21","$24","$27","$30","$33","$36","$39}'>allbands1.csv

答案1

為了防止 shell 接觸您的文字(包括像$and >or 這樣的特殊字符*,請將所有內容放在單引號中。要在字串中包含單引號,您必須中斷帶引號的字串並添加反斜線轉義的單引號,結果是'here comes the embedded '\''quote'

echo 'paste B01_reprojected_subset_comma_updated.csv  B02_reprojected_subset_comma_updated.csv B03_reprojected_subset_comma_updated.csv B04_reprojected_subset_comma_updated.csv B05_reprojected_subset_comma_updated.csv B06_reprojected_subset_comma_updated.csv B07_reprojected_subset_comma_updated.csv B08_reprojected_subset_comma_updated.csv B8A_reprojected_subset_comma_updated.csv B09_reprojected_subset_comma_updated.csv B10_reprojected_subset_comma_updated.csv B11_reprojected_subset_comma_updated.csv B12_reprojected_subset_comma_updated.csv |awk -F",||\t" '\''{print  $1","$2","$3","$6","$9","$12","$15","$18","$21","$24","$27","$30","$33","$36","$39}'\''>allbands.csv' >> jobstext.sh

答案2

如果您拆分列印並使用printf格式字串來安排輸出,則會更容易。首先,貼上指令,利用大括號擴充:

printf "%s " paste B{01..12}_reprojected_subset_comma_updated.csv >> jobstext.sh

然後,使用 OFS 簡化了 awk 指令:

printf "| %s '%s' %s %s '%s' > %s\n" awk -F',|\t' -v OFS=, '{print $1, $2, $3, $6, $9, $12, $15, $18, $21, $24, $27, $30, $33, $36, $39}' allbands1.csv >> jobstext.sh

您也可以使用%q來簡化用於 的格式字串printf

答案3

如果沒有文件範例,或者沒有明確描述您想要執行的操作或所需的輸出文件格式,這只是猜測。也許您想將命令列(貼上內容|awk 操作)附加到檔案 jobstext.sh 中。最好使用編輯器來避免嵌套引用問題...

但是您的問題詢問有關轉義特殊字元的問題,因此回答一個選項是將單引號封裝在雙引號內。將冗長的檔案名稱縮寫為 B01 等,如下所示。

echo 'paste B01 B02 B03 B04 B05 B06 B07 B08 B8A B09 B10 B11 B12 |
awk -F",||\t" '"'"'{print "$1","$2","$3","$6","$9","$12","$15","$18","$21",
"$24","$27","$30","$33","$36","$39}'"'"' >allbands.csv' >> jobstext.sh

第一個單引號開始一個字串,其中包括貼上命令、檔案名稱、管道、awk 和 F 選項。然後序列的第一個單引號'"'"'關閉字串,接下來的 3 個字元定義另一個單引號字串,最後一個字元是開始第三個字串的單引號(包括大括號中的 awk 運算)。類似的 5 引號序列關閉該字串,插入另一個單引號,並開始第五個字串(其中包括將輸出重定向到 allbands.csv)。最後,該字串被關閉(使用該行的最後一個引號),因此所有 5 個字串都被連接並附加到 jobstext.sh 中。

這很麻煩,但這就是嘗試在一個命令列上使用兩級單引號的結果。需要使用單引號來防止 shell 解釋 awk 語句。

改進將是 1/ 使用可以擴展的文件名*(但是您的“B8A”引入了不一致,阻止了簡單的文件名擴展),以及 2/ 通過將其編輯到 jobstext.sh 來簡化命令。 @muru 的答案中的建議值得納入(但要小心“B8A”文件!)

相關內容