14525

Appending contents to a file if doesn't exist using comma values

We have a file named parameters.txt which contains in comma separated value:

db.hostname,db.username,db.password,db.url,db.status,codesettingsA, codesettimgsB

Also, we have a main file called properties.txt which contains in Line format

db.hostname=hostname db.username=username db.password=password

I need to pass the comma separated values in parameters.txt and check whether if it is exist in properties.txt. If it is not exist it should print bottom of the properties.txt

For Example, in the properties.txt: db.url, db.status, codesettingsA and codesettimgsB is not there. However, I’m passing from the parameters.txt. It should be appended and the result should be like this in properties.txt:

db.url db.status codesettingsA codesettimgsB

Note: Already values are declared for db.hostname=hostname, db.username=username and db.password=password in properties file. I'm passing db,hostname,db.username,db.password from parameter file. So, this should ignore the values even after the comma and shouldn't print db,hostname,db.username,db.password

Is there any ways to print them at the bottom page of properties.txt? Can you provide sample code?

Answer1:

You can use this awk:

awk 'FNR==NR{for (i=1; i<=NF; i++) a[$i]; next} $1 in a{delete a[$1]} 1; END{for (i in a) print i "="}' FS=' *, *' params.txt FS=' *= *' props.txt

<strong>Output:</strong>

db.hostname=hostname db.username=username db.password=password codesettimgsB= db.url= codesettingsA= db.status=

Answer2:

Try this :

IFS=',' read -ra params < parameters.txt for param in "${params[@]}"; do grep "$param" properties.txt || echo "$param" >> properties.txt done

Parameters are parsed and stored into a params array.

For each parameter, the loop checks if it's present in properties.txt and, if not, append it at the bottom of the file.

Answer3:

cat properties.txt <(grep -vf properties.txt <(tr ',' '\n' <parameters.txt))

tr .. converts csv formatted records to newline separated, grep .. find the lines not in parameter.txt and cat .. appends them

Recommend

  • Create a table from 2 different tables
  • windows batch file for mysqldump command
  • @echo off in cmd
  • Methods for storing data in java
  • Bash script: perform actions based on file contents
  • cmd insert text into the middle of the file name
  • Creating a C++ function that calls other Lua function
  • Enumerating Controls on a Form
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • How to use function wrapper in mustache.php?
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Creating a DropDownList
  • Check all values in string[] for length?
  • Image map in Flex
  • Who propagate bugfixes across branches (corporate development)?
  • order post according to custom array position
  • Build Successful but not running on simulator
  • Django model inheritance, filtering models
  • Jquery popup on mouse over of calendar control
  • Check for zero lines output from command over SSH
  • Xcode 4 NSLog Macro link in Xcode 3
  • How to clear text inside text field when radio button is select
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Avoid links criss cross / overlap in d3.js using force layout
  • Scrapy recursive link crawler
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • What is the “return” in scheme?
  • Check if a string to interpolate provides expected placeholders
  • Statically linking a C++ library to a C# process using CLI or any other way
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Understanding cpu registers
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases