How to grep for this string that contains an equal sign? - linux

Below is the string I am trying to grep for this in the bash shell:
'#Hostname=sometext.company.com, sometext.company.com' filename
I want to only find the string if it matches that exact pattern. I already tried the command below and a few others.
grep -Fx "#Hostname=sometext.company.com, sometext.company.com" filename

Did you specify the -xoption on purpose?
grep -F '#Hostname=sometext.company.com, sometext.company.com' filename
most likely is what you want. Also, it's better to put single quotes instead of double quotes, just in case your search pattern happens to contain special shell characters.

Related

Shell How to get a new path by replacing a substring in a path string

I have a path like this:
dirname=../2Reconnaissance-annoted/J39/IMG_2208.json
I want to get a new path by replacing ".json" with "_json", so I tried this command:
tr "\.json" "_json" <<<$dirname
The problem is that I get:
__/2Reconnaissance-annoted/J39/IMG_2208_json
Rather than
../2Reconnaissance-annoted/J39/IMG_2208_json
How do you fix it, please?
tr does transliteration, i.e. it replaces a character by a character, not a string by a string. What you need is substitution.
Most shells support substitution directly:
dirname=${dirname/.json/_json}
(${dirname/%.json/_json} would only substitute the substrings at the end of the string).
If your shell doesn't support it, you can use sed:
echo "$dirname" | sed -e 's/\.json$/_json/'

Do bash script and command grep treat single quote differently

In Advanced Bash-Scripting Guide, I find
Within single quotes, every special character except ' gets
interpreted literally.
So I think grep '\<the\>' file.txt would search \<the\>, instead of word the. But it searches the indeed.
#!/bin/bash
grep '\<the\>' file.txt
Added
Maybe I don't describe my question clearly.In man page,
Enclosing characters in single quotes preserves the literal value of each character within the quotes.
So my question is: Now that bash would regard enclosing characters in single quote as the literal value, why '\<the\>' is treated as the in grep? Is it grep own characteristic,differing from bash?
Indeed, bash will pass your string literally.
It is grep that interpretes the string (as a regular expression). If you want to avoid that, use grep -F. With that option, grep will search literally for the given string.
You need to add another backslash \ to match the whole pattern, as the symbols \< and \> are special to grep. Quoting the manpage: man grep
The Backslash Character and Special Expressions
The symbols \< and \> respectively match the empty string at the beginning and end
of a word.

How to get list of files which include string with quotes (using grep command)

I have lost of xmls in Unix env, and I want to get list of the files which include the next string:
Value value="99.00"
My problem is that this string include quotes, which causing me truobles with the grep command. any idea which command I should use?
SHELL=/bin/ksh
Use backslash to escape the ":
grep Value\ value=\"99.00\" *.xml
Or enclose it in single quotes:
grep 'Value value="99.00"' *.xml

How to get grep consider any character as regular

I am trying grep some string in a script.
I don't know what string i am finding or what is in file where I am trying to find it. I just need grep exact string from file.
So, my problem is, sometimes greping string contains square brackets and as I found out, grep consider them special characters.
string='some [text]'
grep "$string" file
I can escape them with sed
string='some [text]'
grep "$(sed -e 's/\[/\\[/g' <<< "$string")" file
I need grep to match exact string no matter what input can be. So is there a nicer way to do it? some way to tell grep to consider every character in string as regular character? If no, are there any other special characters like [ I need to worry about?
You can use -F option, to interpret it as fixed string:
grep -F "$string" file

Replace a phrase in a file with a string which contains special Characters

I am using sed -e "s/foo/$bar/" -e "s/some/$text/" file.whatever to replace a phrase in a certain file. The problem is that the $bar string contains multiple special characters like /. So when I try to replace something in a text file using the following code...
#!/bin/bash
bar="http://stackoverflow.com/"
sed -e "s/foo/$bar/" -e "s/some/$text/ file.whatever
...then I get an error saying : sed: unknown option to s is there anything I can do about it?
You can use any delimiter. s#some#SOME# for example. Another good delimiter is vertical-bar. Other chars can work but have special significance for some contexts such as regular expressions.
You can get this difficulty in sed regardless of what delimiters you use, especially if you don't know what the string contains. I'd pick a different method for passing the shell variables into the helper interpreter.
awk -v rep1="$bar" -v rep2="$text" '{sub(/foo/, rep1); sub(/some/, rep2); print}'
or
perl -spe 's/foo/$rep1/; s/some/$rep2/' -- -rep1="$bar" -rep2="$text"
Correctness trumps brevity in this case.
(reference for Perl example)

Resources