[英]Filter a text from a specific file and append the output to another file Linux
我正在嘗試使用 grep 命令將 append 文件中的文本轉換為 Linux 中的另一個文件。
我有一個名為“temp1buildVersion.properties”的文件,其中包含數據
喜歡
Project version: 1.0.5
另外,我還有另一個名為 buildversion.properties 的文件,其中包含數據
VERSION_BUILD=
我想在“項目版本:”和 append 之后從 temp1buildVersion.properties 中獲取內容到名為“buildversion.properties”的現有文件中
這樣 buildversion.properties 的 output 將是
VERSION_BUILD=1.0.5
目前,我正在使用 grep 命令獲取數據並將 output 附加到文件“buildversion.properties”
grep 'Project version: ' /tmp/tempbuildVersion.properties | cut -d\ -f3 >> /tmp/buildversion.properties
它有兩行我如何將 append 連接到同一行/或特定行?
VERSION_BUILD =
1.0.5
您可以使用此awk
:
awk -F ': ' 'FNR==NR {ver=$2; next} /^VERSION_BUILD=/ {print $0 ver}' temp1buildVersion.properties buildversion.properties > _tmp && mv _tmp buildversion.properties
VERSION_BUILD=1.0.5
另一種選擇是使用sed
到 append 到行尾,例如
sed "/VERSION_BUILD/s/\$/$(grep 'Project version: ' /tmp/tempbuildVersion.properties | cut -d\ -f3)/" buildversion.properties
您的命令上方只是作為命令替換放置在sed "/VERSION_BUILD/s/\$/$(your_cmd)/" file
中。 您將添加sed -i
以更新文件。
您可以通過簡單地使用awk
來隔離版本號並稍微縮短命令來消除管道和cut
,例如
sed "/VERSION_BUILD/s/\$/$(awk '/^Project version:/{printf "%s", $NF; exit}' /tmp/tempbuildVersion.properties)/" buildversion.properties
如果ed
可用/可接受。
printf '%s\n' 'r temp1buildVersion.properties' 's/^Project version: //' '1,$j' ,p Q | ed -s buildversion.properties
如果您對 output 沒問題,將Q
更改為w
並編輯文件buildversion.properties
劇本。
#!/usr/bin/env bash
ed -s "$1" <<-EOF
r $2
s/^Project version: //
1,\$j
,p
Q
EOF
您可以將文件作為 arguments 執行。
./myscript buildversion.properties temp1buildVersion.properties
這可能對您有用(GNU sed):
sed -i '/VERSION_BUILD=/{x;s/.*/cat fileVersion/e;x;G;s/\n.*:\s*//}' fileBuild
處理構建文件,直到匹配到VERSION_BUILD=
行。
切換到保持空間並插入版本文件行。
Append 從版本文件到當前行的行並使用模式匹配將該行操作為所需的格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.