AWK/Sed/Grep
著者: satoshiokita@gmail.com
目次
exercise_awk
fliename: exercise_awk.sh
#!/sw/bin/bash
# default : white space separete
export | grep HOME | awk '{ print $0}' # all
export | grep HOME | awk '{ print $1}' # stinrg1
export | grep HOME | awk '{ print $2}' # stinrg2
# change separator
export | grep HOME | awk -F= '{print $0}' # all
export | grep HOME | awk -F= '{print $1}' # string1
export | grep HOME | awk -F= '{print $2}' # string2
exercise_grep
fliename: exercise_grep.sh
#!/sw/bin/bash -x
# -x mean print execute log to stdout.
#
# Reference
# http://www.rsch.tuis.ac.jp/~mizutani/online/grep/regexp.html
#
# TODO
# grep '[a-z]\{1,5\}_[a-zA-Z]\{1,5\}_mytest[.]gpg'$B!!(B
#
#
# Initialize
#
target_txt="target.txt"
if [ -f "${target_txt}" ]
then
rm -rf ${target_txt}
fi
#
# Generate sample text file.
#
# mean following commands.
#
# cat <<-HEREDOC
# this mean that Here Document input redirect to the cat command.
#
# >> ${target_txt}
# this mean that output from buffer of the cat command to ${target_txt} variable.
cat <<-HEREDOC >> ${target_txt}
bbb^ccc
eeefff
^yyyy
zzzzz
******
a b c d e f g
012
999
abc111ddd
222ccc999
# Pattern Match
# <character>
# \<escape-char> *, ?, +, \, [, ], ^, -, |
# [abc] match a or b or c
# [A-Z] match alphabetic char
# [^A-Z] not match alphabetic char
# [A-Z^] match alphabetic char and ^
# [A-Z-] match alphabetic char and -
# ^d match forward d
# d$ match back d
# . match only one char
# \<
# \>
# \b
# \B
#
# Regular Expresison Match
# r
# r?
# r*
# r+
# r{n}
# r{n,}
# r{,n}
# r{n,m}
# r1|r2
# r1r2
HEREDOC
#
# using Pattern matching(NO regular expression)
#
grep '' ${target_txt}
#
# using Regular Expression
#
echo "grep '^[z^]' ${target_txt}"
grep '^[z^]' ${target_txt}
# Pattern Match
# <character>
grep 'a' ${target_txt}
grep 'b' ${target_txt}
grep 'abc' ${target_txt}
grep 'bbb' ${target_txt}
grep '^[a-z]' ${target_txt}
grep '^[#]' ${target_txt}
echo "*** num 3 "
grep '[0-9][0-9][0-9]' ${target_txt}
echo "*** num 3 head"
grep '^[0-9][0-9][0-9]' ${target_txt}
echo "*** num 3 foot"
grep '[0-9][0-9][0-9]^' ${target_txt}
echo "*** num only"
grep '[0-9]+' ${target_txt}
echo "*** space line only"
grep '^$' ${target_txt}
#echo "*** .*"
#grep '.*' ${target_txt}
#####
echo "*** [\^] is match \,\\, ^, a"
grep '[\^]' ${target_txt}
#####
echo "*** [^\] is NOT match \,"
grep '[^\]' ${target_txt}
#####
# tag
echo "abc" >> ${target_txt}
echo "abcabc" >> ${target_txt}
echo "bcdbcd" >> ${target_txt}
grep '\(abc\)\1' ${target_txt}
grep '\(bcd\)\1' ${target_txt}
#####
# acac, bcbc
echo "abababab" >> ${target_txt}
echo "acaczzzz" >> ${target_txt}
echo "bcbddddd" >> ${target_txt}
echo "bcbcdddd" >> ${target_txt}
grep '\([ab]c\)\1' ${target_txt}
#####
# abcdefdef
echo "abcdefghi" >> ${target_txt}
echo "abcdefdef" >> ${target_txt}
echo "opqrstuv" >> ${target_txt}
grep '\(abc\(def\)\)\2' ${target_txt}
#####
# r*
# a is from 0 to infi.
# z, az, a....z
echo "z" >> ${target_txt}h
echo "za" >> ${target_txt}h
echo "zaaaaaaaaaaa" >> ${target_txt}h
grep 'a*z' ${target_txt}
#####
# .*
# . mean all charactor.
# * mean 0...infi.
# d.*z
# dz, dxxxz, d789z ....
echo 'dz' >> ${target_txt}
echo 'dxxxz' >> ${target_txt}
echo 'd789z' >> ${target_txt}
echo 'd&**z' >> ${target_txt}
grep 'd.*z' ${target_txt}
##########
# or
# match to ac, bc,
echo "# or #"
grep '[a|b]c' ${target_txt}
##########
# GREP OPTIONS
# --help,--verion,
# -c, line Count
# -i, Ignore case
# -l, filename with match.
# -L, filename without match
# -n, line Number
# -r, recursive
##########
grep --help
grep --version
grep -c 'abc' ${target_txt}
grep -i 'abc' ${target_txt}
grep -l 'abc' ${target_txt}
grep -L 'abc' ${target_txt}
grep -cL 'abc' ${target_txt}
grep -r 'abc' ${target_txt}
grep -ilr 'abc' ${target_txt}
exercise_grep2
fliename: exercise_grep2.sh
#!/bin/sh
target_txt="grep2.txt"
if [ -f ${target_txt} ]
then
rm -rf ${target_txt}
fi
cat <<-HEREDOC >> ${target_txt}
abcdefdefz
abcdefdef
abcdefabcdef
def
abcdefghi
a
AAA
ABC
123
125
31
(123)
(abc)
(123abc)
HEREDOC
echo "**********"
grep '[a-z]' ${target_txt}
echo "**********"
grep '[a-z]*' ${target_txt}
echo "**********"
grep '[1-9]' ${target_txt}
echo "**********"
echo "* egrep "
echo "**********"
grep -e '\([123]\)' ${target_txt}
echo "**********"
# http://publib16.boulder.ibm.com/pseries/Ja_JP/cmds/aixcmds2/egrep.html
egrep '\(([A-z]+|[0-9]+)\)' ${target_txt}
echo "**********"
grep -E '\(([A-z]+|[0-9]+)\)' ${target_txt}
# pattern matching / Regular Expression
# * .*
# ? .
# [!a] [^a]
# [abc] [abc]
# [[:alpha:]] [[:alpha:]]
exercise_sed
fliename: exercise_sed.sh
#!/sw/bin/bash
# $Id: exercise_sed.sh 1197 2005-11-05 14:48:28Z s-okita $
# $Rev: 1197 $
# $Date: 2005-11-05 23:48:28 +0900 (土, 05 11 2005) $
# $Author: s-okita $
dest="${0}.sample.txt"
cat <<-EOF >> ${dest}
Hello Sed Sample
# sed target
AAAAA
BBBBB
<<<<<
>>>>>
# tr target
ABC
012
EOF
#####
echo "###### print target"
cat ${dest}
#####
echo "###### # replace string by sed"
cat ${dest} | sed -e "s/</</g" | sed -e "s/>/>/g" > ${dest}
cat ${dest}
#####
echo "##### # replace character by tr command"
cat ${dest} | tr '0' '9' > ${dest}
cat ${dest} | tr '[A-Z]' '*' > ${dest}
cat ${dest}
#####
rm -rf ${dest}
gen_ruby_html
fliename: gen_ruby_html.sh
#!/sw/bin/bash
# $Id: gen_ruby_html.sh 1460 2006-06-04 09:23:17Z s-okita $
#destination="/usr/local/apache2/htdocs/program/sed_awk_grep.html"
TITLE="AWK/Sed/Grep"
AUTHOR="著者: satoshiokita@gmail.com"
DATE=`date '+作成日:%Y年%m月%d日 %H:%M:%S %Z'`
EXT=".sh"
generate_header() {
cat << HEREDOC
<html>
<head>
<title>${TITLE}</title>
</head>
<body>
<h1><a name="0">${TITLE}</a></h1>
<p align="right">$AUTHOR</p>
<p align="right">$DATE</p>
<h2>目次</h2>
HEREDOC
}
generate_footer() {
cat << HEREDOC
</body>
</html>
HEREDOC
}
file_list=`ls *${EXT}`
generate_contents() {
echo "<ul>"
for item in ${file_list}
do
content_name=`echo $item | sed -e "s/${EXT}//"`
echo "<li><a href='#$item'>$content_name</a></li>"
done
echo "</ul>"
}
print_files() {
for item in ${file_list}
do
content_name=`echo $item | sed -e "s/${EXT}//"`
echo "<h2><a name='$item'>$content_name</a></h2>"
echo "<p>fliename: $item</p><pre><code>"
# sedで&は、\でエスケープしなければならない。
cat $item | sed -e "s/</\</g" | sed -e "s/>/\>/g"
echo "</code></pre><p align="right">"
echo "<a href='#0'>目次へ戻る</p>"
done
}
generate_header
generate_contents
print_files
generate_footer
