How to find lines in a .txt contains the strings I want


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find lines in a .txt contains the strings I want
# 1  
Old 11-28-2011
How to find lines in a .txt contains the strings I want

I have a .txt contains a lot of lines.
Now I want to write a shell script to find out all the lines which contain the strings I want, and print these lines.


For example:
A.txt
Code:
when you post any code
you can easily do this
highlighting your code and then click
you should do a Google Site Search or internal search before posting

Now I want to find all lines contains string 'code' and 'when', how to write this script?

Last edited by Scott; 11-28-2011 at 06:29 PM.. Reason: Code tags :)
# 2  
Old 11-28-2011
Code:
grep -E "code|when" file

Talking of which, when posting code, input, output or terminal-screen information, please use code tags. Thanks Smilie
# 3  
Old 11-28-2011
I want lines contain code and when, not code or when...
# 4  
Old 11-28-2011
So you do. Excuse me!

Code:
awk '/code/&&/when/' file

# 5  
Old 11-28-2011
Ok, thank you very much
But one more question, if there is a txt B
B.txt
code
when

Now I just know these two txts, I have to find the lines in A.txt which contains the strings in B.txt. How can I do that?
# 6  
Old 11-28-2011
grep -f B.txt A.txt?
# 7  
Old 11-28-2011
Yes, thank you
This will print the line I want.
But if B.txt is like this:

B.txt
code
when
google

I want to search in A.txt that which words are contain in A and in B, then print them.
How to do that?
I am very appreciate for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Solaris

How to find multiple strings on different lines in file?

Hello, I have spent considerable amount of time breaking my head on this and reached out here. here is the back ground. OS - Solaris 10 There are two strings '<Orin>sop' and '<Dup>two' which I wanted to look for in a file without the quotes on different lines and ONLY if both strings are... (5 Replies)
Discussion started by: keithTait309875
5 Replies

3. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

4. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

5. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

6. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

7. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

8. Shell Programming and Scripting

Find lines containing two strings

How can i find lines which contains two strings (or two charcters) let say "ABC" and "DEF". Line: SEFGWN;BVABCFSDFBDEF (3 Replies)
Discussion started by: ksailesh
3 Replies

9. Shell Programming and Scripting

How to find the lines which do not have certain strings

Hi, guys. I have one question: How can I search the lines in a file which do not have certain string in it. For example, the file is called shadow, the contents of it is below: **************************** ... brownj:SFSM$DFAAA2313:0:0:50:7 hynesp:MNBADF$23$adfd:0:0:50:7... (2 Replies)
Discussion started by: daikeyang
2 Replies

10. Shell Programming and Scripting

Find lines with space between strings

Hello all, I am having trouble with setting up a regular expression used with egrep. My script reads an input file a line at a time. I would like the egrep command to search for the following pattern: server name at the beginning of the line, then one or more spaces, and then a pound sign. ... (5 Replies)
Discussion started by: Galt
5 Replies
Login or Register to Ask a Question