0

I am trying to append a text from a file to the another file in Linux using the grep command .

I have a file named "temp1buildVersion.properties" which contain the data
like

Project version: 1.0.5

also, I have another file named buildversion.properties which contain data

VERSION_BUILD=

I want to fetch content from temp1buildVersion.properties" after "Project version:" and append it to an existing file named "buildversion.properties"

so that output of the buildversion.properties will be

VERSION_BUILD=1.0.5

currently, I am doing using the grep command to fetch data and appending output to file " 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?

VERSION_BUILD =
1.0.5

4 Answers 4

3

You may use this 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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, is there any way we can append to buildversion.properties file itself. currently it print value in console .
2

Another option is using sed to append to the end of the line, e.g.

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. You would add sed -i to update the file in place.

You can eliminate the pipeline and cut by simply using awk to isolate the version number and shorten the command a bit, e.g.

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

1 Comment

Happy to help, good luck with your scripting!
0

If ed is available/acceptable.

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

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.

./myscript buildversion.properties temp1buildVersion.properties

Comments

0

This might work for you (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=.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.