-   Programming (
-   -   Make awk detect lines starting with a dot (

BlueSpirit 05-15-2011 12:46 AM

Make awk detect lines starting with a dot

The title is the question.
I tried, of course:

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.




catkin 05-15-2011 01:01 AM

It works here

c@CW8:/tmp$ awk '/^\./ {print}' FILE.TXT
c@CW8:/tmp$ cat FILE.TXT

Tinkster 05-15-2011 01:01 AM

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


colucix 05-15-2011 01:03 AM

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?

BlueSpirit 05-15-2011 01:19 AM

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.



grail 05-15-2011 01:30 AM

Use the same idea on $NF and you should be all good :)

BlueSpirit 05-15-2011 01:38 AM

Damn, I still dont get how to...
Tried this:

awk 'match($NF, "^\.") {print $NF}' FILE.TXT
But now I get lines which do not necessarily have a $NF starting with a dot... :[ :{ :[

David the H. 05-15-2011 02:23 AM


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.

grail 05-15-2011 02:50 AM

You can also just use David's test without the if:

awk '$NF ~ /^\./{print $NF}' FILE

BlueSpirit 05-15-2011 03:32 AM

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:

The way I do this is:

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:

awk '$NF~/^\..*\.extension$/{print $NF}' FILE.TXT|awk 'sub("\.extension$", "hello")'
Would it be possible to do it in one awk call?



grail 05-15-2011 03:46 AM


awk '$NF~/^\..*\.extension$/{print gensub(/extension$/,"hello","1",$NF)}' FILE.TXT

David the H. 05-15-2011 03:57 AM

Grail to the rescue again. :hattip: 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. :D

Speaking of which, if you haven't seen them yet, here are some good awk links.

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.

Nominal Animal 05-15-2011 10:50 AM

Note that gensub is a GNU awk extension. This variant of grail's solution will work with any awk:

awk '{ v=$NF; if (v ~ /^\./ && sub(/\.extension$/, ".hello", v)) print v }' FILE.TXT

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