Linux: How to append text to a file only if text does not exist

Assume the following scenario! you have multiple log lines coming from various sources but there is always the chance that there could be…

Linux: How to append text to a file only if text does not exist
Photo by Ayo Ogunseinde on Unsplash

Assume the following scenario! you have multiple log lines coming from various sources but there is always the chance that there could be duplicate input, so we need a mechanism that will append only non-existing text to the file.

I need a hero

The “hero” in our case is the grep command, with the bellow statement we can

  • -x : Ensure that the entire line matches
  • -F : Treat the pattern as a fixed string and not as a regular expression
  • -v : Inverts results
  • -f : The filename to search
echo 'input text' | grep -xFv -f hello.txt >> hello.txt

if input text not found in hello.txt then input text will printed in the stdout which in our case is appended to hello.txt.

Let's build a tool!

Now let's build a function that will accept as parameter input text and output file

append_unique_text() { 
    input_text="$1" 
    file="$2" 
     
    echo "$input_text" | grep -xFv -f "$file" >> "$file" 
}

This function accepts two parameters, the first one is the input text and the second one is the file that we want to append non-duplicate text.

Now create a file with the following content and save it as appender.sh

#!/bin/bash 
 
append_unique_text() { 
    input_text="$1" 
    file="$2" 
    echo "$input_text" | grep -xFv -f "$file" >> "$file" 
} 
 
append_unique_text "hello world" text1.txt 
append_unique_text "hello Greece" text1.txt 
append_unique_text "hello world" text1.txt

Make it executable with chmod

chmod +x ./appender.sh

Now execute the script, it should create a file named text1.txt that will contain the following content

hello world 
hello Greece

Conclusion

Even simple commands can create powerful tools when used properly! i hope you enjoyed this article and helped you to create better tools!