1

I have a source file source.txt that contains these:

data1
data2
data3
::remove txt before this line::
data4
data5

This is the output that I want and save it to an output.txt file

data4
data5

Any command can be used as long as it will work in bash and can be saved as script file and run it as sh remove.sh

I tried:

cat source.txt | awk -F'::remove txt before this line::' '{print $1}' | tee > output.txt

But it just removes the line ::remove txt before this line::

Carl Alberto
  • 111
  • 4

2 Answers2

1

You can do it with sed:

sed '1,/^::remove txt before this line::$/d' source.txt

How it works

sed reads the content of the source.txt file line by line and applies the script provided in the first argument to each line.

The program above is very simple; it contains only one command: d (delete). The command applies to the rows that match the addresses provided in front of the command.

The d command above is provided two addresses: 1 and /^::remove txt before this line::$/.
1 means the first line of the file.
/^::remove txt before this line::$/ is a regular expression that matches the line(s) that contain exactly the string ::remove txt before this line:: (^ matches the beginning of the line, $ matches the end of the line).

The sed command above reads all the lines from the input file and removes (doesn't display) the starting with the first line of the file until (and including) the first line that contains only the text ::remove txt before this line::. All the other files are displayed on its output (it doesn't change the input file).

If there is no matching line in the input file, the command doesn't display anything.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

with awk, you should set the variable RS to empyt string to work with multilines:

awk 'BEGIN {RS=""; FS="::remove txt before this line::\n"} {print $2}'
georgexsh
  • 15,984
  • 2
  • 37
  • 62