-1

For example: If below is the content of my log file, I want to keep only the contents between the first occurrence of value '2018' and last occurrence of '2018'

sssssssssssssssssssss
ddddddddddddddddddddd
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018aaaaa

Output should be:

2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018
ghoti
  • 45,319
  • 8
  • 65
  • 104
Shawn
  • 4,064
  • 2
  • 11
  • 23
  • 1
    What have you tried? Please show your attempt so far, so that we can help you with your code. And what does this have to do with winscp? – ghoti Oct 03 '18 at 11:22
  • I have created a bat file which runs WinSCP script to download a log file (response log) from remote server. The log file obviously downloads as-is (responses from all the requests). Once the log file is downloaded, I want to cut only the response specific to my unique ID.Script within my bat file is as below: `winscp.com/command ^ "open sftp://xxx.com/ -hostkey=*" "get /var/log/jboss_sit/suFile.log" "exit" ^` – Shawn Oct 03 '18 at 14:25
  • You'll need to put that and more detail *into your question*. A solution in bash or even POSIX shell would be super easy, but I can't replicate your environment unless I know what it is. – ghoti Oct 03 '18 at 16:05

2 Answers2

1

If the file is not too big, you can do it with GNU grep and the -z flag. From grep.info:

‘-z’
‘--null-data’
     Treat input and output data as sequences of lines, each terminated
     by a zero byte (the ASCII NUL character) instead of a newline.
     Like the ‘-Z’ or ‘--null’ option, this option can be used with
     commands like ‘sort -z’ to process arbitrary file names.

For example:

grep -zo '2018.*2018' infile

Output:

2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018
Thor
  • 45,082
  • 11
  • 119
  • 130
1

Check this Perl solution

> cat 2018_match
sssssssssssssssssssss
ddddddddddddddddddddd
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018aaaaa
> perl -e ' BEGIN { $x=qx(cat 2018_match);$x=~s/(.+?)(2018.+2018)(.+)/$2/osmg; print "$x\n" ; exit } '
2018fffffffffffffffff
dddddd2018ddddddddddd
wwww2018wwwwwwwwwwwww
aaaaaaaaaaaa2018
>
stack0114106
  • 8,534
  • 3
  • 13
  • 38
  • thanks @stack0114106 and @Thor. Can you please help me with findstr solution. I wrote below code, but it retrieves only the lines matching 2018, but i want all lines between first and last occurrence of 2018. `findstr /s "2018" suFile.log > results.log` – Shawn Oct 03 '18 at 14:29