5

I want to grep a file, and I want to get all the lines that have a certain environment variable (to be exact, $PWD).

Of course, using just

cat file | grep '/'$PWD'/'

is not working, since $PWD contains slashes.

I am trying to figure out how to do it correctly, but I come up only with weird and over-complicated solutions. What's some simple way to do this?

2
  • 3
    if you are looking for instance of $PWD inside a file, why add the leading and trailing / to search string?
    – iruvar
    Commented Nov 22, 2014 at 3:53
  • that....is a good question. I thought it's necessary to have the trailing /, apparently it's not. Commented Nov 22, 2014 at 3:57

2 Answers 2

12

Just use double quote instead of single quote, and you don't have to use cat (See UUOC):

grep -F -- "$PWD" file

And remember that without -F, $PWD would be treated as a regular expression as opposed to a string to be looked for in the file.

5
  • 1
    It works, but only with removed slashes; at which point I don't need to use the quotes at all. Commented Nov 22, 2014 at 3:58
  • 2
    @KarelBílek: General, you don't. But using quotes will protect you if your variable contain special character. And you should always use double quotes when you don't want word splitting occurs.
    – cuonglm
    Commented Nov 22, 2014 at 4:02
  • With Stéphane's edit, this answer is the better one. Thanks to both Commented Nov 22, 2014 at 20:38
  • Why do you need to use -- ?
    – CodeGodie
    Commented Aug 2, 2018 at 19:42
  • @CodeGodie because PWD can be set to anything
    – cuonglm
    Commented Aug 3, 2018 at 1:55
2

Since forward slash is not a special character, you can use it freely:

grep "$PWD/" file

Adding the tailing / will not cause any problem. However forward slash is a special character in awk and sed. Patterns are enclosed by / /. In this situation, you can use

awk -v pattern="$PWD/" '$0 ~ pattern' file

to avoid the awk /pattern/ {action} grammar. Here "~" is the match operator, $0 means the whole line.

4
  • 3
    It doesn't make sense for $PWD to be treated as a regex here. So, that should be grep -F -- "$PWD/" file or awk 'index($0, ENVIRON["PWD"])' Commented Nov 22, 2014 at 9:06
  • I have to say grep -F runs faster and works in most cases, but if the dir names contains some special characters, eg: current dir is foo\nbar. grep -F "$PWD" will match the line containing foo or bar. I have tested it with (1) mkdir "echo -e "foo\nbar"", (2) create a file with four lines: 1. foo, 2.abc, 3. foo, 4 abc (3) grep -F "$PWD" returns line one and line three. Commented Nov 22, 2014 at 15:35
  • That's true with or without -F. grep -F -- "$PWD" file returns all the lines in file that contain any of the lines in $PWD. grep -- "$PWD" file return all the lines in file that match any of the regexps in $PWD (newline separated). Commented Nov 22, 2014 at 20:11
  • @Stéphane Chazelas You are right. Missing -F option brings a lot of problems, esp. when dir names have + ? { etc chars. Commented Nov 23, 2014 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .