What does this do (newbie question)...

I was looking through some code online and came accross this...

ls *.txt | grep text1 | cat file1 � file2 | `echo wc �l`

I know what "ls|grep text1" does and I know a word count gets echoed but beyond that I am confused. Please use layman terms as much as possible as I am a newbie.

the

ls *.txt | grep text1

is separate from

cat file1 � file2 | `echo wc �l`

It doesn't make sense to pipe from

 grep text1

and further.

Also when it comes to

 cat file1 � file2

, it doesn't make sense
the code

cat file1 file2

would dump the contents of both file1 and file2 to output, but I don't see any sense in

 cat file1 � file2

^ Yea, as far as I understand it the first 2 statements output all the text files that have "text1" in the filename. After that it just makes no sense :confused:

The ls outputs all .txt files. The grep filters out those whose names do not match text1. The cat is then sandwiching the output of grep between the contents of file1 and file2. The echo command prints "wc -l" which is then run to count lines.

Regards,
Alister

---------- Post updated at 06:54 PM ---------- Previous update was at 06:52 PM ----------

`echo wc -l` should just be wc -l . For more info on that construct, look up 'command substitution' in your shell's manual page.

1 Like

After first seeing the code I also felt this could be it but this command doesn't seem to work in the shell as I get the following...

cat: �: No such file or directory
wc: �l: No such file or directory

Any ideas?

Edit when I rearange slightly: "ls *.txt | grep Text | cat - file1 file2 | wc -l" it works (the "-" coming before file1).

Changing the order of file arguments may not error out, but it's not equivalent.

$ cat f1
1
2
3
$ cat f2
1
2
3
$ echo a b c | cat - f1 f2
a b c
1
2
3
1
2
3
$ echo a b c | cat f1 - f2
1
2
3
a b c
1
2
3

If your cat does not behave in this way, then it's either broken or obsolete. If your problem persists, specify what operating system and shell you're using.

Regards,
Alister

@elohssa

This code does nothing useful and gives syntax errors.

ls *.txt | grep Text | cat - file1 file2 | wc -l
ls *.txt | grep Text | cat file1 - file2 | wc -l

Both of these are very dubious code. They both count the number of lines in file1 and file2 and then add the number of filenames which contain the string "Text". The number of lines in the files whose names contain the word "Text" is ignored.

It always helps to post what Operating System and version you are using and what Shell you prefer. It also helps to explain what you expect from the script.

Ps: A variant which totals the number of lines in a file whose name contains the string "Text" added to the number of lines in file1 and file2 would be:

cat file1 file2 `ls *Text*` | wc -l

I can't argue regarding its utility since the intent is unclear, but I see nothing syntactically wrong (the command substitution is utterly pointless, but deleting it and the echo does not alter the result).

Regards,
Alister

Useful post alister. I pasted the code into the wrong Shell at the time and did not match whatever Shell this poster is using. Agreed that the backticks and echo are pointless in any modern Bourne-type Shell.