Solved

Grep Not Working After Changing Variable

Posted on 2013-05-10
5
329 Views
Last Modified: 2013-05-11
From the command line, cat test | grep 'hello' works. But not when I change it within my script. Basically, trying to resolve an issue is user does a grep without using the ' (needed for multiple search).

I'm trying to manipulate grep so that this command would work: bash run hello:new

#!/bin/bash

if [ -n "$1" ]
then
  string=`echo "'${1/:/\\|}'"`;
  echo $string;
  cat test | grep $string;
fi

Open in new window

0
Comment
Question by:Neapolitan23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 39157642
Try
string=${1/:/\\|}
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 250 total points
ID: 39157768
I don't think you need either the single quotes or the backslashes.  I just replaced the middle section with:
    string=${1//:/|}
    echo $string
    cat test | egrep "$string"

Open in new window

Note the "//" after the 1 in the first line - this replaces all colons with vertical bars, not just the first one (if there is more than one).  Also the "egrep" (or "grep -E" if you prefer), which is needed to search for these multiple strings.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39157773
Also,
 cat test |
is a Useless Use Of Cat when you can do
 grep $string < test
or
 grep $string  test


Even easier, you might
egrep $1
and call the script with  'peter|chris' instead of peter:chris
0
 

Author Comment

by:Neapolitan23
ID: 39157913
Dude, you are amazing. Thanks!
0
 

Author Closing Comment

by:Neapolitan23
ID: 39157922
ozo: Your solution worked. And it works for multiple search even with regular grep.
simon3270: Your solution worked as well. The added value here was adding the extra / so that it can support multiple replace of :.

Very cool. And thanks to you both!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question