Parse a file to display lines containing a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse a file to display lines containing a word
# 1  
Old 03-04-2010
Parse a file to display lines containing a word

Hi!

I'm trying to create a shell script to parse a file which might have multiple lines matching a pattern (i.e. containing some word).
I need to return all lines matching the pattern, but stripping the contents of that line until the pattern is matched

For example, if my input file was
Code:
aaaaaaa bbbbbbbb ccccccc
ddddddd eeeeeeee fffffffffff
ggggggg WORD hhhhhh
iiiiiiiiiiiiiiiii jjjjjjjjjjjjjjjj  kkkkkkkkk WORD mmmmmmm

I need to return
Code:
WORD hhhhhhhh
WORD mmmmmmm

What would be the best way of doing this, using only sed/awk/grep?

Thanks!
-orno
# 2  
Old 03-04-2010
One way:
Code:
sed -n 's/.*\(WORD.*\)/\1/p' file

# 3  
Old 03-04-2010
Code:
nawk 'match($0, "WORD") {print substr($0, RSTART)}' myFile
nawk '/WORD/ {print substr($0, index($0, "WORD"))}' myFile

# 4  
Old 03-04-2010
Bug Is this what you are looking for?

Code:
>sed "s/WORD /WORD_/" < orno.txt | sed 's/ /\n/g' | grep "^WORD" | sed "s/WORD_/WORD /"
WORD hhhhhh
WORD mmmmmmm

# 5  
Old 03-04-2010
Assuming the word needs to be parameterized and is stored in a shell variable:

Code:
$ w=WORD
$ awk -v w="$w" 'i=index($0,w){print substr($0,i)}' data

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

awk to parse file and display result based on text

I am trying using awk to open an input file and check a column 2/field $2 and if there is a warning then that is displayed (variantchecker): G not found at position 459, found A instead. The attached Sample1.txt is that file. If in that column/field there is a black space, then the text after... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

4. Shell Programming and Scripting

Check/Parse log file's lines using time difference/timestamp

I was looking at this script which outputs the two lines which differs less than one sec. #!/usr/bin/perl -w use strict; use warnings; use Time::Local; use constant SEC_MILIC => 1000; my $file='infile'; ## Open for reading argument file. open my $fh, "<", $file or die "Cannot... (1 Reply)
Discussion started by: cele_82
1 Replies

5. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

6. Shell Programming and Scripting

Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4... (6 Replies)
Discussion started by: darbs121
6 Replies

7. Shell Programming and Scripting

Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths. * /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB * /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB * /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB *... (4 Replies)
Discussion started by: senthilkc
4 Replies

8. Shell Programming and Scripting

Extra/parse lines from a file between unque lines through the file

I need help to parse a file where there are many records, all of which are consistently separated by lines containing “^=============” and "^ End of Report". Example: ============= 1 2 3 4 End of record ============= 1 3 4 End of record Etc.... I only need specific lines... (5 Replies)
Discussion started by: jouuu
5 Replies

9. Shell Programming and Scripting

Display file without # lines

Hi to all in this great forum, im sure this has been asked lots of times before but ive been looking for the past day and cant find the answer. I use cat/some/file to display its contents but how can i get it to not display hashed out lines, or do i need another command, Thanks in advance:) (5 Replies)
Discussion started by: dave123
5 Replies

10. UNIX for Dummies Questions & Answers

to display the word containing all of aeiou in a file.

i have a file containing words like Animal education asegdfiwqoqwewqeu etc... i need to display the words education asegdfiwqoqwewqeu alone... ie a word containing all the vowels in it.... (1 Reply)
Discussion started by: arunsubbhian
1 Replies
Login or Register to Ask a Question