LinuxQuestions.org
Visit Jeremy's Blog.
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 05-15-2011, 12:46 AM   #1
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Rep: Reputation: 15
Make awk detect lines starting with a dot


Hello,

The title is the question.
I tried, of course:
Code:
awk '/^\./ {print}' FILE.TXT
That does not work, I just get an empty output, even if FILE.TXT contains lines which fit the desired regexp :[
I've searched but did not found my way.
I'd like to do it with awk, as I want my program to rely only on awk, nothing else such as sed, grep, etc.

Thanks!

Cordially,

BlueSpirit

Last edited by BlueSpirit; 05-15-2011 at 12:48 AM.
 
Old 05-15-2011, 01:01 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
It works here
Code:
c@CW8:/tmp$ awk '/^\./ {print}' FILE.TXT 
.foo
c@CW8:/tmp$ cat FILE.TXT 
.foo
bar
 
Old 05-15-2011, 01:01 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Your approach looks fine, and works here; have a look at your data,
maybe using 'od'...


Cheers,
Tink
 
Old 05-15-2011, 01:03 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Your regular expression should work. Are you sure there aren't leading spaces in the "dotted" lines? Please, can you post some lines of the input file?
 
Old 05-15-2011, 01:19 AM   #5
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
Omg. I made a mistake.
I dont want the lines beginning with a dot...

Still, I don't know how I can achieve what I want.
What I'd like is in fact to find lines which have $NF
(last field of line) starting with a dot.

Sorry for confusing you.

Cordially,

BlueSpirit

Last edited by BlueSpirit; 05-15-2011 at 01:25 AM.
 
Old 05-15-2011, 01:30 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
Use the same idea on $NF and you should be all good
 
Old 05-15-2011, 01:38 AM   #7
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
Damn, I still dont get how to...
Tried this:
Code:
awk 'match($NF, "^\.") {print $NF}' FILE.TXT
But now I get lines which do not necessarily have a $NF starting with a dot... :[ :{ :[
 
Old 05-15-2011, 02:23 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Code:
awk '{ if ( $NF ~ /^[.]/ ) { print $NF }}' file
I'm suspecting though that you're trying to parse the output of ls. If so, there are usually better ways to go about it.

Even if I'm wrong, if you tell us exactly what you're trying to accomplish, and give us an actual example of the input text, we may be able to give you some better advice.
 
Old 05-15-2011, 02:50 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
You can also just use David's test without the if:
Code:
awk '$NF ~ /^\./{print $NF}' FILE
 
1 members found this post helpful.
Old 05-15-2011, 03:32 AM   #10
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
Thanks guys, it worked pretty well.

David, I am parsing a file which contains lines issued by ls, but I cannot modify the way these lines are done since It's from a ftp listing...

I still have a question for you guys. Yesterday I didn't know yet how to use awk, now I'm not bad at all. I still have a question, only for curiosity. My current awk cmdline ensures I find a line which has $NF as a path conforming to the following model:
Code:
./filepath/filename.extension
The way I do this is:
Code:
awk '$NF~/^\..*\.extension$/{print $NF}' FILE.TXT
My question is: WOuld it be possible to add to this command line, i.e. in a single awk call, a call to the awk function sub? I'd like to do this:
Code:
awk '$NF~/^\..*\.extension$/{print $NF}' FILE.TXT|awk 'sub("\.extension$", "hello")'
Would it be possible to do it in one awk call?

Cordially,

BlueSpirit

Last edited by BlueSpirit; 05-15-2011 at 03:38 AM.
 
Old 05-15-2011, 03:46 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
Like:
Code:
awk '$NF~/^\..*\.extension$/{print gensub(/extension$/,"hello","1",$NF)}' FILE.TXT
 
Old 05-15-2011, 03:57 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Grail to the rescue again. I don't know why, but I seem to always have trouble with awk's bracket syntax, even in supposedly simple one-liners like this. So I usually end up being very conservative and spell out the if tests fully. Maybe one of these days I'll get it all figured out.

Speaking of which, if you haven't seen them yet, here are some good awk links.
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt

Just a small caveat. The first link is a commonly-recommended tutorial, but it's based on old unix versions of awk. Be aware that the gnu awk used in Linux has more features and a few other differences.
 
Old 05-15-2011, 10:50 AM   #13
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Note that gensub is a GNU awk extension. This variant of grail's solution will work with any awk:
Code:
awk '{ v=$NF; if (v ~ /^\./ && sub(/\.extension$/, ".hello", v)) print v }' FILE.TXT
 
  


Reply

Tags
awk, regular expression


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
du -hs .* does not show size of directories starting with a dot (anymore) Hannes Linux - Desktop 2 03-22-2011 12:34 PM
why does AWK split my lines into two? keenboy Linux - General 1 08-05-2010 11:34 AM
awk: get lines starting from String1 and finish String2 frenchn00b Programming 9 10-29-2007 08:16 AM
Delete lines using awk kkjegan Programming 13 09-11-2007 07:36 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 05:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration