0

For example, if a have the following text in a file on my Linux system:

10-02-2020
given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
In addition, two variant programs egrep and fgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F
16-02-2020
The top program provides a dynamic real-time view of a running
       system.  It can display system summary information as well as a list
       of processes or threads currently being managed by the Linux kernel.
       The types of system summary information shown and the types, order
       and size of information displayed for processes are all user
       configurable and that configuration can be made persistent across
       restarts.

How can I delete all text before 16-02-2020 and turn the file into this:

16-02-2020
The top program provides a dynamic real-time view of a running
       system.  It can display system summary information as well as a list
       of processes or threads currently being managed by the Linux kernel.
       The types of system summary information shown and the types, order
       and size of information displayed for processes are all user
       configurable and that configuration can be made persistent across
       restarts.
1
  • 1
    actually is before, sorry! Commented Feb 16, 2020 at 18:52

3 Answers 3

3

You can do this with sed. The general format is:

sed -n '/pattern1/,/pattern2/p' file

The -n causes sed not to print unless explicitly told to with p. Som that command will print all lines that fall between a line matching pattern1 and one matching pattern2 (inclusive). If there are multiple matches, multiple lines will be printed.

In your case, you want to print everything until the end of the file, so pattern2 will be $. Therefore, you're looking for this:

$ sed -n '/16-02-2020/,$p' file
16-02-2020
The top program provides a dynamic real-time view of a running
   system.  It can display system summary information as well as a list
   of processes or threads currently being managed by the Linux kernel.
   The types of system summary information shown and the types, order
   and size of information displayed for processes are all user
   configurable and that configuration can be made persistent across
   restarts.

On an unrelated note, fgrep and egrep are deprecated, you should use grep -F and grep -E. See man grep:

In addition, the variant programs egrep and fgrep are the same as grep -E and grep -F, respectively. These variants are deprecated, but are provided for backward compatibility.

3
  • Hey there, I've tested here and it works, but can I edit your edit your answer to delete everything before and I aprove it? Commented Feb 16, 2020 at 18:49
  • 1
    @DaniloNeto already done. Please be careful next time though, as you can see this is now a very different solution. Commented Feb 16, 2020 at 19:38
  • Thank you a lot, worked perfectly Commented Feb 16, 2020 at 19:56
2

Perl version :

perl -ne '$f=1 if /16-02-2020/; print if $f' file
2
  • Note that the OP doesn't specify that 16-02-2020 will always be the only text in the line, with no whitespace before or after. You might want to just use perl -ne '$f=1 if /16-02-2020/; print if $f' file instead. Commented Feb 16, 2020 at 19:40
  • Updated, but it could cause problem if 16-02-2020 happen to appear in the text before. Commented Feb 16, 2020 at 19:59
1

For pruning a block from a file, I think ed, which treats the file as a whole instead of acting on a line at a time the way sed does is a better choice:

$ ed -s input.txt
1,/^16-02-2020/-1d
wq
$ cat input.txt
16-02-2020
The top program provides a dynamic real-time view of a running
       system.  It can display system summary information as well as a list
       of processes or threads currently being managed by the Linux kernel.
       The types of system summary information shown and the types, order
       and size of information displayed for processes are all user
       configurable and that configuration can be made persistent across
       restarts.

Deletes everything from the first line to the line before the one starting with 16-02-2020 and saves the modified file. If used in a script you can use a heredoc to send commands to ed:

ed -s input.txt <<EOF
1,/^16-02-2020/-1d
w
EOF

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.