Skip to content

Instantly share code, notes, and snippets.

@vpnwall-services
Last active November 22, 2023 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpnwall-services/b5ee5387ada2e031196fdcc5b46207c7 to your computer and use it in GitHub Desktop.
Save vpnwall-services/b5ee5387ada2e031196fdcc5b46207c7 to your computer and use it in GitHub Desktop.
[GREP 101] Grep 101 #grep #101 #find #word #line #after #before #recursive #between #tags #script

GREP 101

  • Grep code recursively between <script></script> tags grep -oRP '<script[^>]*>(.*?)</script>'

  • Grep minimum six numbers grep -E '[0-9]{6}' file.txt

  • Grep files that does not containt string grep -L "foo" *

  • Find file and grep two values in one grep find . -name 'myfile' -exec grep 'VAL1\|VAL2' {} ';'

  • Grep two patterns grep -E 'pattern1.*pattern2' -r

  • Grep a tail output tail -F file.txt|grep --line-buffered 'stringtogrep'

  • Print only filename grep -l -R '\[a\]'

  • Grep word after match grep -oP 'APP_SOCKET_URL=\K.*' .env

  • Grep line after match grep -A2 "mymatch" myfile.txt

  • Grep 10 lines after match grep -iR -A 10 'cors:'

  • Grep 5 lines before match grep -iR -B 10 'cors:'

@guest271314
Copy link

Can you provide any guidance on how to convert a JavaScript RegExp to one that grep will match?

@vpnwall-services
Copy link
Author

Hi,

i found this example => https://stackoverflow.com/questions/7432379/convert-javascript-regexp-to-work-with-grep-or-sed

The main idea is to use egrep (grep -E) for extended Regexp

I hope it fits you needs !

@guest271314
Copy link

I read that question and answer before asking here. I adjusted the code at https://stackoverflow.com/questions/50666954/switch-puls-audio-record-stream-for-gstreamer-pipeline

SOURCE_OUTPUT_INDEX=`pactl list source-outputs | grep -E "(Source\sOutput|media\.name)" | grep Record\Stream -B1 | grep -o -E -m1 "[0-9]+"`
SOURCE_INDEX=`pactl list sources | grep -E "(Source\s|device\.class)" | grep monitor -B1 | grep -o -E -m1 "[0-9]+"`
pactl move-source-output $SOURCE_OUTPUT_INDEX $SOURCE_INDEX

Now I will try to adjust the code to toggle between "monitor" device.class and device.class "sound" when the script is called. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment