"find | grep | awk" fails miserabily

0

I'm scanning for all shell scripts on my server, and are doing this by the following command:

find / -type f -exec file --mime-type {} \; | grep "text/x-shellscript"

This is working fine, and here is a sample output from this:

/lib/udev/hwclock-set: text/x-shellscript
/lib/init/bootclean.sh: text/x-shellscript
/etc/network/if-up.d/openssh-server: text/x-shellscript
/etc/network/if-up.d/mountnfs: text/x-shellscript

Now, I want to work on the filenames, and try to use awk for this:

find / -type f -exec file --mime-type {} \; | grep "text/x-shellscript" | awk -F: '{ print $1 }'

This however, does not produce any output. Ive tried redirecting pipes etc, but here I've hit the wall.

Anyone have an idea of what I'm doing wrong?

Dog eat cat world

Posted 2011-10-16T13:35:16.327

Reputation: 266

You are probably having colours in the grep output. Try to use \grep instead so that the normal output appears. – fedorqui – 2015-07-28T12:53:57.790

What happens with echo "/etc/network/if-up.d/mountnfs: text/x-shellscript" | awk -F: '{ print $1 }'? – Daniel Beck – 2011-10-16T13:51:04.297

@DanielBeck That works. Maybe find does not write to stdout? But then I can't explain how grep to do its part... – Dog eat cat world – 2011-10-16T13:53:37.850

You can redirect error output to standard output. Your command will then look like this: find / -type f -exec file --meta-type {} \; | grep "text/x-shellscript" 2>&1 | awk -F: '{ print $1 }' – Daniel Beck – 2011-10-16T14:03:06.643

@DanielBeck No, it does not help. I've tried "find / -type f -exec file --mime-type {} ; | grep "text/x-shellscript" 2>&1 1>this_file_should_not_be_empty", but it seems like grep is outputting to nowhere! Even if the text is displayed on the monitor without pipe redirection. – Dog eat cat world – 2011-10-16T14:20:35.550

You can narrow your problem locus, and get rid of one of those tags on the question, by eliminating the useless use of grep there.

– JdeBP – 2011-10-16T15:41:04.813

@JdeBP, I agree that it can be done without grep. I've narrowed the problem down to grep, but unable to understand what goes wrong. I'm still looking for the answer why it does not work. – Dog eat cat world – 2011-10-16T20:50:26.967

You've narrowed the problem to the wrong thing. Think carefully. – JdeBP – 2011-10-17T07:16:04.137

Answers

2

Try this:

  find / -type f -exec file --meta-type {} \; | grep "text/x-shellscript" | cut -d: -f 1

haimg

Posted 2011-10-16T13:35:16.327

Reputation: 19 503

Thanks, this works. But I'm still curious why awk doesn't – Dog eat cat world – 2011-10-16T13:54:37.287

Your awk command works fine here, there's nothing wrong with it. If you're embedding this pipe into some other command (backticks, etc.) then the only difference I can think of is the single quotes you use in awk command. – haimg – 2011-10-16T14:00:02.747