Search code examples
regexbashsedgreppcregrep

Get specific lines from text file to another text file


I have a plain text file with many lines, I am trying to get only the lines that start with |V| and runtime

What I have tried:

sed -e '/^\(runtime\|\\|V\\|\)/p' test.txt > test.out.txt
sed -e '/^(runtime\|\\|V\\|)/' test.txt > test.out.txt
sed -e '/^runtime/p' -e '/^\\|V\\|/p' test.txt > test.out.txt
sed -i.bak '/^\\|V\\|\|runtime/!d' test.txt
sed -i.bak '/^(\\|V\\|\|runtime)/!d' test.txt
pcregrep -M '^(runtime|\|V\|)*' test.txt > test.out.txt
egrep '^(runtime|\|V\|)*' test.txt > test.out.txt

Nothing works. I will either get an empty file or the same file duplicated.


Solution

  • Like this? First some test data:

    $ cat foo
    |V|
    runtime
    foo
    

    then the greṕ command:

    $ grep "^\(|V|\|runtime\)" foo
    |V|
    runtime