Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-10-2009, 03:14 PM   #1
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Rep: Reputation: 23
Unhappy AWK won't separate fields

Hello, I am having an odd problem with awk. Here is my script:

    gawk --source='
        /^#/    {next}
        /^$/    {next}
        /^+/    {file = $1
                print "file: " $file
                for ( x=2; x <= NF; ++x ) {
                    print  x $x
        {print}' $SOURCE_FILE > $INCLUDE_FILE
My source file is a file with # -delimited comments, so the awk program ignores lines beginning with # and blank lines. The next part is supposed to look for lines beginning with '+' and then print just the first field. If I do
print $1
, I get just the first field, but if I try to assign $1 to another variable, or use any function on it to assign it (such as sub or split), I get $0 instead - the entire line. I want to process the line and remove the leading '+' so I get just the file name.

What gives?

Also, what's an easy way to compare just the first character of an awk field, so I can check if the first character is a +, a -, or a (?

Thanks for your help.
Old 03-10-2009, 04:05 PM   #2
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,107

Rep: Reputation: 970Reputation: 970Reputation: 970Reputation: 970Reputation: 970Reputation: 970Reputation: 970Reputation: 970
In awk you don't use $variable to refer to a variable, just variable

I assume that if you refer to $file, awk thinks it is $0 as it cannot translate $file properly to a number.

Old 03-10-2009, 04:11 PM   #3
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Original Poster
Rep: Reputation: 23
That's all it was - it had to be something basic and simple like that! I haven't used awk in a while....

Thanks so much for your help.


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
modify all fields in awk tostay2003 Programming 16 08-09-2008 01:41 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
AWK - why fields go to seperate lines? korhan Linux - Newbie 2 03-01-2007 03:21 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 11:06 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration