I have a string variable in a bash script file as follows:
string="
test1
test2
"and I want to check whether a file test.txt containts this specific string (including the linebreaks. i.e. it should fail if it only contains the following:
this is a test:
test1
test2
and another onebecause the linebreaks above test1 and below test2 aren't present.
(The reason I want to check this is because I want to check whether a certain piece of code is in a source file, and if not, add it.)
The following doesn't work:
string=" test1 test2 "
if ! grep -q string "test.txt"; then echo "$string" >> test.txt
fiThis correctly adds the string to the file, but it does it even if the string has already been added. Also, it performs correctly when I change the string to have no linebreaks.
EDIT:
The answers by @terdon and @steeldriver below work for the string example I wrote above, but they for some reason break for this more realistic example:
string="
if [ -f ~/.script ]; then . ~/.script
fi
" 5 3 Answers
The problem is that grep will run on each line, not the entire file. As long as the file is small enough to fit into memory (which should be the case in the vast majority of situations these days), you can use grep's -z flag to slurp the entire file:
-z, --null-data Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used with commands like sort -z to process arbitrary file names.
The next issue, is that if you pass grep something with newlines, it will treat it as a list of patterns to grep for:
$ string="1
> 2"
$ seq 10 | grep "$string"
1
2
10
"Which means that I am afraid you will have to express the pattern as a proper regular expression:
\n\ntest1\n\ntest2\n\nHowever, this also means you need the -P flag to enable perl-compatible regular expressions so the \n will work.
I created these two files to demonstrate:
$ cat file1
this is a test:
test1
test2
and another one
$ cat file2
this is a test:
test1
test2
and another oneUsing those two files and the information above, you can do:
$ grep -Pz '\n\ntest1\n\ntest2\n\n' file1
$
$ grep -Pz '\n\ntest1\n\ntest2\n\n' file2
this is a test:
test1
test2
and another onePutting all this together gives us:
string='\n\ntest1\n\ntest2\n\n'
if ! grep -Pzq "$string" test.txt; then printf "$string" >> test.txt
fiOr, as suggested by @steeldriver in a comment, you can use a variable and convert the newlines to \n on the fly:
$ string=" test1 test2 "
$ if ! grep -Pzq "${string//$'\n'/\\n}" test.txt; then printf "$string" >> test.txt
fiIf your string contains special characters which have meanings in regular expressions, as you now show in your updated question, then that's a whole different situation. For the example you show, you would need something considerably more complicated. Like this:
searchString='\n\nif \[ -f ~/.script \]; then\s*\n\s*\.\s+~/\.script\s*\nfi\n\n'
printString='
if [ -f ~/.script ]; then . ~/.script
fi
'
if ! grep -Pzq "$searchString" test.txt; then printf "%s" "$printString" >> test.txt
fi 10 You might want to consider using pcregrep with the -M or --multiline option to allow matching of literal newlines:
-M, --multiline Allow patterns to match more than one line. When this option is given, patterns may usefully contain literal newline char‐ acters and internal occurrences of ^ and $ characters.Ex. given
$ cat test.txt
this is a test:
test1
test2
and another one test1 test2 and
$ cat test2.txt
this is a test:
test1
test2
and another one test3 test4 with
$ string=" test1 test2 "then
$ pcregrep -qM "$string" test.txt && echo 'found' || echo 'not found'
found
$ pcregrep -qM "$string" test2.txt && echo 'found' || echo 'not found'
not found 4 Searching for multiline patterns in a file might be easier with awk:
awk '/Start pattern/,/End pattern/' filenameCheck this post for further details
0