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 03-10-2009, 04:14 PM   #1
slinx
Member
 
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:

Code:
    gawk --source='
        /^#/    {next}
        /^$/    {next}
        /^+/    {file = $1
                print "file: " $file
                for ( x=2; x <= NF; ++x ) {
                    print  x $x
                }   
                next}
        {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
Code:
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, 05:05 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,728

Rep: Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754
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.

jlinkels
 
Old 03-10-2009, 05:11 PM   #3
slinx
Member
 
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.
 
  


Reply


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 07:23 AM
modify all fields in awk tostay2003 Programming 16 08-09-2008 02:41 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
AWK - why fields go to seperate lines? korhan Linux - Newbie 2 03-01-2007 04:21 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 12:06 PM

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

All times are GMT -5. The time now is 04:34 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration