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
285 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
--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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now