LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2013, 05:39 PM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
Search for specific string containing "/" on remote hosts


Hello everyone,

I'm trying to set up a script to search a file in several remote hosts for a specific text:

Code:
I 0 0 \ 7 \
The problem is in order to run a grep command on a remote host I need to put the command in quotations. But in order to grep the specific string I need the characters to also be in quotations.

When I do the following below it brings back all the lines from the file. I was hoping someone had a better idea how I can accomplish bring back only the lines that contain I 0 0 \ 7 \ in them on the remote hosts. Thanks.

Code:
for STORE in `cat /tmp/cbrain-only-storelist`
do
echo ====================
echo $STORE
                ping -c 1 $STORE > /dev/null 2>/dev/null
PingResult=$?
                if [ $PingResult -eq 0 ]; then
                        ssh root@$STORE grep "I 0 0 \ 7 \" /tmp/engine.log 
        else
                echo ==============
                        echo "Store down"
fi
done

Last edited by keif; 11-26-2013 at 05:41 PM. Reason: removed file direct
 
Old 11-26-2013, 06:05 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Use single quotes and delimit your slashes
Code:
grep 'I 0 0 \\ 7 \\' file
 
1 members found this post helpful.
Old 11-26-2013, 06:21 PM   #3
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Use single quotes and delimit your slashes
Code:
grep 'I 0 0 \\ 7 \\' file
Thanks for the reply.

This works if I am logged into the host and run the command. But it doesn't work when I try and put it into a script and run it from a remote host.

When I try it with just the single quotes, it brings back a bunch of lines that aren't related to the specific string:

Code:
ssh root@$STORE grep -a 'I 0 0 \\ 7 \\' /tmp/engine.log
When I run it with quotes around the whole command it doesn't bring back anything:

Code:
ssh root@$STORE "grep -a 'I 0 0 \\ 7 \\' /tmp/engine.log"
Thanks for any information you can provide. I'm still searching around online too.

Thanks.
 
Old 11-26-2013, 07:49 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
In that case, you'll need to delimit the delimiter

Code:
ssh user@IP "grep -a 'I 0 0 \\\\ 7 \\\\' tempfile"
ssh will use the delimiters to pass the literal "I 0 0 \\ 7 \\" to grep, then grep will use the delimiters to search for "I 0 0 \ 7 \".

Last edited by suicidaleggroll; 11-26-2013 at 07:50 PM.
 
1 members found this post helpful.
Old 11-27-2013, 10:57 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Since you are matching a plain string and not a regexp, you can pass -F to grep and avoid one layer of escaping.

grep(1):
Quote:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX .)
 
1 members found this post helpful.
Old 11-28-2013, 07:52 AM   #6
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
In that case, you'll need to delimit the delimiter

Code:
ssh user@IP "grep -a 'I 0 0 \\\\ 7 \\\\' tempfile"
ssh will use the delimiters to pass the literal "I 0 0 \\ 7 \\" to grep, then grep will use the delimiters to search for "I 0 0 \ 7 \".
This worked great. And thank you for explaining the workings behind it so I can understand it better.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Awk: Search for string containing "/" shivaa Linux - Newbie 5 12-08-2012 10:45 PM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
vim search string question with /" and \" m_lp_ql_m Programming 2 08-03-2006 12:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration