Score:0

How to save specific lines of command line outputs

cn flag

I have an executable that produces the following to the terminal:

---Start---
Some text
More text
-1.
1.
0.39
10
-199
some more text
   Complete
---End---

I want to write only the numerical values in the terminal to a file. Right now, I am using

./executable 2>&1 | grep -vE 'S|M|s|^[--]|^[ ]' | tee log.txt

This seems to do what I want. Specifically, by using -v, I am explicitly stating what to exclude. However, this seems rather inefficient since I would have to add in the whole alphabet (both upper and lower case) to make sure that only numerical values get added to the log.txt file.

Is there a better write all of the numerical values (positive and negative) to a file?

in flag
Are you looking for a new regex? '^[0-9-].*$' will capture whole lines starting with a digit or minus-sign. Or use \d for digits, see eg [regular-expressions.info](https://www.regular-expressions.info/quickstart.html).
hr flag
See for example [grep - Only allow floating points regex](https://unix.stackexchange.com/questions/397784/only-allow-floating-points-regex) and [Matching Floating Point Numbers with a Regular Expression](https://www.regular-expressions.info/floatingpoint.html)
bac0n avatar
cn flag
@pbhj the dot should be moved into the character class `'^[0-9.-]*$'` but as @steeldrive pointed out may not be enough.
waltinator avatar
it flag
Unless it's at the beginning of the expression, "`-`" is confused with the "range" ("`[0-9]`") operator. Use "`^[-.0-9]+`". You could also make use of `grep`'s "`-o`" option to output only the matched string.
bac0n avatar
cn flag
@waltinator that will match Start and End too...
in flag
@bac0n, none of the numbers start with a decimal point, my regex was to find lines starting with a digit or "-", the rest of the line can be any character. If you put the dot inside the range selector then you'll need to escape it because "." means "any character once", also it might be needed to escape the "-" (ie "\-") but if it's not between two other characters I've found the regex engine to always recognise it as a separate character. Depends on the version of grep (or other program); mine works for the given input in GNU Grep 3.6; tested on Kali.
bac0n avatar
cn flag
@pbhj first, the pattern will match the start and end tags too. Next, a dot inside `[.]` brackets are interpreted literally (no need to escape). If a hyphen is placed first or last in the range class it will also be interpreted literally.
in flag
You might be correct on the literal dot, not something I have experience with, but the hyphen not needing escaping certainly works, like I said, tested on GNU Grep. It matches the start because he's looking for lines with a digit at the start, it matches the end because the sample shows he wants the whole of the lines that have a digit (or minus) on. Try it.
bac0n avatar
cn flag
`echo '---End---' | grep '^[0-9-].*$'`
Score:1
cn flag

You simply need to exclude lines that have text in them. Use this grep in your command:

grep -v '[A-Za-z]'
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.