Solved

Need for work, only a noob. need to search many files for a string "USER HELP", then drop down 1 line and search only next line..

Posted on 2003-10-23
8
286 Views
Last Modified: 2010-04-21
Hi, I'm new to Linux and starting to learn it at work. I need to search many files for a string "USER HELP" if that exists, i need to check the next line down to see if it has "USERHELP=1" under neith it then if it does, pip to a file, have tried and am lost now..

all I have got to is:   find . -name "*4gl" | xargs grep "USER HELP" > results

but i think i need a loop..
0
Comment
Question by:malcolm_fitz
8 Comments
 
LVL 1

Expert Comment

by:AgelmarJagad
ID: 9611584
Have you tried that command without using xargs?  As in 'find . -name "*4gl" | grep "USER HELP" > results.  What is the exact problem that you are having?  Are you not getting output into the file?  Are you not getting the correct output?
0
 

Author Comment

by:malcolm_fitz
ID: 9611715
Ive got this so far:

find /home/malcolm/ -name "*4gl" | xargs grep -n -i --after-context=2 "USER HELP"  > /home/malcolm/workdir/aa

but I now need to check to see if 1 line under is not "USERHELP=1" then pip that line number and file to a file..  :)
0
 
LVL 3

Expert Comment

by:_tack
ID: 9612343
If a file contains the first line, can you assume it also contains the second line too ?
I mean, if a file contains "USER HELP" can you assume that if the file contains the line "USERHELP" it will be
the following line ?

if yes, then use:

find . -name "*.4gl" -exec grep -e "USER HELP" -e "USERHELP=1" \; -print

Of course you will have to redirect some output to /dev/null if you just want filenames.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 1

Accepted Solution

by:
AgelmarJagad earned 125 total points
ID: 9612345
--after-context=1 should be sufficient to catch the next line after the one that matches the grep, in fact it would make it much easier in the long run to do --after-context=1 (incidentally, you can also use the option -A 1 which means the same thing as --after-context=1).  Here is why: if you use --after-context=1 then you will output the line that has USERHELP on it, and the line immediatly following, so you just have to check every other line in the output.

So this should do it:
find /home/malcolm/ -name "*4gl" | xargs grep -n -i -A 1 "USER[ ]*HELP |  \
perl -pe 's/--\n//' | grep .*-[0123456789]*-.* > /home/malcolm/workdir/aa

When I was looking at output for a your original command (except wiht after-context=1 and the modification to USER HELP) I noticed that the output looked something like:
filename:line#:text that matched
fliename-line#-test the didn't match but was the next line
If the next line read USERHELP then it would match too (since I changed your original grep to work both with and without a space betweent he words) and the the line# would be delimited by : instead of -, so basically all of the lines you were looking for should have their line numbers delimeted by dashes, and that is what this command finds.  It uses regular expresssions, and they would be a good thing for you to learn if you don't know them already, but I'm not going to try to explain them here.

0
 
LVL 1

Expert Comment

by:AgelmarJagad
ID: 9612348
Oh, and by the way, the \ at the end of the first line of the command just signifies that the command continues on the next line, you can ignore it and just keep typing.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10243757
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by AgelmarJagad
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

khkremer
EE Cleanup Volunteer
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question