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
290 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
Industry Leaders: 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

733 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