27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

wc −w gives only the word count.<br />

wc −l gives only the line count.<br />

wc −c gives only the character count.<br />

wc −L gives only the length of the longest line.<br />

Using wc to count how many .txt files are in current working directory:<br />

$ ls *.txt | wc −l<br />

# Will work as long as none of the "*.txt" files have a linefeed in their name.<br />

# Alternative ways of doing this are:<br />

# find . −maxdepth 1 −name \*.txt −print0 | grep −cz .<br />

# (shopt −s nullglob; set −− *.txt; echo $#)<br />

# Thanks, S.C.<br />

Using wc to total up the size of all the files whose names begin with letters in the range d − h<br />

bash$ wc [d−h]* | grep total | awk '{print $3}'<br />

71832<br />

Using wc to count the instances of the word "Linux" in the main source file for this book.<br />

bash$ grep Linux abs−book.sgml | wc −l<br />

50<br />

See also Example 12−30 and Example 16−7.<br />

Certain commands include some of the functionality of wc as options.<br />

... | grep foo | wc −l<br />

# This frequently used construct can be more concisely rendered.<br />

... | grep −c foo<br />

# Just use the "−c" (or "−−count") option of grep.<br />

tr<br />

# Thanks, S.C.<br />

character translation filter.<br />

Must use quoting and/or brackets, as appropriate. Quotes prevent the shell from<br />

reinterpreting the special characters in tr command sequences. Brackets should be<br />

quoted to prevent expansion by the shell.<br />

Either tr "A−Z" "*"

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!