Unix [SOLVED]: Creating two files from one file using unix

Unix [SOLVED]: Creating two files from one file using unix

Home Forums Unix Unix [SOLVED]: Creating two files from one file using unix

Tagged: , , , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #37030

    Anonymous

    QuestionQuestion

    I have one input file, I need to read and apply some condition and route to two other files.

    100 COMPANY Records
    500ABC COMPANY 345         
    2pen9999out
    2cow7777out
    2goa7777out
    500ABC COMPANY IN 456
    2car9999out
    2cow7777out
    2HAT7777out
    2BAL9999out
    2BAL6666out
    

    here, record start with 5 was header and 2 was detail

    i need to create two file ABC_COMPANY.txt and ABC_COMPANY_IN.txt?

    I have written below logic, which I want to convert using awk or other right approach?

    NUMBER1="666"
    NUMBER2="777"
    while read line
     do
    
     REC_IND=`echo "${line}" | awk '{print substr($1,1,1)}'`
    
     if [ "${REC_IND}" = "5" ]; then
     FILENAME="ABC_COMPANY"
     DEAIL_COMPANY=`echo "${line}" | awk '{print substr($0,3,7)}'`
      if [[ $DEAIL_COMPANY = "ABC COMPANY IN" ]]; then
        FILENAME="ABC_COMPANY_IN"
      fi
    fi
    #check for detail record
      if [ "${REC_IND}" = "2" ] ;then
     #substring to find value
     Value=`echo "${line}" | awk '{print substr($0,4,9)}'`
      #if record belongs to bank then route to the respective file
      if [ "$Value" = "$NUMBER1" ]  || [ "$Value" = "$NUMBER2" ] ; then
        echo $line >> ${FILENAME}".txt"
      fi
    fi
    done <  /bk/shan.txthere
    

    expected output:

    ABC_COMPANY.txt

    2cow7777out
    2goa7777out
    

    ABC_COMPANY_IN.txt

    2cow7777out
    2HAT7777out
    2BAL6666out
    

    #37031

    Anonymous

    Accepted AnswerAnswer

    I would recommend using a proper Awk script like something below. This takes care of achieving your requirement as stated in the question and creates two files ABC_COMPANY.txt and ABC_COMPANY_IN.txt with contents as mentioned in the question.

    #!/usr/bin/env awk
    
    $1 ~ /^5/ {
        gsub(/[0-9]+/,"")
        gsub(/^[[:space:]]+|[[:space:]]+$/,"")
        gsub(/[[:space:]]/,"_")
        header=$0".txt"    
        openhandles[len++] = header
    }
    
    $1 ~ /^2/ && $1 ~ /666|777/ {
        print > header
    }
    
    END {
        for (i=0; i<len; i++ )
            close(openhandles[i])
    }
    

    Run the script as

    awk -f script.awk inputfile
    

    You can use the command-line version of the script as

    awk '$1 ~ /^5/ { gsub(/[0-9]+/,""); gsub(/^[[:space:]]+|[[:space:]]+$/,"");
    gsub(/[[:space:]]/,"_"); header=$0".txt"; openhandles[len++] = header; }
    $1 ~ /^2/ && $1 ~ /666|777/ { print > header }
    END {for (i=0; i<len; i++ )  close(openhandles[i]) }' file
    

    Source: https://stackoverflow.com/questions/48018230/creating-two-files-from-one-file-using-unix
    Author: Inian
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.