0

I have below file with me :-

====== 20160606:034441 ====== Mango(Test)
TestName     MangoT
Row  0
Season N
Name Safeda
Location    Delhi

====== 20160606:034441 ====== Mango(Result)
TestName     MangoR
Result  0
No_of_Mango 13
Quantity    2
Quantity    3
Quantity    6
Quantity    0
Quantity    1
Quantity    9
Quantity    54
Quantity    2
Quantity    4
Quantity    6
Quantity    76
Quantity    0
Quantity    99
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30


====== 20160606:034441 ====== Mango(Test)
TestName     MangoT
Row  0
Season N
Name Alphonso
Location    Mumbai


====== 20160606:034441 ====== Mango(Result)
TestName     MangoR
Result  0
No_of_Mango 13
Quantity    5
Quantity    3
Quantity    1
Quantity    0
Quantity    7
Quantity    8
Quantity    70
Quantity    3
Quantity    23
Quantity    43
Quantity    734
Quantity    2
Quantity    929
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30

Now i need two input files from above file based on Mango Name like :- FileName :- safeda.txt

TestName     MangoR
Result  0
No_of_Mango 13
Quantity    2
Quantity    3
Quantity    6
Quantity    0
Quantity    1
Quantity    9
Quantity    54
Quantity    2
Quantity    4
Quantity    6
Quantity    76
Quantity    0
Quantity    99
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30

2nd FileName :- Alphonso.txt

TestName     MangoR
Result  0
No_of_Mango 13
Quantity    5
Quantity    3
Quantity    1
Quantity    0
Quantity    7
Quantity    8
Quantity    70
Quantity    3
Quantity    23
Quantity    43
Quantity    734
Quantity    2
Quantity    929
Price   50
Price   70
Price   40
Price   30
Price   40
Price   30
Price   20
Price   60
Price   70
Price   80
Price   90
Price   30
Price   30

I need to create these 2 files using shell script.

  • You can use simple while read line; do... done. And if line begins with name, you change file you are writing in to. – knezi Jun 20 '16 at 12:51
1
#!/bin/bash

filename=""
do_write=0

while read line
do
  case $line in
    ==*Result*) do_write=1
                ;;
      ==*Test*) do_write=0
                filename=""
                ;;
         Name*) [[ $do_write == 0 ]] && filename=${line#Name }.txt
                ;;
            "") # Skip blank lines
                ;;
             *) [[ $do_write == 1 ]] && echo "$line" >> $filename
  esac
done

With your input file:

$ head -10 input
====== 20160606:034441 ====== Mango(Test)
TestName     MangoT
Row  0
Season N
Name Safeda
Location    Delhi

====== 20160606:034441 ====== Mango(Result)
TestName     MangoR
Result  0

We get the results:

$ ./parse < input

$ ls
Alphonso.txt  input  parse   Safeda.txt

$ head Alphonso.txt
TestName     MangoR
Result  0
No_of_Mango 13
Quantity    5
Quantity    3
Quantity    1
Quantity    0
Quantity    7
Quantity    8
Quantity    70    

$ head Safeda.txt
TestName     MangoR
Result  0
No_of_Mango 13
Quantity    2
Quantity    3
Quantity    6
Quantity    0
Quantity    1
Quantity    9
Quantity    54
1

Here's one way:

$ awk '(/=====/){a=0}
       (/\(Result\)\s*$/){a=1; next} 
       ($1=="Name"){n=$2}
       (a==1){print >> n".txt"}' file 

Explanation

  • (/=====/){a=0} : if the current line matches ======, set a to 0.
  • (/\(Result\)\s*$/){a=1; next}: if the current line ends with(Results)followed by 0 or more whitespace, setato1` and skip to the next line.
  • ($1=="Name"){n=$2} : if the 1st field is Name, set the n variable to the value of the 2nd field.
  • (a==1){print >> n".txt" : if a is 1, print this line into a file called n (the Name) with the extension .txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.