简体   繁体   English

过滤来自特定文件的文本和 append 和 output 到另一个文件 Linux

[英]Filter a text from a specific file and append the output to another file Linux

I am trying to append a text from a file to the another file in Linux using the grep command.我正在尝试使用 grep 命令将 append 文件中的文本转换为 Linux 中的另一个文件。

I have a file named "temp1buildVersion.properties" which contain the data我有一个名为“temp1buildVersion.properties”的文件,其中包含数据
like喜欢

Project version: 1.0.5

also, I have another file named buildversion.properties which contain data另外,我还有另一个名为 buildversion.properties 的文件,其中包含数据

VERSION_BUILD=

I want to fetch content from temp1buildVersion.properties" after "Project version:" and append it to an existing file named "buildversion.properties"我想在“项目版本:”和 append 之后从 temp1buildVersion.properties 中获取内容到名为“buildversion.properties”的现有文件中

so that output of the buildversion.properties will be这样 buildversion.properties 的 output 将是

VERSION_BUILD=1.0.5

currently, I am doing using the grep command to fetch data and appending output to file " buildversion.properties "目前,我正在使用 grep 命令获取数据并将 output 附加到文件“buildversion.properties”

grep 'Project version: ' /tmp/tempbuildVersion.properties | cut -d\   -f3  >> /tmp/buildversion.properties

it comes in two-line How can I append to the same line /or a specific line?它有两行我如何将 append 连接到同一行/或特定行?

VERSION_BUILD =
1.0.5

You may use this awk :您可以使用此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

Another option is using sed to append to the end of the line, eg另一种选择是使用sed到 append 到行尾,例如

sed "/VERSION_BUILD/s/\$/$(grep 'Project version: ' /tmp/tempbuildVersion.properties | cut -d\   -f3)/" buildversion.properties

Above your command is simply placed as a command substituion in sed "/VERSION_BUILD/s/\$/$(your_cmd)/" file .您的命令上方只是作为命令替换放置在sed "/VERSION_BUILD/s/\$/$(your_cmd)/" file中。 You would add sed -i to update the file in place.您将添加sed -i以更新文件。

You can eliminate the pipeline and cut by simply using awk to isolate the version number and shorten the command a bit, eg您可以通过简单地使用awk来隔离版本号并稍微缩短命令来消除管道和cut ,例如

sed "/VERSION_BUILD/s/\$/$(awk '/^Project version:/{printf "%s", $NF; exit}' /tmp/tempbuildVersion.properties)/" buildversion.properties

If ed is available/acceptable.如果ed可用/可接受。

printf '%s\n' 'r temp1buildVersion.properties' 's/^Project version: //' '1,$j' ,p Q | ed -s buildversion.properties

Change Q to w if you're ok with the output and to edit the file buildversion.properties如果您对 output 没问题,将Q更改为w并编辑文件buildversion.properties

The script.剧本。

#!/usr/bin/env bash

ed -s "$1" <<-EOF
  r $2
  s/^Project version: //
  1,\$j
  ,p
  Q
EOF

You can execute with the files as the arguments.您可以将文件作为 arguments 执行。

./myscript buildversion.properties temp1buildVersion.properties

This might work for you (GNU sed):这可能对您有用(GNU sed):

sed -i '/VERSION_BUILD=/{x;s/.*/cat fileVersion/e;x;G;s/\n.*:\s*//}' fileBuild

Process the build file until a match on a line VERSION_BUILD= .处理构建文件,直到匹配到VERSION_BUILD=行。

Swap to the hold space and insert the version file line.切换到保持空间并插入版本文件行。

Append the line from the version file to the current line and using pattern matching manipulate the line into the desired format. Append 从版本文件到当前行的行并使用模式匹配将该行操作为所需的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM