Line contains a word in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line contains a word in shell
# 1  
Old 03-15-2012
Line contains a word in shell

Hi,

I am looking for a solution to address following:

I have a file with several lines. If the line contains a specific word I would like to write the line to new file . Can someone please help me with this. So basically I am looking to print the file if it contains a specific word.

Thanks in advance

Last edited by davidtd; 03-15-2012 at 05:25 PM..
# 2  
Old 03-15-2012
Print the whole file, or just the matching line? Or just the word? Or the just the filename?

Whichever way you want, you can start from this:

Code:
# Append contents of filename to outfile if 'word' is found inside it
grep -l "word" filename > /dev/null && cat filename >> outfile

# 3  
Old 03-15-2012
Thanks , but grep does not work the way i looking for. May be i did not make it clear, but here is my script with example.
Code:
File1
This is shell script forum.
We can ask unix questions
Forum is very good with amazing helpful people
Shell scripting is fun
I am newbie to scripting

Script: 

while read LINE
   do
     #need commands to identify each line with word  "forum" or "shell"  and print it to a                      #file
    
   done <  File1

Hope you can help me here...Thanks

Last edited by davidtd; 03-15-2012 at 05:33 PM.. Reason: Correction
# 4  
Old 03-15-2012
I think egrep can do what you want:
Code:
egrep -i "(^|[^a-zA-Z])(forum|shell)($|[^a-zA-Z])" filename

This is shell script forum.
Forum is very good with amazing helpful people
Shell scripting is fun

$

If it finds either word with either spaces, non-alphabetic, or beginning/end of line on either side, it takes that as a match. Redirect the output as you see fit.
# 5  
Old 03-15-2012
Thanks, but how can I process each line instead of the whole file. I want to process each line, because if I find a line in the file then i process the line and then redirect it to a different file. To make it more clear, below is the example:

Code:
while read LINE
   do
     #need commands to identify each line with word  "forum" or "shell"  and print it to a                   file
    # Add date &  time to the LINE and some message
done <  File1


So after processing the line would look like:

Message:  "This is shell script forum. " was reported at Thu Mar 15 17:38:50 EDT 2012
Message:  "Forum is very good with amazing helpful people" was reported at Thu Mar 15 17:41:01 EDT 2012
Message: "Shell scripting is fun" was reported at Thu Mar 15 17:41:36 EDT 2012

Thanks and appreciate your quick responses.
# 6  
Old 03-15-2012
The reason I've been trying to run grep once instead of thousands of times is because doing this for individual lines will be very slow.

Code:
if echo "$line" | egrep ... > /dev/null
then
        echo "word was found in line"
fi

I still think this is not a good way of doing it.

Since you've finally told me what you want and not just the way you want to do it, GNU awk may be able to do this quite efficiently:

Code:
awk '/(^|[^a-zA-Z])(word1|word2)($|[^a-zA-Z])/ { print "Message: " $0 " was reported at " strftime("%F %$") }' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. UNIX for Dummies Questions & Answers

Shell Script for displaying the line till the target word

" Script for display sentences with special character" Hi, Could any one share a command how to display a line until my target word. For ex: My file has the content as: select * from db_wrk where col1 < col2 insert into table_name values('1','2','tst','wrk','dev','prod') My target... (10 Replies)
Discussion started by: Kalaiselvi66
10 Replies

4. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

5. Shell Programming and Scripting

to insert some word somewhere in the line with shell (or perl)

hello mighty all there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end.. my own knowledge base can say it is... (12 Replies)
Discussion started by: tip78
12 Replies

6. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

7. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

8. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

9. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question