3

I have a log file containing multiple lines of data. I need to extract and the all the lines between the delimiters and save it to the output file

input.log

Some data 
<delim_begin>ABC<delim_end>
some data
<delim_begin>DEF<delim_end>
some data

The output.log file should look like

ABC

DEF

I tried this code but it does not work, it prints all the content of input.log

sed 's/<delim_begin>\(.*\)<delim_end>/\1/g' input.log > output.log
msd_2
  • 1,117
  • 3
  • 16
  • 22

4 Answers4

4

Using awk you can do it using custom field separator:

awk -F '<(delim_begin|delim_end)>' 'NF>2{print $2}' file
ABC
DEF

Using grep -P (PCRE):

grep -oP '(?<=<delim_begin>).*(?=<delim_end>)' file
ABC
DEF
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

sed alternative

$ sed -nr 's/<delim_begin>(.*)<delim_end>/\1/p' file

ABC
DEF
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

This should do it:

cat file | awk -F '<(delim_begin|delim_end)>' '{print $2}'

jkdba
  • 2,378
  • 3
  • 23
  • 33
Dheeraj R
  • 701
  • 9
  • 17
0

You can use this command -

cat file | grep "<delim_begin>.*<delim_end>" | sed 's/<delim_begin>//g' | sed 's/<delim_end>//' > output.log

avivb
  • 187
  • 11