awk grep 以某个特定字母开头 shell 常用操作

判断文件夹是否存在

if [ ! -d "data" ];then
  mkdir data
else
  echo "file folder already exists"
fi

shell 接收命令行参数

$0是程序名,$1之后是参数, ${10}要加花括号。当命令行参数有空格时,用双引号包起来。
在test.sh文件中这样写,就可以运行 sh test.sh zhihu看到输出 zhihu
运行 sh test.sh "zhihu ha"就可以看到输出zhihu ha

name=$1
echo $name

shell 替换字符 sed

sed 's/A/B/'
将A替换为B
比如将文件中的括号和逗号去掉

cat test.txt | sed 's/(//' | sed 's/)//' | sed 's/,//' > out.txt

shell 取特定的列 awk

awk -F ',' '{print $1","$4}' test.txt
将以逗号为分隔符的数据test.txt 中第一列和第四列取出来。其中去掉 -F ','表示文件默认分隔符为空格,多个空格会被认为为一个空格处理。

grep 用法

筛选以特定字母开头

以r或者R开头的

cat test.spice | grep ^[rR].*

统计以r或者R开头的行数

cat test.spice | grep ^[rR].* | wc -l

不 以r或者R开头的 使用-v参数

cat test.spice | grep -v ^[rR].*

统计不同字符 并计数 uniq -c

查看 test.spice文件【cat test.spice】中以i或者I开头的行【| grep \^[iI].*】,输出以空格分隔的第四个参数【| awk '{print $4}'】,输出不同字符 并且统计个数【|uniq -c

cat test.spice | grep ^[iI].* | awk '{print $4}' | uniq -c

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_32507417/article/details/107097014