Using echo, grep and wc and outputing to text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using echo, grep and wc and outputing to text file
# 1  
Old 05-14-2014
Using echo, grep and wc and outputing to text file

My current line command is as follows:

Code:
echo -n "text: " ; grep "blah text" ../dir1/filename | wc -l

The output to the screen is as needed, but how do I print to a text file?
# 2  
Old 05-14-2014
Huh? Smilie

There's not much to go on here. Do you want to:-
  • display every file that matches your search
  • count all the files that match your search
  • list every file and the counts that match your search
  • something else?
Go on, give us a clue.



Robin
# 3  
Old 05-14-2014
The screen output of

Code:
text: 3102

needs to be output to a text file.
# 4  
Old 05-14-2014
Oh right then! The operator > can be added to the end of a command line to capture the output to a named file, e.g.:-
Code:
echo Hello > /tmp/my_file

If you want it to display it to the screen too, you can do this:-
Code:
echo Hello | tee /tmp/my_file

Does that help, or have I missed the point?



Robin
# 5  
Old 05-14-2014
Code:
echo -n "text: " > test.txt ; grep "blah text" ../dir1/filename | wc -l

gives me screen output of
3102

but file output of
text:

I need the entire output to go to the file.
# 6  
Old 05-14-2014
Yes, we can manage that:-
Code:
( echo -n "text: " ; grep "blah text" ../dir1/filename | wc -l ) > test.txt

Have I got it right now?

You can also trim the command a bit:-
Code:
( echo -n "text: " ; grep -c "blah text" ../dir1/filename ) > test.txt



Robin
These 2 Users Gave Thanks to rbatte1 For This Post:
# 7  
Old 05-14-2014
Or redirect stdout itself so all lines below print into the log file:

Code:
exec > file.log

echo "text: "
grep "blah text" ../dir1/filename | wc -l

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Grep the ip to a text file

Hi all, We Have Squid server, We need to get the particular IP's log from /var/log/squid/access.log, if i need to get only the log's of 192.168.0.99, How can i get the log's to a separate file. Here is the sample log what i have got from access.log file 1392706763.690 847... (2 Replies)
Discussion started by: babinlonston
2 Replies

2. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

3. Shell Programming and Scripting

Echo variable contents into a text file...

Hi all, I am trying to create a script to read my Windows UUIDs and create mounts in fstab. I know there are different and maybe even better ways to mount Windows partitions at boot time, but I always manually create them in fstab successfully. I do a clean install of Ubuntu often and would like to... (2 Replies)
Discussion started by: Ian Pride
2 Replies

4. Shell Programming and Scripting

sed not outputing variable into new file

Hi all, I have a script that creates folders depending on what variables i enter when calling the script. This all works as expected but what i want to do is add some additional data into another file so that other scripts are aware of this file. I've found the following sed command which... (12 Replies)
Discussion started by: springs2
12 Replies

5. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

Grep text from file

Hi, I used the grep command and got the result below. However, I just want the following information: Build Version: DnETest Build Build Date-Time: 01/27/2012 07:56:14 AM Any suggestion from the current out put I have: <td><span class="style29"> Build Version: </span></td>... (1 Reply)
Discussion started by: xitrum
1 Replies

7. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

8. Shell Programming and Scripting

Text file with grep

Sirs, i am trying to create simple script file.. what i do is grep for a pattern and output 1 line after it. exmp: grep -A 1 time files.txt Output: time file1 time file2 --- Is there some option I can use so I can get on result each 2 lines combined as one file? like: time file1... (2 Replies)
Discussion started by: alekkz
2 Replies

9. Shell Programming and Scripting

help with grep and echo

Can't get this to work. Something is wrong with the systax maybe. :o if ] && ] ; then: Thanks (2 Replies)
Discussion started by: rstone
2 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question