0

How can I add a few line breaks? I want it to show OpenFin category, have a line break, Chrome category, another line break, then memoryInfo.

Here is what I have so far:

cat LoginExInternal.txt | grep OpenFin >> LoginExcInternal.txt | echo $'\r' 
&& cat LoginExInternal.txt | grep Chrome >> LoginExcInternal.txt | echo $'\r' 
&& cat MemoryUnderThreshold.txt | grep memoryInfo:jsHeapSizeLimit:1 >> LoginExcInternal.txt | echo $'\r'

I can't seem to get it into new lines though, the echo $'\r' doesn't seem to work. Please tell me how I can modify to make this work.

  • 1
    It would help to see (a reasonable part of) the contents of your files LoginExInternal.txt and MemoryUnderThreshold.txt and how the result should look like. Do you use \r (CR) to get DOS/Windows line endings (\r+\n or CR+LF)? – Bodo Mar 13 at 13:10
2

You don't need to use cat with grep, and you can group the commands to only put the redirection there once:

{
  grep OpenFin < LoginExInternal.txt && echo 
  grep Chrome < LoginExInternal.txt && echo
  grep memoryInfo:jsHeapSizeLimit:1 < MemoryUnderThreshold.txt && echo
} >> LoginExcInternal.txt

Note also that if you join all the commands with &&, then a failing grep will stop the following echo, but also the rest of the greps from running. That may or may not be what you want. Using just grep && echo as above would have all the greps run in any case, but only print the extra newlines when the grep matches something.

Also, I'm not exactly sure how linebreaks are handled in cygwin, that is, if explicitly printing out the carriage return $'\r' is required.

  • This did not work for me. This is a part of a bigger script, when I added it, it said the grep command was not found. I think I will use what I have as it works, thanks for trying to help! – Ibrahim A Mar 13 at 13:50
  • @IbrahimA, grouping with braces shouldn't affect what commands are found or not, so if you had grep working in the original, I can't see why it wouldn't work here. – ilkkachu Mar 13 at 13:55
0

This seems to work. I am not sure if there is a simpler way to do this:

grep OpenFin LoginExInternal.txt >> LoginExcInternal.txt && 
echo $'\r' >> LoginExcInternal.txt && 
grep Chrome LoginExInternal.txt >> LoginExcInternal.txt &&
echo $'\r' >> LoginExcInternal.txt &&
grep MemoryUnderThreshold.txt memoryInfo:jsHeapSizeLimit:1 >> LoginExcInternal.txt
  • 1
    You don't need the cat commands. Instead of cat filename | grep pattern simply use grep pattern filename. – Bodo Mar 13 at 13:12
  • Great Point! I just changed it and it works perfectly. – Ibrahim A Mar 13 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.