-1

This is my date format in my file: [1/24/21 9:19:20:183 MYT].

I want to print all entries marked with timestamps from this moment onward to the end of the file. How can I do this using sed?

2 Answers 2

1

Do you want to only print the date or the entire line containing the date?

To print the entire line, I would use grep '\[1/24/21 9:19:20:183 MYT\]' file, but if you insist on using sed, it can be accomplished with sed -n '/\[1\/24\/21 9:19:20:183 MYT\]/p' file.

EDIT: Ugly backslashes can be avoided with grep -F, as suggested by user freddy in the first comment below.

The -n option ensures that lines are only displayed if they are explicitly printed by the sed program. The p command explicitly prints the line.

To only print the date, grep is also the better solution. You just add the -o or --only-matching option. It should also be possible with sed, but requires more skill than I have.

EDIT: OP actually has a different requirement and modified the question. You can give a line range to sed. "from the line that contains [1/24/21 9:19:20:183 MYT] to the end" is written as /\[1\/24\/21 9:19:20:183 MYT\]/,$. The dollar sign indicates the last line. So:

sed -n '/\[1\/24\/21 9:19:20:183 MYT\]/,$p' file
4
  • i got this error "grep: illegal option -- F" after i use in solaris 11. Commented Jan 25, 2021 at 5:59
  • If the Solaris version of grep doesn't have the -F option, use backslashes to remove the special meaning of [ and ]. You could also use the manual page to find out if Solaris grep has an alternative. Commented Jan 25, 2021 at 6:02
  • Solaris seems to have two grep versions. From the manual (not sure if Solaris 11) at docs.oracle.com/cd/E26502_01/html/E29030/grep-1.html: "The options -E and -F affect the way /usr/xpg4/bin/grep interprets pattern_list". Commented Jan 25, 2021 at 6:04
  • 1
    @berndbausch /usr/bin/grep' on Solaris systems is the legacy Solaris grep` that's pre-POSIX. Solaris has quite a significant forward-compatibility guarantee going back over two decades, so legacy utilities with unique behaviors are kept around - Solaris awk is almost legendary (or is that infamous?) in that regard. /usr/xpg4/bin/grep is the POSIX-compliant grep. And Solaris 11 probably has ggrep, which would be GNU grep. Commented Jan 25, 2021 at 15:29
0

If it doesn't have to be sed, the following awk program would work:

awk -v start="[1/24/21 9:19:20:183 MYT]" 'index($0,start)==1{f=1} f' file

This will parse the file until it finds a line starting with the string as specified in the start variable. Once found, it sets a flag f to 1. Lines are only printed if this flag is set.

This assumes a strict time ordering of entries, i.e. you cannot have "earlier" timestamps occuring after the line with the start timestamp. Although improbable, this is possible in case of buffered logging.

2
  • Hi I got below error when i run the awk command awk:syntax error near line 1 awk:bailing out near line 1 Commented Jan 26, 2021 at 1:43
  • @DastageerUmar Hmmm, I tested it with gawk, nawk and mawk on a Linux system and it worked in all cases. Can you post which awk version you have?
    – AdminBee
    Commented Jan 26, 2021 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .