Search code examples
linuxsedaix

Display element from file until a limit value in the file?


I've a lot of file that looks like :

2019-10-28-00-00;/dev/xxx;128.00;106.54;/var/x/x/xxx
2019-10-28-06-00;/dev/xxx;128.00;106.54;/var/x/x/xxx
2019-10-28-12-00;/dev/xxx;128.00;106.54;/var/x/x/xxx
2019-10-28-18-00;/dev/xxx;128.00;106.54;/var/x/x/xxx
2019-10-29-00-00;/dev/xxx;128.00;106.54;/var/x/x/xxx
2019-10-29-06-00;/dev/xxx;128.00;106.54;/var/x/x/xxx
2019-10-29-12-00;/dev/xxx;128.00;106.54;/var/x/x/xx
2019-10-29-18-00;/dev/xxx;128.00;21.46;/var/x/x/xxx
2019-10-30-00-00;/dev/xxx;128.00;21.46;/var/x/x/xxx

I would like to display the content of my files until the pattern " 2019-10-29-12-00 "

I've try :

sed -n '/2019-10-29-12-00/,/endif/p' file.txt

But this command did just the opposite.

I don't know how... Do you have any idea ?


Solution

  • This might work for you (GNU sed):

    sed '/2019-10-29-12-00/q' file
    

    This will print every line up to and including 2019-10-29-12-00, or

    sed '/2019-10-29-12-00/Q' file
    

    This will print every line up to but not including 2019-10-29-12-00

    Edit: as per user comments.

    sed '/2019-10-29-12-00/{:a;n;//ba;Q}' file
    

    Having found a line containing 019-10-29-12-00 print it and fetch the next line and repeat until the end of file or a line that does not contain 019-10-29-12-00. The :a is the name of the beginning of a loop, the n command prints the current line and fetches the next (or if end of file, terminates all sed commands), //ba uses the last regexp i.e. /2019-10-29-12-00/ and if true, breaks to the following loop name i.e. :a. Q terminates all sed commands but does not print the current line. The {...} limits the sed command to the previous regexp.