Place digit in front of the line searching pattern using sed command

hi All,

i want to add the single digit front of the line in the report file and string compare with pattern file.

patter file: pattern1.txt

pattern num
like 4
love 3
john 2

report file: report.txt

i like very much
but john is good boy
i will love u

so after execute the script output is report.txt file like

4 i like very much
2 but john is good boy
3 i will love u

---------- Post updated at 08:18 PM ---------- Previous update was at 12:52 PM ----------

Please update anyone using by sed command...

What the output, if have two and more patterns found in one line?

i like john very much 

And why it needs be sed command?

hi ,

Please check now. what i want na ...i have a one report file and pattern file.
i get the pattern from pattern file and compare to report file if matchine i get the line and place the equal digit to front of the line in the same report file.

patter file: pattern1.txt

pattern num
like 4
love 3
john 2

report file: report.txt

i like very much
but john is good boy
i will love u

output -> report.txt file like

4 i like very much
2 but john is good boy
3 i will love u

Your still don't answer my question:

What's the expect output, if have two and more patterns found in one line?

i like john very much

something like:

4 2 i like john very much
or 
4 i like john very much
or 
2 i like john very much

output similar like.....

4 i like john very much

Sorry for miscommunication....Thanks in advance

awk 'NR==FNR{a[$1]=$2;next}
{for(i=1;i<=NF;i++) if($i in a) print a[$i] FS $0}' pattern report
4 i like very much
2 but john is good boy
3 i will love u
1 Like

really Thank you....can u explain the command.

another thing i need to update the output report file itself.

awk 'NR==FNR{a[$1]=$2;next} ## NR>FNR means awk is reading through the "pattern" as input, also building an array a: a[$1]=$2. "next" means then start to read "report" 
{for(i=1;i<=NF;i++) if($i in a) print a[$i] FS $0}  ## if $i exsit in the array a, print a[$i](the value is from "pattern") and $0 
' pattern report

using a temp file like

 awk code pattern report >tmp; cat tmp >report ;rm tmp 

Thanks ya!...

sorry for trouble again...i have wriiten one code for same thing but i am using sed command it is going to place the digit front of the pattern in the report file. but all the lines is there....

Please check my script,pattern file and sample report files and give suggestion how to place the digit front of the line.

here pattern file Name: sch.txt
Report file Names Ext: *.R

script:

for i in `ls *.R`
do
cat sch.txt | while read LINE
do
CLT=`echo $LINE | cut -f1 -d "|"`
NAME=`echo $LINE | cut -f2 -d "|"`
NUM=`echo $LINE | cut -f3 -d "|"`
echo " $NAME $NUM$NAME "
comp_string=`grep "%s" $i| cut -d ":" -f2 | cut -d "|" -f1`
if [ $CLT = $comp_string ]
then
NAME1=`grep $NAME $i`
cat $i | sed "s:$NAME:$NUM$NAME:g" > tmp_file.dat
cat tmp_file.dat > $i
else
echo " Not Matching CLient Code"
fi
done
done

try:

for i in `ls *.R`
do
cat sch.txt | while read LINE
do
CLT=`echo $LINE | cut -f1 -d "|"`
NAME=`echo $LINE | cut -f2 -d "|"`
NUM=`echo $LINE | cut -f3 -d "|"`
echo " $NAME $NUM$NAME "
comp_string=`grep "%s" $i| cut -d ":" -f2 | cut -d "|" -f1`
if [ $CLT = $comp_string ]
then
#NAME1=`grep $NAME $i` 
sed -i "/$NAME/s/.*/$NUM &/" $i
#cat $i | sed "s:$NAME:$NUM$NAME:g" > tmp_file.dat
#cat tmp_file.dat > $i
else
echo " Not Matching CLient Code"
fi
done
done