LinuxQuestions.org
Review your favorite Linux distribution.
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 11-13-2013, 10:38 AM   #1
jeffwang66
LQ Newbie
 
Registered: Jan 2011
Posts: 27

Rep: Reputation: 0
Question about a awk command


Hi,

I am studying an awk command:

awk '{ sub(/\/\/.*/, "", $NF); print }' input.txt

The input.txt is:

char*s1="//hello"; //comment
//delete
/* hello //*/


The output is :

char*s1="//hello";

/* hello


My question is why the contents ("//hello") is not deleted? And what does $NF mean?

Thanks
 
Old 11-13-2013, 11:10 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
The NF variable is an awk internal and holds the number of fields for the current line that is processed.

This sub(/\/\/.*/, "", $NF) will do the substitution on the last field.

/* hello //*/ is 3 fields and the substitution is done on the last field only, hence the /* hello being outputted.

EDIT: Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Last edited by druuna; 11-13-2013 at 11:10 AM. Reason: Added url
 
Old 11-13-2013, 11:24 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
My question is why the contents ("//hello") is not deleted?
As per druuna's explanation to NF, it would imply there is white space between the semicolon and the slashes in front of comment, which would mean '//comment' is the last field
on the line and so only it is removed.
 
  


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
question in awk command dr.x Linux - Server 6 10-01-2013 05:17 AM
turning awk command line entries into awk scripts tabbyagirl Linux - Newbie 5 08-22-2013 12:46 AM
[SOLVED] Awk question (but could use other basic unix command) r0tty Programming 18 05-13-2011 11:44 AM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

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

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