Shell script to modify files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    Shell script to modify files

    Looking for shell script to modify file2 below based on first file. Basically I need to look for rows start with "TAPES" in file1, then I have to take 2nd column in that line "LOC" and look for matching word in file2, then edit that line in file2, the third column in file1 in that row tells that column should edit in file2, basically third column tells which column should edit in file, the fourth column in file1 is the replacement in file2 in that column. I should pass "TAPES" to the script and second file name like below. can I get script?.

    ./script1 file2 TAPES


    file1
    TAPES|LOC|2|CA
    CD|TOP|5|CA
    TAPES|LOC|3|PA
    CD|TOP|5|CA
    TAPES|LOC|5|TX


    file2
    LOC|2|STATE|NOT |FILL|
    LOC|2|PXA|FILL| EMPTY|
    LOC|2|STATE|NOT |FILL|
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Code:
    #!/bin/bash
    file1='file1' #specify your file1 name
    file2=$1      #file2 name
    kword=$2      #Word to find in first column of file1
    
    for line1 in `cat $file1`
    do
            first_word_file1=`echo $line1 | awk '{print $1}' FS="|"` > /dev/null
            if [ $first_word_file1 == $kword ]
            then
                    second_word_file1=`echo $line1 | awk '{print $2}' FS="|"` > /dev/null
                    third_word_file1=`echo $line1 | awk '{print $3}' FS="|"` > /dev/null
                    fourth_word_file1=`echo $line1 | awk '{print $4}' FS="|"` > /dev/null
    
                    echo $third_word_file1 $fourth_word_file1
                    #Create temporary file for future use
                    touch /tmp/temp1221.bf
    
                    #Now search file2 for line starting with $second_word_file1
                    for line2 in `cat $file2`
                    do
                            first_word_file2=`echo $line2 | awk '{print $1}' FS="|"` > /dev/null
                            if [ $second_word_file1 == $first_word_file2 ]
                            then
                                    #Now replace the word coming under column number specified
                                    #by $third_word_file1 by word in $fourth_word_file1
    
                                    target_word=`echo $line2 | awk -v N=$third_word_file1 '{printf("%s",$N)}' FS="|"` > /dev/null
                                    modified_line=`echo $line2 | sed s/\|$target_word\|/\|$fourth_word_file1\|/`
                                    echo $modified_line >> /tmp/temp1221.bf
    
                            else
                                    echo $line2 >> /tmp/temp1221.bf
                            fi
    
                    done
                    rm -rf $file2
                    mv /tmp/temp1221.bf $file2
            fi
    done
    This script is strictly based on files you provided..
    After running this script my file2 looked like:

    LOC|CA|PA|NOT|T X|
    LOC|CA|PA|FILL| TX|
    LOC|CA|PA|NOT|T X|

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      Great, thank you verymuch.

      Comment

      • tvnaidu
        Contributor
        • Oct 2009
        • 365

        #4
        Can I add random number for every entry which I modifled in file2 (append random number into the entry like below).

        LOC|CA|PA|NOT|T X| ----> LOC|CA1234|PA|N OT|TX|
        LOC|CA|PA|FILL| TX| -----> LOC|CA1234|PA45 67|NOT|TX|
        LOC|CA|PA|NOT|T X| -------> LOC|CA1234|PA45 67|NOT|TX3456|

        Comment

        • ashitpro
          Recognized Expert Contributor
          • Aug 2007
          • 542

          #5
          RANDOM=`date '+%s'`

          if you echo $RAMDOM you will get random number for each echo call...
          Add this statement to top of the script
          And change line number 29 to
          Code:
          modified_line=`echo $line2 | sed s/\|$target_word\|/\|$fourth_word_file1$RANDOM\|/`

          Comment

          • tvnaidu
            Contributor
            • Oct 2009
            • 365

            #6
            thank you verymuch, appreciated

            Comment

            • tvnaidu
              Contributor
              • Oct 2009
              • 365

              #7
              I added RANDOM into the program, it always generates same number, I need to append different number for each replacement. Is there anyway I can get different number usiong RANDOM everytime?.

              Now I gets 21468 for RANDOM. thanks in advance.

              Comment

              • tvnaidu
                Contributor
                • Oct 2009
                • 365

                #8
                When I run this script for a large original file, I am getting below error for line 23, any idea?, I was referring internet for this kind of error, some places says, "=" should be "==", but in this case, it is comparing those two words with "==", what else could be?.

                Line 23: [: LOC: Unary operator expected


                if [ $second_word_fi le1 == $first_word_fil e2 ]

                Comment

                • ashitpro
                  Recognized Expert Contributor
                  • Aug 2007
                  • 542

                  #9
                  I tried this at shell:
                  RANDOM=`date '+%s'`

                  later for each call of
                  echo $RANDOM

                  I had got random number, I am not sure what is going wrong in your case.
                  Try initializing RANDOM variable just above line 29, instead of at the top.

                  Comment

                  • ashitpro
                    Recognized Expert Contributor
                    • Aug 2007
                    • 542

                    #10
                    change this line:
                    if [ $second_word_fi le1 == $first_word_fil e2 ]
                    to:
                    if [ "$second_word_f ile1" = "$first_word_fi le2" ]

                    You are having empty strings in either second column of file1 or first column of file2, recheck the files. I haven't tested or written the script considering the emptiness of column.

                    Comment

                    • tvnaidu
                      Contributor
                      • Oct 2009
                      • 365

                      #11
                      Thank you verymuch, appreciated. Let me try.

                      Comment

                      Working...