Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

542
Views
Replace key:value from one file in another file in shellscript?

I have a "database file" with key-value entries and I have another file where the keys appear among other content. Now I want to replace that keywords with the values of my key-value file. Example:

There is one file "keys.txt" with "keyword space url":

name1 https://maps.google.com
something http://www.domain.com/bla.php?value=500
blabla http://thisis.example.com/moooooar.asp

I have another file "text.txt" which has one or several words per line, like this:

notrelated somewhatrelated blabla
blabla unimportant
asdf asdf asdf name1 dadadada

And as a result I want to have something like this:

notrelated somewhatrelated http://thisis.example.com/moooooar.asp
http://thisis.example.com/moooooar.asp unimportant
asdf asdf asdf https://maps.google.com dadadada

Actually my question is very similar to https://stackoverflow.com/questions/5283653/ but non of the solutions mentioned there did work out since I am working with URLs (slashes, colons...) I guess?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

awk '
    NR==FNR {url[$1]=$2; next} 
    {for (i=1; i<=NF; i++) if ($i in url) $i=url[$i]; print}
' keys.txt text.txt 
notrelated somewhatrelated http://thisis.example.com/moooooar.asp
http://thisis.example.com/moooooar.asp unimportant
asdf asdf asdf https://maps.google.com dadadada
over 4 years ago · Santiago Trujillo Report

0

Well, this works (but will probably break if you have commas in your keys):

while read k v; do sed -e "s,$k,$v,g" -i text.txt; done < keys.txt
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!