LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-29-2021, 05:50 AM   #16
ColDon
LQ Newbie
 
Registered: Apr 2021
Posts: 7

Original Poster
Rep: Reputation: Disabled

Hi Made In Germany,

Many thanks for your contribution. I was looking for a solution using Sed as I understood that Grep could not provide one! Tell that to Memorek! Both your solutions worked perfectly. The Sed one, however, wasn't half convoluted! Yet, whenever someone says something can't be done, someone else says hold on a minute I've just done it!

Your help is very much appreciated
 
Old 04-29-2021, 06:15 AM   #17
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by ColDon View Post
Hi Turbocapitalist,
Thank you very much. Your response does exactly what I was looking for. I have not had any previous involvement with Awk as I was given to understand that it was a difficult language to master. It looks very powerful.

Your help is much appreciated.
No problem. Be sure to consider the comments on that method in #4 and #5 above. I left the grep in there because it wasn't clear what kind of pattern you were searching for, say if it was a PCRE expression. In most other cases you can fold the pattern into AWK and skip grep completely. There are also a lot of built-in variables like FILENAME which can help.

As for mastering the language, that would take a long while but the basic orientation as to what it's good at and how to use it can be acquired in an hour or two of concentrated study.
 
Old 04-29-2021, 06:17 AM   #18
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Or, in case you used grep -R, try find
Code:
find /media/sda3/ -type f -name "*.txt" -exec grep -H 'PATTERN' {} \; -exec echo \;

Last edited by MadeInGermany; 04-29-2021 at 06:19 AM.
 
Old 04-29-2021, 07:00 AM   #19
ColDon
LQ Newbie
 
Registered: Apr 2021
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Shruggy, thank you for drawing my attention to that. On the same note, if I wanted Grep to cycle through all the files in the Z2 directory and also any other sub directories within the Z2 directory, is that doable? If so, how would I code it?
Thank you
 
Old 04-29-2021, 08:44 AM   #20
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
See MadeInGermany's post directly above yours.

Bash also has the globstar option.
Code:
shopt -s globstar
for i in /media/sda3/Z2/**.txt

Last edited by shruggy; 04-29-2021 at 08:50 AM.
 
Old 04-29-2021, 03:56 PM   #21
ColDon
LQ Newbie
 
Registered: Apr 2021
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Shruggy, thank you for pointing this out to me. On that point, how do I code it if I wish Grep to cycle through all the files in directory Z2 including any sub directories? Using /media/sda3/Z2/*.* doesn't seem to work. Sda3 is my data drive.
 
Old 04-29-2021, 05:02 PM   #22
ColDon
LQ Newbie
 
Registered: Apr 2021
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi MadeinGermany, thank you for your suggestion. To be honest I hadn't thought any wider than files in a particular directory. I had not considered that there might also be sub directories involved. I have run your Grep -R suggestion and it works perfectly. I appreciate your contribution very much.
 
Old 04-30-2021, 02:36 AM   #23
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by ColDon View Post
Using /media/sda3/Z2/*.* doesn't seem to work.
Please pay attention. It's not *.*, it's **
 
Old 04-30-2021, 02:49 AM   #24
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
globstar is a fairly new option in Bash, but it's in a lot of tutorials. Once one knows it exists, there are a lot of uses for it.
 
  


Reply



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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Grep another grep's output alancampbell6 Linux - Newbie 4 08-19-2011 09:49 AM
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 -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-30-2009 11:21 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

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

All times are GMT -5. The time now is 07:48 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