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
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

724 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