The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ERROR: more than one instance of overloaded function "vprintf" has "C" linkage donatoll HP-UX 0 10-14-2008 06:35 AM
iCal, Mac OS X 10.5: Empty "Home" and "Work" calendars may appear after installing Le iBot OS X Support RSS 0 10-13-2008 11:20 PM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-03-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question awk {'FS="," ... vs. awk -F"," ...

I used the following command

Code:
awk '{FS=","} $3>2007' $workfile5t > $workfile6t

But it appears to skip over the first record; even though field 3 is not > 2007, it is output.
However, the following appears to function properly

Code:
awk -F"," '$3>2007' $workfile5t > $workfile6t

Confused about the rule for the field separators.
  #2 (permalink)  
Old 12-03-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
awk 'BEGIN {FS=","} $3>2007' $workfile5t > $workfile6t

  #3 (permalink)  
Old 12-03-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question Where should BEGIN be placed if processing multiple files?

See this section of my larger program:

Code:
   awk -v wrk1=$workfile1ns -v wrk2x=$workfile2x -v wrk4=$workfile4 -v wrk5=$workfile5 -v planf=$workfile3 '
# put all plans in an array
      FILENAME==planf { FS=","
         ua1_pl[$1]=$2
         print "### 40a ## Set plan "$1" to "$2
         }
# put all jobs in an array
      FILENAME==wrk2x { FS=","
         j_num[$1]=$2
         j_yr[$1]=$3
         j_mon[$1]=$4
         j_month[$1]=$5
         print "### 40b ## Set job "$1"="$2" to month/year "$4"/"$3
         }
# put all the data together
      FILENAME==wrk1 { FS=","
# check on plan data
         if ( ua1_pl[$1] >"")
           { w_plan=ua1_pl[$1]
         } else
           { w_plan="***UA1 CODE UNDEFINED ["$1"]"
#           print "### 40c ## Undefined UA1 code "$1","$2
         }
# check on job number data
#         print $1,$2,j_num[$2],j_yr[$2]
         if ( j_num[$2] > "")
           { w_jnum=j_num[$2] ; w_jyr=j_yr[$2]
           w_jmon=j_mon[$2]
         }
# write out all the variables
         print $1","$2","w_plan","w_jnum","w_jyr","w_jmon > wrk4
         print ","w_jnum","w_jyr","w_jmon","w_plan > wrk5
   }' "$workfile3" "$workfile2x" "$workfile1ns"

I have to read through over a million records, and match up against a couple of smaller files. Thus, my thought to load the two smaller files into arrays and then read through the entire third file. I do array lookups on each record of the third file; thus creating one file containing all the important details.

In the example above, since I forced the FS to be "," I suppose I could simply change the awk -v ... to awk -F"," -v ... and be all set. But, what if the files had different delimiters? Where would I put the BEGIN before the FS="," commands?
  #4 (permalink)  
Old 12-03-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
assuming ALL 3 files have different FieldSeparators - adjust the FS assignments in the 'BEGIN' accordingly:

Code:
   awk -v wrk1=$workfile1ns -v wrk2x=$workfile2x -v wrk4=$workfile4 -v wrk5=$workfile5 -v planf=$workfile3 '
      BEGIN {
         FSf1=","
         FSf2="|"
         FSf3="#"
      }

      FNR == 1 {
          if (FILENAME==planf) FS=FSf3
          if (FILENAME==wrk2x ) FS=FSf2
          if (FILENAME==wrk1 ) FS=FSf1
          $1=$1
      }
# put all plans in an array
      FILENAME==planf { 
         ua1_pl[$1]=$2
         print "### 40a ## Set plan "$1" to "$2
         }
# put all jobs in an array
      FILENAME==wrk2x { 
         j_num[$1]=$2
         j_yr[$1]=$3
         j_mon[$1]=$4
         j_month[$1]=$5
         print "### 40b ## Set job "$1"="$2" to month/year "$4"/"$3
         }
# put all the data together
      FILENAME==wrk1 { 
# check on plan data
         if ( ua1_pl[$1] >"")
           { w_plan=ua1_pl[$1]
         } else
           { w_plan="***UA1 CODE UNDEFINED ["$1"]"
#           print "### 40c ## Undefined UA1 code "$1","$2
         }
# check on job number data
#         print $1,$2,j_num[$2],j_yr[$2]
         if ( j_num[$2] > "")
           { w_jnum=j_num[$2] ; w_jyr=j_yr[$2]
           w_jmon=j_mon[$2]
         }
# write out all the variables
         print $1","$2","w_plan","w_jnum","w_jyr","w_jmon > wrk4
         print ","w_jnum","w_jyr","w_jmon","w_plan > wrk5
   }' "$workfile3" "$workfile2x" "$workfile1ns"

  #5 (permalink)  
Old 12-03-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Joeyg,

If you change the field separator within the code, you have to rearrange the line with $0=$0.

Regards
  #6 (permalink)  
Old 12-03-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question

vgersh99 - thanks, that makes sense

Franklin52 - what is meant by your instruction $0=$0?
The solution proposed by vgersh99 has a $1=$1.
  #7 (permalink)  
Old 12-03-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
If you change the field separator within the code, the the command is read after the first record is read.
The command $0=$0 or $=$1 is to rearrange the fields with the new field separator in the buffer ($0).

Regards
Closed Thread

Bookmarks

Tags
awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0