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

Abhijeet Anand

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 <br /> 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 <br/> 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 <br/> 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 <br> tag only in specicfied user input text area not whole HTML body file?

xhienne

You can make your substitution with awk. Replace:

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

with

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

Do the same for agenda and lscrm. If your are using GNU awk and you don't want the initial and/or ending <br>, you can play with RS:

attendees=$( awk 'BEGIN { RS = "\n*\\|\n*" } ...' $file )

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

extracting a subset of lines from a large text file

분류에서Dev

extract text from file using sed

분류에서Dev

Using sed to get specific text from XML file

분류에서Dev

Replace text in file with variable using sed

분류에서Dev

Formatting text file with awk command

분류에서Dev

How to get text file content to table using awk,sed,grep or cut in linux terminal prompt

분류에서Dev

Replacing plain text in HTML file, using js function

분류에서Dev

Pipe lines of text into a linux command

분류에서Dev

Extracting columns from a text file with no delimiters

분류에서Dev

Data extraction from a text file using bash

분류에서Dev

Editing nested text and specific lines within a file using bash script

분류에서Dev

change and manipulate lines in a file using awk

분류에서Dev

Extracting float numbers from file using python

분류에서Dev

Echo specified lines from a text file

분류에서Dev

Extract several lines from large text file

분류에서Dev

Change line in text file from other text file with command line

분류에서Dev

Advancing text in a text file using user input

분류에서Dev

Text File processing - using java

분류에서Dev

Using Grunt to Replace Text in a File

분류에서Dev

Search pattern from tag and inserting it to next line by using sed or awk

분류에서Dev

How to Merge Lines from 2 Text File without creating a Line Break in Batch Command

분류에서Dev

Is there any way to Copy and Paste from one file(from beginning to end) to another using SED and AWK?

분류에서Dev

Python Write lines of a text in between a range of numbers to a new file

분류에서Dev

how to delete the last line in a text file with 100M lines without having to rewrite the whole file?

분류에서Dev

read specific values from text file

분류에서Dev

Looking for lines which is in one file but not in other using Unix and Awk

분류에서Dev

replace lines from a file using script

분류에서Dev

Problem Counting the lines of a text file

분류에서Dev

Simple way of extracting a value from a file using regex (python)

Related 관련 기사

  1. 1

    extracting a subset of lines from a large text file

  2. 2

    extract text from file using sed

  3. 3

    Using sed to get specific text from XML file

  4. 4

    Replace text in file with variable using sed

  5. 5

    Formatting text file with awk command

  6. 6

    How to get text file content to table using awk,sed,grep or cut in linux terminal prompt

  7. 7

    Replacing plain text in HTML file, using js function

  8. 8

    Pipe lines of text into a linux command

  9. 9

    Extracting columns from a text file with no delimiters

  10. 10

    Data extraction from a text file using bash

  11. 11

    Editing nested text and specific lines within a file using bash script

  12. 12

    change and manipulate lines in a file using awk

  13. 13

    Extracting float numbers from file using python

  14. 14

    Echo specified lines from a text file

  15. 15

    Extract several lines from large text file

  16. 16

    Change line in text file from other text file with command line

  17. 17

    Advancing text in a text file using user input

  18. 18

    Text File processing - using java

  19. 19

    Using Grunt to Replace Text in a File

  20. 20

    Search pattern from tag and inserting it to next line by using sed or awk

  21. 21

    How to Merge Lines from 2 Text File without creating a Line Break in Batch Command

  22. 22

    Is there any way to Copy and Paste from one file(from beginning to end) to another using SED and AWK?

  23. 23

    Python Write lines of a text in between a range of numbers to a new file

  24. 24

    how to delete the last line in a text file with 100M lines without having to rewrite the whole file?

  25. 25

    read specific values from text file

  26. 26

    Looking for lines which is in one file but not in other using Unix and Awk

  27. 27

    replace lines from a file using script

  28. 28

    Problem Counting the lines of a text file

  29. 29

    Simple way of extracting a value from a file using regex (python)

뜨겁다태그

보관