Advertisement
image28

Example Grep's and Awk's Update 1

Jul 18th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.11 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # defines
  4. cat /usr/include/sys/stat.h | grep "#define "
  5. # conditional defines
  6. cat /usr/include/sys/stat.h | grep "# define "
  7.  
  8. # all root ifdefs
  9. cat /usr/include/sys/stat.h | awk '/# ifdef/,/# endif/'1
  10. # all code inbetween ifdef
  11. cat /usr/include/sys/stat.h | awk '/# ifdef/,/# endif/'$1
  12. cat /usr/include/sys/stat.h | awk '/# ifdef/,/# endif/'
  13.  
  14. # define tree
  15. cat /usr/include/sys/stat.h | awk '/#[ ]{0,}define/'
  16.  
  17. # ifdef, define, endif tree
  18. grep -E "(#[ ]{0,}define|#[ ]{0,}ifdef|#[ ]{0,}endif)" /usr/include/sys/stat.h
  19.  
  20. # list all includes
  21. grep -iE "#[ ]{0,}include" /usr/include/sys/stat.h
  22.  
  23. # list all error codes in a c header file ( -P used to detect tabs )
  24. grep -P "^#define( |\t){1,}E.*[0-9]{1,3}" /usr/include/asm-generic/errno.h
  25.  
  26. # list all error codes in all c header files in /usr/include folder and it's subfolders
  27. for e in `slocate "/usr/include/*.h"`; do grep -P "^#define( |\t){1,}E.*[0-9]{1,3}" $e; done
  28.  
  29. # list include tree
  30. grep -iE "#[ ]{0,}include" /usr/include/sys/stat.h
  31.  
  32. # list full file (*not finished)
  33. grep -E "(#[ ]{0,}define|#[ ]{0,}ifdef|#[ ]{0,}endif|#[ ]{0,}include)" /usr/include/sys/stat.h
  34.  
  35. # ifdef, define, endif, include tree
  36. grep -E "#[ ]{0,}(define|ifdef|ifndef|endif|include)" /usr/include/sys/stat.h
  37.  
  38. grep -iE "^[ ]{0,}[struct]{6}[ ]{1}.*" /usr/include/bits/statx.h
  39. grep -iE "(#[ ]{0,}(define|ifndef|ifdef|endif|include|error)|^[ ]{0,}struct )" /usr/include/bits/statx.h
  40.  
  41. # extract text from .msg format, could be improved
  42. cat Draft\ email\ cover\ letter.msg | grep -Eao "[A-Za-z0-9 \.\(\)\\\/\-]" | tr -d '\n' > ~/draft-extract
  43.  
  44. # find inode/(folders/subfolders) usage in folders
  45. find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n > ~/inode-usage.folders-`date +%j.%Y` # find inode usage in folders
  46. du --inodes -xS / > ~/inode-usage\(du\).folders-`date +%j.%Y` # find inode usage in folders
  47.  
  48. # replace octal terminal escape coded colours with conky colours
  49. sed 's/\(\\\033\[3\)\(.\)\(m\)/{color fg \2}/g' # foreground color ( replace 0-7 with conky colour names )
  50. sed 's/\(\\\033\[4\)\(.\)\(m\)/{color bg \2}/g' # background color ( replace 0-7 with conky colour names )
  51.  
  52. #Extract xml tag from line
  53. grep -iE "<title>.*</title>"
  54.  
  55. # Make more generic ( removes need for loops in a lot of situations in bash scripts )
  56. SKIP=`cat Regions | head -n4 | sed -e s/"\n"/"|"/g`; # region file contains lists of citys to group into regions comma separated for citys, endline for regions ( from xero-grab.sh )
  57. cat $NAME | grep -iE "<(City|Region)>" | grep -i "$l" | grep -viE "($(exec echo $SKIP))" > /dev/null; # grep skip with | or for citys for a single region
  58.  
  59. # Find variables in a Makefile
  60. function get-vars-from-makefile()
  61. {
  62.     IFS=$',';
  63.     for i in `cat Makefile | \
  64.             grep "CC" | \
  65.             grep -Eo "\\$\\([A-Z]{1,}\\)" | \
  66.             sort | \
  67.             uniq | \
  68.             cut -c3- | rev | cut -c2- | rev | \
  69.             tr '\n' ',' | \
  70.             rev | cut -c2- | rev`;
  71.     do
  72.         echo "$i";
  73.     done
  74. }
  75. # match a regex and for a c header, and insert comment to front ( & symbol in sed regex used to re-print whole match ($_ for whole line) )
  76. cat somefile.cpp | sed -E s/"^[ ]{0,}#include.*boost"/"\\/\\/&"/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement