LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2017, 07:54 AM   #1
seth1788
LQ Newbie
 
Registered: Mar 2017
Posts: 1

Rep: Reputation: Disabled
grep help


Hello,

Looking for assistance on a small script. I have a text file with Names numbers and special notes. Not everyone has a special note. I am looking to search the file and output the matching results. Search and output all Johns (special notes if any). Search for all managers(and their name and number). The special note is always led with " ---->".

Format of file:
Code:
John Doe 111-111-1111
Mary Jane 222-222-2222
 ----> Manager
John Smith 333-333-3333
 ----> Director

What I have so far:

Code:
if grep -i -q -A1 $1 /home/username/file.txt; then
   grep -i -A1 $1 /home/username/file.txt;
else
    grep -i $1 /home/username/file.txt;
fi

It seems like what I have is displaying some results, but is far from complete. Any pointers would be greatly appreaciated.

Thanks!
 
Old 03-28-2017, 08:20 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,507
Blog Entries: 3

Rep: Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814Reputation: 3814
Welcome.

Unless there is a constraint requiring grep, I'd try awk. Though I'd hope there is a way with sed too.

Code:
awk -v n="John" '$1 ~ n { print; a++; } a && $1 ~ "---->" { print; a=0; next; } a=0;' /home/username/file.txt
The basic awk statements are implied if-then statements unless stated explicitly otherwise. The -v option passes an environment variable into the script as an awk variable.

Code:
man awk

Last edited by Turbocapitalist; 03-28-2017 at 08:25 AM.
 
Old 03-28-2017, 12:51 PM   #3
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 383Reputation: 383Reputation: 383Reputation: 383
MIGHT -z with some tricky \n regex work? See #15 in http://gnu.org/software/grep/manual/grep.html
But basically as above post says, grep wasn't designed for the complexity of --after having conditionality.
I thought of possibly some obscure tricks with tr, joining all the ----> with prior line, so basic grep could operate on ONE line as designed, then maybe |tr to re-split that, re-inserting the newline, but obscure tricks probably are NOT the goal here.

A useful learning exercise for OriginalPoster might be to explain what/why/how their attempt produced not the desired result. Awaiting reply from OP. Welcome to LQ!
 
Old 03-28-2017, 04:55 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,238

Rep: Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151Reputation: 4151
A small correction if I might be so bold
Quote:
Originally Posted by Turbocapitalist View Post
Code:
awk -v n="John" '$1 ~ n { print; a++; next} a && $1 ~ "---->" { print; a=0; next; } a=0;' /home/username/file.txt
Doesn't handle the second part of the request, but does indicate what might be done. Bit difficult if no prior awk knowledge.

I think this is beyond the scope of grep - holding the data to check the next line needs some logic ability. Could be done in sed, but would be ugly.
 
1 members found this post helpful.
Old 03-29-2017, 12:02 AM   #5
DDukes
LQ Newbie
 
Registered: Mar 2017
Posts: 12

Rep: Reputation: Disabled
@ OP

Your code of grep -i -A1 $1 /home/username/file.txt can work if the members with no special notes was replaced with a line space.

I did a test with your file entries. I enter your entries like so in a file:

Quote:
John Doe 111-111-1111

Mary Jane 222-222-2222
----> Manager
John Smith 333-333-3333
----> Director
Then ran:
Code:

grep -i -A1 "john" members 
John Doe 111-111-1111


John Smith 333-333-3333
 ----> Director
As you can see, it prints all johns with special notes if applicable .

Another test with Mary

Code:
grep -i -A1 "mary" members 
Mary Jane 222-222-2222
 ----> Manager
The key is to keep things in a uniform pattern. Use a line space for members with no special notes and the grep -i -A1 should do ok.

BTW, awk and grep/egrep can have more flexibility of output results if you create a database of members using columns a special character as a field separator.
Quote:
John Doe|111-111-1111
Mary Jane|222-222-2222|Manager
John Smith|333-333-3333|Director
The pipe symbol is use here as the field separator.
- cheers

Last edited by DDukes; 03-29-2017 at 12:06 AM.
 
1 members found this post helpful.
Old 03-30-2017, 02:24 PM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
If the file is standardized and predictable you could bump through each line and retain information in a bash script. Although awk is probably a more efficient search mechanism, especially on large data sets. If you're just wanting neighboring lines on the quick, your could grep -v and remove john, then diff that with the original file.

$ cat data_original.txt | grep -i -v john > data_new.txt
$ diff -c3 data_new.txt data_original.txt | less
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find & grep - how to return pathes, not grep phrases ? postcd Linux - General 2 11-25-2014 12:43 PM
grep to file outputs more than grep to screen? tcpman Linux - Server 4 06-07-2013 04:46 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration