Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2013
    Posts
    1

    Unanswered: awk to remove data until number of fields

    I have a file
    1|2|3|4
    a|b|c|d
    1|2
    3|4
    a|
    b|
    c|
    d|

    The file should have 4 fields to load into a database. The file may have cr, lf, or end of line characters.

    What I want to see as output is
    1|2|3|4
    a|b|c|d
    1|23|4
    a|b|c|d

    I have tried

    BEGIN {FS="|";break_flag = 0;field_count=4}
    {
    #print NF
    delc=gsub(/\|/,"|",$0)
    print delc
    if (NF == 4 )
    {
    print $0
    }
    else if (delc != 3)
    {
    #gsub("\"","")
    gsub(/\r/,"")
    printf("%s|",$0)
    }
    }

    What I want is that if the line does not have 4 fields, continue reading until you get 4 fields and print them without cr, lf etc only | as the field separator

    Thanks,

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    #t
    IFS="|"             
    while read a b c d  
    do                  
    echo $a             
    if [ "a$b" != "a" ] 
    then                
            echo $b     
    fi                  
    if [ "a$c" != "a" ] 
    then                
            echo $c     
    fi                  
    if [ "a$d" != "a" ] 
    then                
            echo $d     
    fi                  
    done <Source_data
    Code:
    #t1
    while read a        
    do                  
    read b              
    read c              
    read d              
    echo $a\|$b\|$c\|$d 
    done
    .
    to execute
    Code:
    ./t |./t1

Posting Permissions

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