LinuxQuestions.org
Review your favorite Linux distribution.
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-18-2014, 05:21 AM   #1
suDoh!
LQ Newbie
 
Registered: Apr 2014
Location: Bangalore
Distribution: Precise Pangolin
Posts: 21

Rep: Reputation: Disabled
Question What's wrong with this awk command?


Hi LQ,

I am using the following awk commands to get particular conf files listed from /etc/ but its not giving me any output, so just wondering if anyone knows what I am doing wrong here:

ls -l /etc/ | awk '/\<(a|n).*\.conf$/ {print $9}'

or,

ls -l /etc/ | awk '/\<(u|r).*\.conf$/ {print $9}'

Cheers.
 
Old 04-18-2014, 05:37 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
this worked for me.
Code:
> ls -l /etc/ | awk '/\<(a|n).*\.conf$/ {print $9}'
amavisd.conf
ant.conf
autofs_ldap_auth.conf
cups-autoconfig.conf
nscd.conf
nsswitch.conf
ntp.conf
probably the version of awk (GNU Awk 3.1.8)?
 
1 members found this post helpful.
Old 04-18-2014, 07:00 AM   #3
suDoh!
LQ Newbie
 
Registered: Apr 2014
Location: Bangalore
Distribution: Precise Pangolin
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thumbs up

Thanks for pointing that one out... I believe the system was using mawk:

$ update-alternatives --list awk
/usr/bin/mawk

$ update-alternatives --display awk
awk - auto mode
link currently points to /usr/bin/mawk
/usr/bin/mawk - priority 5
slave awk.1.gz: /usr/share/man/man1/mawk.1.gz
slave nawk: /usr/bin/mawk
slave nawk.1.gz: /usr/share/man/man1/mawk.1.gz
Current 'best' version is '/usr/bin/mawk'.

using gawk I got the required output (finally):

$ ls -l /etc/ | gawk '/\<(a|n).*\.conf$/ {print $9}'
adduser.conf
apg.conf
netscsid.conf
nsswitch.conf

Cheers!
 
Old 04-18-2014, 07:40 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
I would add, do not parse ls for reasons seen here

For example, on my system your code will return nothing as all file and directory names are in the 8th column and I have no 9th.

I am curious though, is there anything wrong with simply using ls and the shell to return what you want:
Code:
$ cd /etc
$ ls [an]*.conf
Not sure if I missed a requirement here?

Last edited by grail; 04-18-2014 at 07:43 AM.
 
Old 04-18-2014, 07:46 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
Quote:
Originally Posted by grail View Post
I would add, do not parse ls for reasons seen here

For example, on my system your code will return nothing as all file and directory names are in the 8th column and I have no 9th.

I am curious though, is there anything wrong with simply using ls and the shell to return what you want:
Code:
$ cd /etc
$ ls [an]*.conf
Not sure if I missed a requirement here?
see for example cups-autoconfig.conf, will give different result. but I do not know what would be the preferred one...
 
Old 04-18-2014, 07:47 AM   #6
suDoh!
LQ Newbie
 
Registered: Apr 2014
Location: Bangalore
Distribution: Precise Pangolin
Posts: 21

Original Poster
Rep: Reputation: Disabled
Wink

Hey grail,

I agree that what you wrote is a pretty straight forward solution to my requirement, but I was digging through awk and came by that scenario I posted... which is why I was trying the same and got stuck until pan64 came for rescue

Cheers.
 
  


Reply

Tags
awk


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
turning awk command line entries into awk scripts tabbyagirl Linux - Newbie 5 08-22-2013 01:46 AM
[SOLVED] AWK: what's wrong with getline? cristalp Programming 2 11-18-2011 10:49 AM
something wrong when extracting TR file by awk aicaicai Linux - Newbie 16 09-20-2011 11:30 PM
[SOLVED] awk command help fredora Programming 4 11-25-2010 06:23 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM


All times are GMT -5. The time now is 06:31 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration