?
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
Medium Priority
?
299 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 1

Accepted Solution

by:
AgelmarJagad earned 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

770 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