Difference between revisions of "Unix Helpers"

From Blue-IT.org Wiki

(Created page with "== grep == === Colored output === Colered Output of a complete file. The regular expression "|$" matches those line that matches the pattern AND have an endline. This are ALL...")
 
(Colored output)
 
(2 intermediate revisions by the same user not shown)
Line 11: Line 11:
 
* http://superuser.com/questions/175527/is-there-a-tool-that-beeps-and-highlights-matching-lines-that-are-piped-through
 
* http://superuser.com/questions/175527/is-there-a-tool-that-beeps-and-highlights-matching-lines-that-are-piped-through
  
 +
<pre>
 
  #!/bin/bash
 
  #!/bin/bash
 
   
 
   
 
  regex="${1}"
 
  regex="${1}"
 
   
 
   
  # 1 green
+
  if [ "${regex}" = "" ]
  # 3 red
+
then
  highlight_on=$(tput setaf 2) # green, use setab to do inverse instead of foreground
+
echo "USAGE $0 <PATTERN> < <FILE>"
 +
exit 1
 +
fi
 +
 +
  # green, use setab to do inverse instead of foreground
 +
  highlight_on=$(tput setaf 2)
 +
 +
# reset to standard color scheme
 
  highlight_off=$(tput sgr0)
 
  highlight_off=$(tput sgr0)
 
   
 
   
 
  while read line
 
  while read line
 
  do
 
  do
    [[ $line =~ $regex ]] && echo -n $''"$highlight_on"
+
      [[ $line =~ $regex ]] && echo -n $''"$highlight_on"
    echo "$line$highlight_off"
+
      echo "$line$highlight_off"  
 
  done
 
  done
 +
 +
exit 0
 +
</pre>

Latest revision as of 10:56, 7 January 2014

grep

Colored output

Colered Output of a complete file. The regular expression "|$" matches those line that matches the pattern AND have an endline. This are ALL lines of the file!

egrep --color "pattern|$" file
grep --color -E "pattern|$" file
 #!/bin/bash
 
 regex="${1}"
 
 if [ "${regex}" = "" ]
 then 
 	echo "USAGE $0 <PATTERN> < <FILE>"
 	exit 1
 fi
 
 # green, use setab to do inverse instead of foreground
 highlight_on=$(tput setaf 2)
 
 # reset to standard color scheme
 highlight_off=$(tput sgr0)
 
 while read line
 do
      [[ $line =~ $regex ]] && echo -n $''"$highlight_on"
      echo "$line$highlight_off" 
 done
 
 exit 0