Visit Our UNIX and Linux User Community


What does this do (newbie question)...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What does this do (newbie question)...
# 1  
Old 02-23-2012
What does this do (newbie question)...

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

Code:
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.

Last edited by methyl; 02-23-2012 at 08:44 PM.. Reason: please use code tags
# 2  
Old 02-23-2012
the
Code:
ls *.txt | grep text1

is separate from
Code:
cat file1 – file2 | `echo wc –l`

It doesn't make sense to pipe from
Code:
 grep text1

and further.

Also when it comes to
Code:
 cat file1 – file2

, it doesn't make sense
the code
Code:
cat file1 file2

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

# 3  
Old 02-23-2012
^ 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 Smilie
# 4  
Old 02-23-2012
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.
This User Gave Thanks to alister For This Post:
# 5  
Old 02-23-2012
Quote:
Originally Posted by alister
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
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).

Last edited by elohssa; 02-23-2012 at 08:01 PM..
# 6  
Old 02-23-2012
Changing the order of file arguments may not error out, but it's not equivalent.

Code:
$ 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
# 7  
Old 02-23-2012
@elohssa
Quote:
ls *.txt | grep text1 | cat file1 – file2 | `echo wc –l`
This code does nothing useful and gives syntax errors.


Code:
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:
Code:
cat file1 file2 `ls *Text*` | wc -l


Last edited by methyl; 02-23-2012 at 09:15 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #513
Difficulty: Easy
Most programming languages are not case sensitive.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

newbie question

Hi all, I am sure this is very simple but I cant quite get it. I am trying to search textfile1.txt for a string then take the results of the search and append the result to textfile3.txt So far I have used $ find file1.txt -exec grep "string i am looking for" '{}' \; -print this... (2 Replies)
Discussion started by: radgator
2 Replies

2. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

3. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

4. Programming

Newbie question

Dear all, I have a question related to parallel programing and if you can give me some hints on how to deal with it, it would be really great. I would like to run a small application on a supercompter of 128 CPUs. Unfortunately, on this machine only jobs which require 32 CPUs are allowed to... (1 Reply)
Discussion started by: Eduard
1 Replies

5. UNIX for Dummies Questions & Answers

Newbie question?

What is the best way to learn UNIX on the web, with out buying books? any link would be much help. Thank you in advance, L (1 Reply)
Discussion started by: lsoria1
1 Replies

6. UNIX for Dummies Questions & Answers

newbie question

I am taking a db classes toward oracle 10g. I am taking unix as well . I need to know what is the best option for os . should I use linux fedora. or get a sun box and start learning from there. Thanks (6 Replies)
Discussion started by: xzyan
6 Replies

7. UNIX for Dummies Questions & Answers

Very new newbie question

sorry if im not asking inthe right spot but, how do you turn the beeping off every time you hit a key onthe keyboard. I tried the click -n but it told me it didnt recognize click any help would be greatly appreciated ( the beeping is not going over well in the surrounding cubicles) thank you... (4 Replies)
Discussion started by: Split100
4 Replies

8. Shell Programming and Scripting

Newbie question

Hello, I have text file while looks this test1 test2 test3 test4 test5 test6 and if I want to parse it and make new file which would like this test1 test2 test3 test4 test5 test6 How can I do this in korn shell script Thanks (9 Replies)
Discussion started by: peeyush_23
9 Replies

9. UNIX for Dummies Questions & Answers

Newbie Question

Hi, I have a file, that is delimited by :: and the purpose of this file is none of your business. ;) There are about 65000 lines in this file, and there are lines that I would like to remove. About 45000 of them. Is there some sort of commands that I can run, to remove word(s) from this... (4 Replies)
Discussion started by: th3gh05t
4 Replies

10. UNIX for Dummies Questions & Answers

newbie question

hi im thinking of getting unix but i have no idea where to start I know that its an OS similar to linux but what hardware does in run on? i've heard of solaris but im not quit sure what it is thankxs (3 Replies)
Discussion started by: ninja
3 Replies

Featured Tech Videos