Search text file, then grep next instance of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search text file, then grep next instance of string
# 1  
Old 09-17-2011
Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this:

[1] Line1
Line2
Line3
Line4
Line5
Line6
Line7

[2] Line1
Line2
...

In my case, there are up to 1000 numbered beginner lines. I will be keying off of data from line 1, and I know the information I want is in Line5, every time. So, how do I search for the relevant beginning "Line1", then extract the next instance of "Line5" to use?

Thanks!
# 2  
Old 09-17-2011
Do you mean a literal 'Line 5' or a five line offset from the start of a datablock?

If yes,
Code:
grep '^Line5' inputfile

else
Code:
awk ' /^Line1/ { ok=FNR+5}
        /FNR==ok/ {print $0}' inputfile

In the latter case you need to change "Line1" to the actual datum you use to find the start.
# 3  
Old 09-17-2011
Something like this might work for you:

Code:
#!/usr/bin/env ksh

awk -v pat="[$1]" '
    $1 == pat {
        snarf = 1;
        next;
    }

    snarf {
        if( ++snarf == 5 )
        {
            print;
            exit(0);
        }
    }
' $2
exit

The script assumes you pass 1, 2, 3, etc. on the command line which then finds the section "[1]" etc in the input file. The second, optional, command line parameter is the name of the input file. If not supplied, the script will read from stdin. It finds the section and then prints the fifth line that follows.

If you key string isn't of the form [n], then just change the -v pat="" string to suit your needs.

Assuming you put this script into foo.ksh, the following would invoke it to find the 986th section from "input.file"

Code:
foo.ksh 986 input.file

# 4  
Old 09-17-2011
Try:
Code:
awk '/^\[/{for (i=1;i<5;i++)getline;print}' file

# 5  
Old 09-17-2011
Code:
sed -n '/^\[1\]/{n;n;n;n;p}' inputFile

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Programming

Search a string in a text file in C

Hello guys, i want some help please in coding that program , " A mini dictionary" the file looks like : Waver --- To be hesitated retirement --- life after end of career The user enter the word , and then it prints the meaning of it Please use CODE tags when... (4 Replies)
Discussion started by: Alyy
4 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

grep for a string until instance of a space

Hey guys, I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this. I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
Discussion started by: terrell
6 Replies

7. Shell Programming and Scripting

grep second instance of same string

Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times. <title>Tonight</title> <title>Thursday</title> <title>Friday</title>... (6 Replies)
Discussion started by: scubasteve39
6 Replies

8. Shell Programming and Scripting

Deleting files that don't contain particular text strings / more than one instance of a string

Hi all, I have a directory containing many subdirectories each named like KOG#### where # represents any digit 0-9. There are several files in each KOG#### folder but the one I care about is named like KOG####_final.fasta. I am trying to write a script to copy all of the KOG####_final.fasta... (3 Replies)
Discussion started by: kmkocot
3 Replies

9. Shell Programming and Scripting

Search the last instance of a string in a file

I have a big database log file which keepsgrowing daily. OS is HP UX. Here is a small part of it: Tue Jan 27 04:03:22 2009 : add session begin for mfgeb on /dev/pts/th. : Converting relative path to absolute path. : add session end. Tue Jan 27 04:03:29... (6 Replies)
Discussion started by: dinesh1178
6 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question