Finding a word in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a word in a file
# 1  
Old 02-05-2008
Finding a word in a file

Hi frndz,

i have a flat file like,

xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc..

look, in this file i want to fetch the substring from one yyy to another one and need to print it then from next values between yyy's..

can you please give me some inputs on this..

Thanks in advance,
- Raja.
# 2  
Old 02-05-2008
Code:
 echo "xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc" | sed 's/.*yyy\(.*\)yyy.*/\1/'

# 3  
Old 02-05-2008
Quote:
Originally Posted by Tytalus
Code:
 echo "xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc" | sed 's/.*yyy\(.*\)yyy.*/\1/'

may not work if there are more "yyy"s .
# 4  
Old 02-05-2008
Quote:
Originally Posted by smr_rashmy
Hi frndz,

i have a flat file like,

xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc..

look, in this file i want to fetch the substring from one yyy to another one and need to print it then from next values between yyy's..

can you please give me some inputs on this..

Thanks in advance,
- Raja.
Code:
# awk 'BEGIN{FS="yyy"}{for(i=2;i<NF;i++) print $i}' file
 zzz sss aaa bbb
 xxx rrr sss ttt

# 5  
Old 02-05-2008
check if this is what you are looking for:
Code:
perl -nl -e 'while (m/yyy(.*?)yyy/g) { print $1; }' file

# 6  
Old 02-05-2008
Quote:
Originally Posted by ghostdog74
Code:
# awk 'BEGIN{FS="yyy"}{for(i=2;i<NF;i++) print $i}' file
 zzz sss aaa bbb
 xxx rrr sss ttt

yes this is working... however, the thing is.. i want to do some operation on each print $1... that is, need to work with,
zzz sss aaa bbb
then,
xxx rrr sss ttt
ect.,

am i clear frnd?
# 7  
Old 02-05-2008
Quote:
Originally Posted by smr_rashmy
yes this is working... however, the thing is.. i want to do some operation on each print $1... that is, need to work with,
zzz sss aaa bbb
then,
xxx rrr sss ttt
ect.,

am i clear frnd?
it depends on what you want to do. You can do it inside awk, or outside in the shell
Code:
awk 'BEGIN{FS="yyy"}
{    for(i=2;i<NF;i++) {
           print $i
          #do something here
      }
}' file

until that information of what you want to do is spelt out, otherwise not much i can do
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding a word through substring in a file

I have a text file that has some data like: PADHOGOA1 IOP055_VINREG5_1 ( .IO(VINREG5_1), .MONI(), .MON_D(px_IOP055_VINREG5_1_MON_D), .R0T(px_IOP054_VINREG5_0_R0T), .IO1() ); PADV30MA0 IOP056_VOUT3_IN ( .IO(VOUT3_IN), .V30M(px_IOP056_VOUT3_IN_V30M)); PADV30MA0 IOP057_VOUT3_OUT (... (2 Replies)
Discussion started by: utkarshkhanna44
2 Replies

2. Shell Programming and Scripting

Finding a word with awk or sed

Hello, in a AIX system : AIX CDRATE01 2 7 00FAB3114C00 my following commande give the result : LISTE /tmp/RESS **************************************************************** Liste TYPE = XXXXXXX EX = YYYY VER ... (13 Replies)
Discussion started by: sam01
13 Replies

3. Shell Programming and Scripting

Finding pattern in a text file and returning a part of the word

Dear All, assume that we have a text file or a folder of files, I want to find this pattern followers*.csv in the text file , and get * as the output. There are different matches and * means every character. Thank you in advance. Best, David (1 Reply)
Discussion started by: davidfreed
1 Replies

4. Shell Programming and Scripting

Finding total count of a word.

i want to find the no:of occurrences of a word in a file cat 1.txt unix script unix script unix script unix script unix script unix script unix script unix script unix unix script unix script unix script now i want to find , how many times 'unix' was occurred please help me thanks... (6 Replies)
Discussion started by: mahesh1987
6 Replies

5. Shell Programming and Scripting

Finding a specific word

Hi, I am trying to develop a script which should find a word if a particular word exists. Below is the content of the file. insert_job: test_job ----> job name days_of_week: all start_times: "16:00" date_conditions: 1 insert_job: test_job2 ----> job name days_of_week: all... (16 Replies)
Discussion started by: rpatty
16 Replies

6. Emergency UNIX and Linux Support

Getting last 50 lines after finding a word

Hi All, I have a requirement as below I need find for a string in a log file and once i found that string i need to send a mail.Thsi can be done thru grep and mailx cmnd. Here once i found the string i need to send last 50 lines(up) from where string found. Actually they require to find... (9 Replies)
Discussion started by: pssandeep
9 Replies

7. Shell Programming and Scripting

Finding a word at specific location in a string

Hi All , I have different strings (SQL queries infact) of different lengths such as: 1. "SELECT XYZ FROM ABC WHERE ABC.DEF='123' " 2. "DELETE FROM ABC WHERE ABC.DEF='567'" 3. "SELECT * FROM ABC" I need to find out the word coming after the... (1 Reply)
Discussion started by: swapnil.nawale
1 Replies

8. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

9. Shell Programming and Scripting

need help with finding a word in file's contents

Hi, I need to check if a particular name is already in the file or not and i am using following code for this... match=$(grep -n -e "$output1" outputfiles.txt ) where output1 is the variable name having names in it and outputfiles.txt is the file name ..and i am using ksh can anybosy... (6 Replies)
Discussion started by: manmeet
6 Replies

10. Shell Programming and Scripting

Finding word in file then print the preceding....

Hi, I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line. Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line. Example - I can grep for ouch... (5 Replies)
Discussion started by: g_jumpin
5 Replies
Login or Register to Ask a Question