0

I have a text file 1.grep

grep -P -e "^<job.+type.+rule" "Emake-4agents-1st-10-25-51.53.xml"

To make my grepping go faster, I do the following in bash

cat 1.grep | bash > 1.search

This works fine normally but in this case, I get the following:

$ cat 1.grep
grep -P -e "^<job.+type.+rule" "Emake-4agents-1st-10-25-51.53.xml"

$ cat 1.grep | bash > 2.search
: No such file or directory25-51.53.xml

Why does bash think that my .xml filename is a directory?

Bob
  • 4,576
  • 7
  • 39
  • 107
  • 2
    you don't want to do `cat foo|bash`, you can make the `foo` as a shell script with hashbang, and `+x` – Kent Jun 16 '16 at 15:46
  • `cat`_ing_ to `bash` won't help. Just do `grep -P -e "^ 1.search` – agc Jun 16 '16 at 16:00
  • @agc this is not practical when I have many other parameters. This is a trivial example. – Bob Jun 16 '16 at 16:02
  • `bash 1.grep` will execute the command in 1.grep. Why do you need the cat ? – matzeri Jun 16 '16 at 16:29
  • @matzeri you're right it does. Problem is I still get the same error `$ bash 1.grep : No such file or directory25-51.53.xml` – Bob Jun 16 '16 at 18:49

1 Answers1

2

The immediate problem is that the file 1.grep is in DOS/Windows format, and has a carriage return followed by linefeed at the end of the line. Windows treats that two-character combination as the end-of-line marker, but unix tools like bash (and grep and ...) will treat just the linefeed as the end-of-line marker, so the carriage return is treated as part of the line. As a result, it's trying to read from a file named "Emake-4agents-1st-10-25-51.53.xml^M" (where ^M indicates the carriage return), which doesn't exist, so it prints an error message with a carriage return in the middle of it:

cat: Emake-4agents-1st-10-25-51.53.xml^M
: No such file or directory

...where the carriage return makes the second part overwrite the first part, giving the cryptic result you saw.

Solution: use something like dos2unix to convert the file to unix (line-feed-only) format, and use text editors that store in the unix format.

However, I also have to agree with several comments that said using cat | bash is ... just plain weird. I'm not sure exactly what you're trying to accomplish in the bigger picture, but I can't think of any situation where that'd be the "right" way to do it.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • yay! thanks. I use notepad++ extensively and I followed this http://stackoverflow.com/questions/8195839/choose-newline-character-in-notepad to always have it use `\n` for new line. `bash ` works now. – Bob Jun 16 '16 at 18:52
  • For future reference, you can always use `tr` (or sed) to get rid of the ^M characters: `tr '\r' '\n' < file.txt`. Also, as said before, avoid the use of `cat file | something else. Google UUOC :) – Vinicius Placco Jun 16 '16 at 19:02