image28

Example Grep's and Awk's Update 1

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