check line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check line in a file
# 1  
Old 11-25-2006
check line in a file

Hi all,

How do we check in a file whether a line started with KEYWORD2 is right after the line started with KEYWORD1

for example, this file content:

Abcdef gsh iasdi
94945 9085095 lksdjlkj
KEYWORD1 skljfi slakjfoi ' opiport sdfl
KEYWORD2 ksjflsk jfasope
jkdfsk393 89374982 23

Please help me!
Thanks
# 2  
Old 11-25-2006
something to start with

Code:
awk '/KEYWORD1/ {getline; if($1 == "KEYWORD2") print "Success at line no ", NR}' filename

# 3  
Old 11-25-2006
It so great!
Thank you, ranj@chn!
# 4  
Old 11-25-2006
awk '/KEYWORD2/ { if ( prev !~ /KEYWORD1/ ) {print "Error at " NR}} {prev = $0 }' <sample.txt

John Arackal
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. UNIX for Dummies Questions & Answers

Check for empty line at end of a dynamic header in each file

Hi Folks, I have a requirement to develop a shell script. PFB my requirement, Requirement: I need to check an empty line after the end of each header in respective file and if a empty line is present simply echo file OK and if empty line is not present echo "Adding empty line" and add an... (6 Replies)
Discussion started by: tpk
6 Replies

3. UNIX for Dummies Questions & Answers

Help! Need "File exist check" and line count

Hello, I've been stuck with this for a few days now :wall:, so I'm hoping someone out there might be kind enough to help me. I need to do a "file exist" check with a variable in the file name. After that I need to do a line count on the file, and output its results to a txt file. For... (3 Replies)
Discussion started by: ronan1219
3 Replies

4. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

5. Shell Programming and Scripting

How to check for a character at last line of the file?

this is the csv file. i want to check the last line contains the character N. Record Type#Batch Job ID#Batch Number#FileCreation Date#FileCreation Time#Production/Test Fileindicator#File Character H#0002#0002#20100218#17.25#P#barani Record Type#A#B#C#D#E#F#G#H#J#K#L... (5 Replies)
Discussion started by: barani75
5 Replies

6. Shell Programming and Scripting

one line command to check file existence and FTP it

Hi all, I need a batch script to Check for existence of file say i check for files with extension xml.done in C:\Myfile.(This folder contains .xml file and its corresponding .done files) If the .done file exists then it should FTP the corresponding .xml file to UNIX. Is this possible to do... (6 Replies)
Discussion started by: Codesearcher
6 Replies

7. Shell Programming and Scripting

how to check if a new line has been added to a file?

Have come up with the following but it doesn't seem to work.. Is there some other command i could use to get this to work? OUTPATH=/home/out PARMFILE=$OUTPATH/jobcount_test.txt LOG=$OUTPATH/job_count_monthlymail_log.txt HLOG=$OUTPATH/job_count_monthlymail_hlog.txt # echo " started at... (2 Replies)
Discussion started by: Jazmania
2 Replies

8. Shell Programming and Scripting

check line by line in a file

Hi there How can I check line by line in a file? I need to compare the first value with the second to know if they are equal. If those values are equal, I require to send "TRUE" to the output or "FALSE" otherwise until the complete file has been read. Thank you (6 Replies)
Discussion started by: loperam
6 Replies

9. Shell Programming and Scripting

Read last line of file to check for value

Folks How best to read the last line of a file, to check for a particular value? This is the last line of my file...... 00000870000002000008 0000000020000 ......I need to check that this line contains '70' in positions 7 and 8, before I continue processing. Regards ... Dave (12 Replies)
Discussion started by: daveaasmith
12 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question