I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2).
Mapping for pattern match and add line :
for Alpha 123
for Beta 234
for Gamma 345
no match (goes into another file)
File 1
insert_job: Alpha_jaa job_type: cmd
description: ABC
machine: XYZ
insert_job: Beta_baa job_type: cmd
description: ABC
machine: XYZ
insert_job: Gamma_caa job_type: cmd
description: ABC
machine: XYZ
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ
File 2
condition: 123
condition: 234
condition: 345
Output required(3 separate files created)
File Alpha.txt
insert_job: Alpha_jaa job_type: cmd
description: ABC
machine: XYZ
condition: 123
File Beta.txt
insert_job: Beta_baa job_type: cmd
description: ABC
machine: XYZ
condition: 234
File Gamma.txt
insert_job: Gamma_caa job_type: cmd
description: ABC
machine: XYZ
condition: 345
File nomatch :
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ
condition: None
cat $FILENAME | while read LINE
do
case "$LINE" in
"alpha")
echo $LINE >>alpha.txt
echo "condition: 123" >>alpha.txt
;;
"beta")
echo $LINE >>beta.txt
"echo condition: 234">>beta.txt
;;
echo $LINE >>gamma.txt
"gamma") echo "condition: 345">>gamma.txt
;;
*)
echo $LINE >>nomatch.txt
echo "condition: None" >>nomatch.txt
;;
;;
esac
done
I am still trying at my end and would like to mention where the problem is :
Writing the rest of the statements that follow into the respective files.
RudiC
2
Try this:
awk 'FNR==1 {f++}
f==1 {A[$2]=$0; next}
f==2 {B[$2]=A[$3]; next}
{for (x in B) if ($0 ~ x) {print $0, B[x] > x".txt"; next}}
{print $0, "condition: none" > "nomatch"}
' file2 file3 RS= FS="\n" OFS="\n" file1
This is a simple example that will accomplish what I believe you want to:
Input file:
$ cat t
insert_job: Alpha_jaa job_type: cmd
description: ABC
machine: XYZ
insert_job: Beta_baa job_type: cmd
description: ABC
machine: XYZ
insert_job: Gamma_caa job_type: cmd
description: ABC
machine: XYZ
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ
SHELL script:
# function: cond is used to write 'condition' line for each group
cond ()
{
if [[ $which_group == "A" ]]; then
echo "condition: 123" >> file_1.txt
elif [[ $which_group == "B" ]]; then
echo "condition: 234" >> file_2.txt
elif [[ $which_group == "G" ]]; then
echo "condition: 345" >> file_3.txt
elif [[ $which_group == "N" ]]; then
echo "condition: None" >> file_4.txt
fi
return
}
while read -r -a LineToArray
do
if [[ -z "${LineToArray[0]}" ]]; then
:
else
if [[ ${LineToArray[0]} == *insert_job: && ${LineToArray[1]} == *Alpha_* ]]; then
cond $which_group
echo ${LineToArray[@]} > file_1.txt
which_group="A"
elif [[ ${LineToArray[0]} == *insert_job: && ${LineToArray[1]} == *Beta_* ]]; then
cond $which_group
echo ${LineToArray[@]} > file_2.txt
which_group="B"
elif [[ ${LineToArray[0]} == *insert_job: && ${LineToArray[1]} == *Gamma_* ]]; then
cond $which_group
echo ${LineToArray[@]} > file_3.txt
which_group="G"
elif [[ ${LineToArray[0]} == *insert_job: ]]; then
cond $which_group
echo ${LineToArray[@]} > file_4.txt
which_group="N"
fi
if [[ ${LineToArray[0]} != *insert_job: && $which_group = "A" ]]; then
echo ${LineToArray[@]} >> file_1.txt
elif [[ ${LineToArray[0]} != *insert_job: && $which_group = "B" ]]; then
echo ${LineToArray[@]} >> file_2.txt
elif [[ ${LineToArray[0]} != *insert_job: && $which_group = "G" ]]; then
echo ${LineToArray[@]} >> file_3.txt
elif [[ ${LineToArray[0]} != *insert_job: && $which_group = "N" ]]; then
echo ${LineToArray[@]} >> file_4.txt
fi
fi
done < t
cond $which_group
Files Created:
$ cat file_1.txt file_2.txt file_3.txt file_4.txt
insert_job: Alpha_jaa job_type: cmd
description: ABC
machine: XYZ
condition: 123
insert_job: Beta_baa job_type: cmd
description: ABC
machine: XYZ
condition: 234
insert_job: Gamma_caa job_type: cmd
description: ABC
machine: XYZ
condition: 345
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ
condition: None