Partially output commandline screen to file

4

I'm writing a logfile for a backup solution.

The backup solution is a simple 7-zip update command.

I'd like to log which files it backups. If I use >> log.txt I will get the result, but also lots of other information that 7-zip outputs that I don't want.

I'm looking for a way to partially log what is on the screen. I don't mind to use 7z .... >templog.txt and then doing something to templog.txt before appending it to log.txt. But I don't know how to selectively remove lines from templog.txt from the commandline either.

So I guess my question boils down: How can I remove the first 10 lines from templog.txt or alternatively only redirect part of the output on screen to a logfile?

Bonus question: How to selectively remove lines from templog.txt or the output redirection that are not at the beginning?

LPChip

Posted 2016-09-01T12:02:02.327

Reputation: 42 190

Answers

2

How can I remove the first 10 lines from templog.txt or alternatively only redirect part of the output on screen to a logfile?

Below are a couple different ways to accomplish what you ask for omitting lines with certain strings in them with Findstr or else to just remove specific line numbers with SED from templog.txt before it appends what's in it to the full log file of log.txt.


Using FINDSTR

This will omit the strings in the double-quotes which are in templog.txt from being appended to log.txt

FINDSTR /V /I "string1 string2 string3 string4" templog.log>>log.txt

This will omit the regular expressions in the double-quotes after each /C: switches which are in templog.txt from being appended to log.txt so you'd just have to be sure to have each regular expression which you need to match for omitting.

FINDSTR /V /R /C:"<regex1>" /C:"<regex2>" /C:"<regex3>" templog.log>>log.txt

Using SED

This will remove lines 1 - 10 from templog.txt and then after that operation append the rest to log.txt

sed -i -e "1,10d" templog.txt

Further Resources

Pimp Juice IT

Posted 2016-09-01T12:02:02.327

Reputation: 29 425

I looked into Findstr but given that it searches for words rather than actual strings or I have to use regexes, it seemed to not fit my needs. using SED however does. I'd love to see a proper solution that does not require any additional software though. This answer definitely deserves a +1 so that has been given. I'm going to wait a few more days to see if more answers are given before accepting this one. – LPChip – 2016-09-01T14:53:08.057

The regex one is definitely more interesting. Can it regex over the content of the entire file, past newlines too? So if I find a string, remove from there all the way until I find another string which is 8 lines down for example? – LPChip – 2016-09-01T16:20:03.327

@LPChip findstr can search across line breaks. See http://ss64.com/nt/findstr-linebreaks.html

– DavidPostill – 2016-09-01T16:36:14.133

2

@LPChip What are the undocumented features and limitations of the Windows FINDSTR command? by dbenham (our resident batch file guru) is worth a read ...

– DavidPostill – 2016-09-01T16:37:56.140

Windows 8.1 or 10, and the output of the tempfile will be 20 to 30 lines big possibly, but lets say in extreme cases, the file is still under 1 mb. Well within the limits of findstr. Diving more and more into findstr, it seems I might be able to use the match against other file option to accomplish my goal the quickest. – LPChip – 2016-09-01T16:46:06.237

@LPChip I think it'll work just fine then regardless with Findstr and you just tell it what action you want it to take accordingly whether match, exclude, omit, append, etc. I think it'll work just fine with your explanation so it should be easy enough for you to test. Let me know how it goes... I did this on Windows 10 from my side today for my answers by the way. – Pimp Juice IT – 2016-09-01T16:48:48.983

Awesome. Yeah, I'll be testing this tomorrow. The target pc is those of my parents, where I implemented a simple backup solution that backups their documents to a 7z file on shutdown (gpedit) Due to it being at a moment there is no user interaction, I needed a logfile. Given that its not my pc, a logfile is always great for troubleshooting, but the 7z output is just too much, and only using the stderr is insufficient (which does show when it was unable to update the file due to it being in use, but given that I don't expect that to happen, I want to refine the logging a bit more. – LPChip – 2016-09-01T20:14:00.823

Doesn't sound like a good idea. Much work for something small. Their documents are not much. I think 2gb or something like that, but the update 7z takes less than a second for no files to backup anyway. – LPChip – 2016-09-01T22:15:25.010

@LPChip No worries, I usually remove my comments to keep it clean though; just let me know how it goes when you get to it. – Pimp Juice IT – 2016-09-02T00:14:49.967

1

PowerShell - Remove Specific Lines from a File

You can use the below PowerShell commands to skip the first five lines using the -Skip parameter with Select-Object from the content of the templog.txt file, and then update it with everything except those file lines skipped to omit.

(Get-Content "C:\path\templog.txt" | Select-Object -Skip 5) | Set-Content "C:\path\templog.txt"

You can use the below PowerShell commands using the -Skip parameter with Select-Object to skip the first five lines of the templog.txt, and then append all but those lines to the log.txt file.

(Get-Content "C:\path\templog.txt" | Select-Object -Skip 5) | Add-Content "C:\path\log.txt"

Further Resources

Pimp Juice IT

Posted 2016-09-01T12:02:02.327

Reputation: 29 425

Nice idea, but unfortunately for what I want it to do not that practical. +1 for thinking outside of the box though. :) – LPChip – 2016-09-01T16:20:38.793