У меня есть файл ниже :-
====== 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
Теперь мне нужно два входных файла из вышеуказанного файла на основе имени Манго, например :-.
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
Мне нужно создать эти 2 файла с помощью сценария оболочки.
Вот один из способов:
$ awk '(/=====/){a=0}
(/\(Result\)\s*$/){a=1; next}
($1=="Name"){n=$2}
(a==1){print >> n".txt"}' file
(/=====/){a=0}
: если текущая строка соответствует ======
, установите a
в 0
. (/\(Result\)\s*$/){a=1; next}: если текущая строка заканчивается на
(Results)за которым следует 0 или более пробелов, установите
aв
1` и перейдите к следующей строке. ($1=="Имя"){n=$2}
: если первое поле - Имя
, установите переменную n
в значение второго поля. (a==1){print >> n".txt"
: если a
равно 1
, выведите эту строку в файл с именем n
(Имя) с расширением .txt
. #!/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
С вашим входным файлом:
$ 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
Получаем результаты:
$ ./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