0

I'm trying to learn regular expressions, and so I created a file with some characters in it. When I use the cat command and pipe it to rgrep command with an argument to filter out text, the resulting output is "filename:filtered_text".

cat regex.txt | rgrep -E '^a'
regex.txt:a
regex.txt:aa
regex.txt:aaa
regex.txt:aaaa
regex.txt:aaaaa
regex.txt:aaaaaa
file:acpi
file:adduser.conf
file:aliases
file:aliases.db
file:alternatives
file:anacrontab
file:apache2
file:apg.conf
file:apm
file:apparmor
file:apparmor.d
file:apport
file:appstream.conf
file:apt
file:avahi

Not only that it also checks for other files in the folder and if they match the regular expression it shows them as well, even though I only piped a single file.

How do I get a normal output with only the contents of the file I've passed into cat?

  • Can I ask what you are intending with the -R flag? – BowlOfRed Sep 28 '18 at 17:36
  • 1
    cat doesn't output the file names. – RalfFriedl Sep 28 '18 at 17:38
  • @BowlOfRed That was a mistake. I needed to use the -E option (for more options according to the video I'm watching). The output is still the same without any options. – Skaper Sep 28 '18 at 17:40
  • Have you tried using cat < regex.txt? – unxnut Sep 28 '18 at 17:55
  • @unxnut I did, just now, and got the same output. This time it even checked subdirectories. – Skaper Sep 28 '18 at 17:58
2

You need to use the -h flag with rgrep, as it is the rgrep that prints the file names. This flags stops this.

I can only guess why you use rgrep as opposed to the simple grep and cat. So, first, you don't need cat. grep PATTERN FILE is enough. Second rgrep is equivalent to grep -r. And here's what the -r flag to grep does:

-r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.

For a simple solution, try:

grep '^a' regex.txt

(It's unclear with this pattern, why you would use the -E switch.)

  • That took out the file name but it still reads from all the files in the folder. I used '^a' and got lines starting with 'a' from all the files. – Skaper Sep 28 '18 at 17:53
  • @Skaper Use normal grep – Tomasz Sep 28 '18 at 17:58
  • I used it but it gives me the whole output of the file with the characters matching the regular expression highlighted as red color. Shouldn't it only output the lines matching the regex? – Skaper Sep 28 '18 at 18:03
  • @Skaper You need to ask a full question with what you do, what you expect and why, and what you get. I attempted a better answer with my edit. – Tomasz Sep 28 '18 at 18:06
  • @Skaper See a solution added. – Tomasz Sep 28 '18 at 18:11
-1

rgrep is a shortcut (on my ubuntu 18.04: a script) for grep -r. man grep:

   -r, --recursive
          Read  all  files  under  each directory, recursively,

In fact, the use of cat is pointless, as rgrep seems to ignore it.

  • I'm using Linux 18.04.1 LTS. Are you talking about predefined aliases? If yes, then I don't have any for rgrep command. – Skaper Sep 28 '18 at 17:49
  • No, it's a script in /usr/bin. – RudiC Sep 28 '18 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.