LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2004, 02:03 AM   #1
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Rep: Reputation: 0
Can I use grep inside awk?


I have the following file:
www/home/pictures/bird.gif
www/home/pictures/home.gif
www/home/documents/home.gif

I want to filter out the records that have a file name containing "home". I don't want the records with a directory named "home" to be included. To only pick out only the filename, I do this:
fn=`echo $tmp | grep '[^\ /]*$' -o`

where $tmp is the first record in the file.

So, can I do this operation using awk? I have tried something like shown below, but I only get syntax errors (invalid char '`' in expression). Am I not allowed to use grep inside an awk?
awk '{ fn=`echo $1 | grep '[^\/]*$' -o` if ( $fn ~/'home'/ ) print $0 }' $myFile > newfile

I hope someone can help me out!
 
Old 04-25-2004, 02:36 AM   #2
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
It's really unclear what you're trying to do. You say you want to "filter out" filenames w/'home' in them, but that you don't want "records with a directory named 'home' to be included".

1) by filter out, do you mean to ignore files with 'home' in them or that you want to "work with" files with 'home' in them?
2) all of the directories contain directories named 'home'
3) your regex "[^\ /]*$" matches any input line (see end of this post for why). Also, you needn't escape '/' in a character class - [^/] works fine.
4) what about something with awk -F'/' '{blah blah}' to get at each compenent of the full pathname (whatever it is you're doing )?

the regex "[^/]*$" matches any line which contains zero or more occurences of any character but '/', followed by an end of line marker. Note that even "//////" matches, because [^/]* is in a sense "optional". Think about that one. Also, the -o option puts grep into an endless loop with that regex (at least for me it does). I've never used -o so I'm not familiar with what can go wrong with it.

Last edited by rkef; 04-25-2004 at 02:43 AM.
 
Old 04-25-2004, 02:50 AM   #3
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Ok, I'll try again.

1) I want to work with the records that actually contain a file with "home" in. I don't want to work with www/home/pictures/bird.gif, and to avoid this I use the regular expression. If I don't use a regular expression, this record will be selected because it has "home" in it.

2)Yes, all the records contain "home", and that's the problem. As mentioned above.

3) As far as I can see, my reg.expr is correct because it only returns the filename. I want to use this filename for further testing in the awk:
awk '{ fn=`echo $1 | grep '[^\/]*$' -o` if ( $fn ~/'home'/) print $0 }' $myFile > newfile

Any clearer?
 
Old 04-25-2004, 06:21 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You are trying to start the "grep" program from inside a awk script. I'm not very sure, but I think that's not (easily) possible. But it's also doesn't make a lot of sense, since awk is more powerfull than grep. It can do the same, and a lot more, but with different syntax and semantics.

For example if this is "files.txt":
Code:
www/home/pictures/bird.gif
www/pictures/animal.gif
www/home/documents/home.gif
www/nothome/somebitmap.gif
www/somedir/pictures/next.gif
www/home/documents/home.gif
...then, is this what you are looking for?
Code:
awk -F/ '!/\/home\//{print $NF}' files.txt

Last edited by Hko; 04-25-2004 at 06:23 AM.
 
Old 04-26-2004, 07:54 AM   #5
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you for help! I realise that I should have told you the whole scenario instead of simplify it too much. Sorry

This is my actual file:
anna;www/home/pictures/bird.gif;23
arna;www/pictures/animal.gif;4
emma;www/home/documents/home.gif;333
kim;www/nothome/somebitmap.gif;123
sarah;www/somedir/pictures/next.gif;43
alf;www/home/documents/home.gif;1

It is semicolon separated, so I can't use field separator you suggested (-F/).
Is it possible to put your awk-command inside mine? Like this:
awk -F';' '{ fn=`your awk` if ( $fn ~/'home'/ ) print $0 }' $myFile > newfile

Again; I want to print out the records that has a filename with "home" in it.
 
Old 04-26-2004, 01:08 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Now (I think) I understand the "home"-part of the problem, but it's still unclear to me what part of an input line, you want to see in the output.

So here are 3 options:
Code:
# Entire line in ouput. I this case you can also use grep.
awk '/\/[^/]*home[^/]*;/{print $0}' files.txt
grep '\/[^/]*home[^/]*;' files.txt

# Only file in output, including the path:
awk -F\; '/\/[^/]*home[^/]*;/{print $2}' files.txt

# Only file in output, with path stripped:
awk -F\; '/\/[^/]*home[^/]*;/{sub(".*/","",$2) ; print $2 }' files.txt
Hope I could help this time.

Last edited by Hko; 04-26-2004 at 01:16 PM.
 
Old 04-27-2004, 12:58 AM   #7
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you, Hko.
The first option seems to be the right one in my case. I didn't get the idea about not using the field-separator!
 
Old 09-28-2015, 01:38 AM   #8
jagsb
LQ Newbie
 
Registered: Sep 2015
Posts: 1

Rep: Reputation: Disabled
cat files.txt
www/home/pictures/bird.gif
www/home/pictures/home.gif
www/home/documents/home.gif


awk -F / '{tmp=match($NF,/home/);if(tmp) print}' files.txt

This gives you the below
www/home/documents/home.gif
www/home/documents/home.gif
 
Old 09-28-2015, 02:59 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This gives the same o/p as Hko's num 1
Code:
for rec in $(cat t.t)
do
    dn=$(basename $rec)
    if [[ $dn =~ "home" ]]
    then 
        echo $rec
    fi
done
ie the entire rec only for recs whose filename (not checking dirname) includes 'home'
 
Old 09-29-2015, 05:29 AM   #10
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
In case you missed it, I just want to point out that you are responding to a thread from 2004(!).

Best regards,
HMW
 
Old 09-29-2015, 08:48 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Red face

yeah - I missed 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
awk inside a makefile linux.fob Programming 2 10-12-2005 04:57 PM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 02:57 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
Newbie playing with Awk and Grep Helene Programming 6 04-15-2004 02:08 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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