Occurrence of two strings in a line of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Occurrence of two strings in a line of a file
# 1  
Old 11-23-2009
Occurrence of two strings in a line of a file

Hi all,
is there a way to find the occurrence of two strings in a line of a file?

e.g. I have
Code:
    XXXX     yyyyy    zzzz 111
    XXXX     yyyyy    zzzz 222
    XXXX     yyyyy    zzzz 333
    XXXX     yyyyy    zzzz 444

and want to find in one shot
Code:
    XXXX     yyyyy    zzzz 222

Thank you,
Sarah

Last edited by Yogesh Sawant; 02-26-2010 at 01:25 PM.. Reason: added code tags
# 2  
Old 11-23-2009
Try:
Code:
awk '/XXXX/ && /222/' < filename

# 3  
Old 11-23-2009
Quote:
Originally Posted by dennis.jacob
Try:
Code:
awk '/XXXX/ && /222/' < filename

can this be made case insensitive (on both strings)? maybe with grep?
# 4  
Old 11-23-2009
Quote:
Originally Posted by f_o_555
can this be made case insensitive (on both strings)? maybe with grep?

Try:

Code:
awk 'toupper($0) ~ /XXXX/ && /222/'  < file

# 5  
Old 11-23-2009
Quote:
Originally Posted by dennis.jacob
Try:

Code:
awk 'toupper($0) ~ /XXXX/ && /222/'  < file

...mmhhh not working... it seems I get no match

could I have, instead of XXXX and 222, two variable in array?
I tried something like

OCCURRENCE=`awk '/blah/ && /${STRING_NAME[1]}/' tmp.tmp1| wc -l`

but it does not work (it does if explicitly use the STRING_NAME value)
Thank you,

Last edited by f_o_555; 11-23-2009 at 11:15 AM..
# 6  
Old 11-23-2009
Code:
grep -Ei ^X{4}.*2{3}$ your_file

# 7  
Old 11-24-2009
Quote:
Originally Posted by daPeach
Code:
grep -Ei ^X{4}.*2{3}$ your_file

How can I put in two arbitrary strings $STRING1 and $STRING2?

grep -Ei $STRING1.$STRING2 doesn't work
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

4. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

5. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

Search for a multi-line strings in a file

Hello I need to search for a mult-line strngs(with spaces in between and qoted) in a file1 and replace that text with Fixed string globally in file1. The strng to search for is in file2. The file is big with some 20K records. so speed and effciency is required file1: (where srch & rplc will... (7 Replies)
Discussion started by: Hiano
7 Replies

8. Shell Programming and Scripting

Count no of occurrence of the strings based on column value

Can anyone help me to count number of occurrence of the strings based on column value. Say i have 300 files with 1000 record length from which i need to count the number of occurrence string which is existing from 213 to 219. Some may be unique and some may be repeated. (8 Replies)
Discussion started by: zooby
8 Replies

9. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

10. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies
Login or Register to Ask a Question