Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2004

    Unhappy Unanswered: AWK: Removing Characters from field


    When I use AWK I seem to have a problem with the newline character attaching itself to the last field I request. I use AWK to gather fields from a line in a log file. When I ask AWK to grab the last field, it takes the number plus the newline character at the end of the line. I would like to know of anyway of making AWK ignore the newline character when gathering field values. The reason I want to drop the newline character is that I want to add the string $MSU (passed by shell script) to the end of the line without this error occuring.

    /usr/bin/awk -v field=$fieldNum 'BEGIN{numOfFields=split(field,fieldList,",")} {
    for ( x = 2 ; x < NF ; x += 2 )
    fields[$x] = sprintf("%s",$(x+1));
    for ( i=1;i<=numOfFields;i++ ) {
    if ( fields[fieldList[i]] != "" )
    printf fields[fieldList[i]] " " ;
    print "'"$MSU"'";

    Any help on the matter would be greatly appreciated.

  2. #2
    Join Date
    Jun 2002
    I don't believe this is the case. A newline character is the default record separator in awk and would not be read in as part of the line.

    It's more likely the problem is down to the data you are running your script against.

    Can you post some sample data that would illustrate what you mean? The value of $1 you are passing to your script would also be useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts