Extracting values from a text file having | pipe as a delimiter in text file using awk command and Replacing new lines with
tag using sed?

I have a text file in which I have 3 sections values separated by pipe. My text file:

Saibal,Navnath,Taral,Sagar,Ankit,Prasham,Manika,Arvind,Gaurav,Abhijeet,Rohit,Madhu,Ganesh,Zahoor|
LSCRM:Abhijeet

MCRM:Zahoor

TLGAPI:Bhargav

MOM:Manika|
Prod :
No major activity were scheduled and no issues were reported throughout the week.
Last weekend on Sunday, we performed Full bounce. We are doing so to allow any USDOTT transaction during this window while they do code-fix (they need CRM available at all times).
Coming weekend, we have ordering client Ef deployment and CK External BLM Phase 2 activity scheduled on both APH and STL.

Non-Prod:
Over the week, we released 1710 CT11 K2view to build: 220 and Env TRN3 to 1707 Build:300.

Now I'm extracting values from this text file using shell script and storing it in a shell variable and now I want to replace the shell variable value to a variable in my HTML file. In this replacement, I want to replace the new lines encountered in the values in text file (stored in shell variable) with
tag
so that in my HTML file, output should be in the same format as input given in text file for all the 3 sections.

My shell script to extract and replace the values is :

#! /bin/bash -x
file='/home/websphe/tomcat/webapps/MOM/mom.txt'
file1='/home/websphe/tomcat/webapps/MOM/web/mom.html'
common_path='/home/websphe/tomcat/webapps/MOM/web/'
if test -s $file
   then
cp $file1 $common_path/momcpy.html
attendees=$( awk 'BEGIN { RS = "|" } NR == 1 { print }' $file )
echo "$attendees"
agenda=$( awk 'BEGIN { RS = "|" } NR == 2 { print }' $file )
echo "$agenda"
lscrm=$( awk 'BEGIN { RS = "|" } NR == 3 { print }' $file )
echo "$lscrm"
perl -p -i -e "s#attendees#$attendees#g" $common_path/momcpy.html
perl -p -i -e "s#agenda#$agenda#g" $common_path/momcpy.html | sed -i'' 's|\n|<br/>|' $common_path/momcpy.html
perl -p -i -e "s#lscrm#$lscrm#g" $common_path/momcpy.html | sed -i'' 's|\n|<br/>|' $common_path/momcpy.html

Now, here you can see attendees section, I don't want
tag
as there is no new line in 1st section that but I want in agenda section and lscrm section. Note: In above script attendees, agenda and lscrm are variables present in different columns of a table in my HTML file where I want to replace

perl -p -i -e "s#agenda#$agenda#g" $common_path/momcpy.html | sed -i'' 's|\n|<br/>|' $common_path/momcpy.html

With above attempt
tag is inserted in whole html file and so due this my table in html file is aligned very down in chrome or IE browser.

What changes should be done in above script to get
tag only in specicfied user input text area not whole HTML body file?

0
10.09.2017, 13:56
1 ответ

Вы можете произвести замену с помощью awk. Замените:

attendees=$( awk 'BEGIN { RS = "|" } NR == 1 { print }' $file )

с

attendees=$( awk 'BEGIN { RS = "|" } NR == 1 { print gensub("\n", "<br />", "g") }' $file )

Сделайте то же самое для agendaи lscrm. Если вы используете GNU awk и вам не нужны начальная и/или конечная <br>, вы можете поиграть с RS:

attendees=$( awk 'BEGIN { RS = "\n*\\|\n*" }...' $file )
0
28.01.2020, 04:40

Теги

Похожие вопросы