Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: [SOLVED] Print certain string from a big file (awk/vim/grep) (Read 563 times) previous topic - next topic
0 Members and 2 Guests are viewing this topic.

[SOLVED] Print certain string from a big file (awk/vim/grep)

Hello guys! Trying to extract from a text file any reference that contains .ko.zst string. Tried with grep but can't use wildcards
For example i have this text but this is just a small part the file is huge about 5000 lines so can't manually edit it.
Code: [Select]
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha256-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha512-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-avx-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64-3way.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/power.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/intel-cstate.ko.zst

I need only the last part that contains .ko.zst (e.g: sha256-ssse3.ko.zst) to be printed in a text file or in terminal. How this can be done, in awk vim or what's the best tool for this?

Re: Print certain string from a big file (awk/vim/grep)

Reply #1
Code: [Select]
$ grep '.*\.ko\.zst$' << EOT | sed 's#.*/\([^/]\+\.ko\.zst\)$#\1#g'
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha256-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha512-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-avx-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64-3way.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/power.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/intel-cstate.ko.zst
EOT
sha256-ssse3.ko.zst
sha512-ssse3.ko.zst
twofish-avx-x86_64.ko.zst
twofish-x86_64-3way.ko.zst
twofish-x86_64.ko.zst
power.ko.zst
intel-cstate.ko.zst
For a file, naturally, you would use:
Code: [Select]
$ grep '.*\.ko\.zst$' <some.file | sed 's#.*/\([^/]\+\.ko\.zst\)$#\1#g'

Update: awk one-liner (single program execution, no pipes):
Code: [Select]
$  awk '{print gensub(/.*\/([^/]+\.ko\.zst)$/, "\\1", "g", $1)}' <some.file

Re: Print certain string from a big file (awk/vim/grep)

Reply #2
Hello guys! Trying to extract from a text file any reference that contains .ko.zst string. Tried with grep but can't use wildcards
For example i have this text but this is just a small part the file is huge about 5000 lines so can't manually edit it.
Code: [Select]
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha256-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha512-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-avx-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64-3way.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/power.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/intel-cstate.ko.zst

I need only the last part that contains .ko.zst (e.g: sha256-ssse3.ko.zst) to be printed in a text file or in terminal. How this can be done, in awk vim or what's the best tool for this?

A simple "grep" & "cut" also get it done. :)

Code: [Select]
/tmp » cat foo                                                                                                                                  
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha256-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/sha512-ssse3.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-avx-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64-3way.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/crypto/twofish-x86_64.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/amd/power.ko.zst
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/
    usr/lib/modules/5.14.11-hardened1-1-hardened/kernel/arch/x86/events/intel/intel-cstate.ko.zst

/tmp » grep "ko.zst" foo | rev | cut -f1 -d'/' | rev                                                                                           
sha256-ssse3.ko.zst
sha512-ssse3.ko.zst
twofish-avx-x86_64.ko.zst
twofish-x86_64-3way.ko.zst
twofish-x86_64.ko.zst
power.ko.zst
intel-cstate.ko.zst

Re: Print certain string from a big file (awk/vim/grep)

Reply #3
Ohohoho I made it thank you very much @strajder you're the boss ;D. Absolutely brilliant!!!  ;D

 

Re: Print certain string from a big file (awk/vim/grep)

Reply #4
For the guys that needs to remove certain string like i do from a text file can be used sed

Code: [Select]
sed -i -e 's/goodbye//g' filename.txt

where the word goodbye is the string to be removed from the text file. Thought would be useful for the guys that may need this also