0

I need to make a bash script that reads a file containing variable = text. What I need is for the script to check the file, ignore the variable name and search for a word in the text after the = symbol.

Example

thread.give_respect.title = Give respect
thread.receive_respect.title = Gain respect
thread.profile_respect.title = Show my Respect

As the first part is variable I don't want the word respect to be changed. Only the one after the = Also if possible I want to make a check if the word I want to change is with Capital or not so that I can change it accordingly.

Edit:

Esteban I tried your script but it creates the file with ${var at the beginning of each line.

For the other guys that are asking what I want to replace so:

We have huge files with string variables = their text. For example:

admin.help_text = help
admin.Help_text = Help
home.faq_title = FAQ
...

These files contain more then 15000 variables and their values. Sometimes we need to change a word to different one for example:

**help** to **guidance** 
**Help** to **Guidance**
**helper** to **guide**
**Helper** to **Guide**

What we need:

  1. Script that checks a file for: word1, word2, word3 and word4.

  2. If word exist on line to copy that line into a separate file.

  3. After everything is done to run find and replace for word1, word2 and etc. on the new file.

For example we have file (file1.txt) with all the variables and their content:

admin.help_text = help
admin.Help_text = Help
home.faq_title = FAQ
home.something.help = If you need help please send us email on...

When script start it will check for:

word1 = help
word2 = Help

And saves the lines that it found into file2.txt

admin.help_text = help
admin.Help_text = Help
home.something.help = If you need help please send us email on...

Then the script checks file2.txt and changes help to guidance and Help to Guidance

admin.help_text = guidance
admin.Help_text = Guidance
home.something.help = If you need guidance please send us email on...
3
  • what is the replacement value and how should look the final result? May 11, 2017 at 13:26
  • 1
    The title says "find", the text says "search for", and then there's something you don't want to "replace". So what it is you want to do? Replace some text in the part of the line after the equals sign? In purely the shell? Or with sed? (They're different tools.) Or with any standard or common tool?
    – ilkkachu
    May 11, 2017 at 13:31
  • Hey guys, I edited my question adding more information to it. :)
    – Apoc
    May 11, 2017 at 18:17

2 Answers 2

1

Since the edit of the question :

@Apoc If the number of patterns are limited, you can do the steps 1 and 2 with this command :

awk -F'=' '$2 ~ /word1|word2|word3|word4/' file1.txt > file2.txt

For the step 3 it depends on how you want and have to do it


I would do something like :

  • Loop each line
  • Retrieve the variable part
  • Make the modification you want
  • Print the line with the modification into a temporary output file
  • Replace the file by the temporary one

In shell script :


#!/bin/sh

# Loop through lines
cat file.txt | while read line; do
  # Store variable part of the line
  var=`echo $line | sed -r 's/^[^=][ ]?(.*)$/\1/'`

  # Make the modification you want on var
  # ...

  # Print in temporary file the modified line
  echo $line | sed -r 's/^([^=])/\1 ${var}/' >> temporary_file.txt
done

# Replace file by temporary one
mv temporary_file.txt file.txt
5
  • The issue with that is that you're running two copies of sed and a subshell (for the backticks), for every, single, line of the input file. And you need to take care about the subtleties of read and variable quoting. While at the same time sed is perfectly capable of running through a full file at one go. Of course, some problems may not be easy to solve in sed (or will look unreadable), but in that case it may be better to switch to awk or perl... See also: unix.stackexchange.com/questions/169716/…
    – ilkkachu
    May 11, 2017 at 15:51
  • I totally agree with you, it's an awful solution in term of performance, but here @Apoc has to handle a properties file which does not seem to contain enough line to be a problem in execution time, considering that it seems it will not be executed that many time also. For me it's a compromise between the time to spend finding the best solution and the time to spend finding a suitable working solution for the task at hands. But all in all, you're absolutely right, it could be done with an awk script for example
    – Esteban
    May 11, 2017 at 18:10
  • @ilkkachu : With the edit on the question, my previous comment is irrelevant ;) It should work, but with 15000 lines will likely take more time and ressources than necessary.
    – Esteban
    May 11, 2017 at 18:22
  • Hey guys, our files contain more than 15000 lines of variable = text. Sometimes text that is stored in a variable is 1000/2000 symbols. I added more information to my original post. I'm currently checking the URL from ilkkachu comment. Thank you guys!
    – Apoc
    May 11, 2017 at 18:24
  • @Apoc If the number of patterns are limited, you can do the steps 1 and 2 with this command : awk -F'=' '$2 ~ /word1|word2|word3|word4/' file1.txt > file2.txt. For the step 3 it depends on how you want and have to do it.
    – Esteban
    May 11, 2017 at 19:00
-1

From the comments to Esteban answer, the final answer used was:

awk -F'=' '$2 ~ /word1|word2|word3|word4/' file1.txt > file2.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .