shell_常用过滤命令或脚本

蛋蛋 2022年09月13日 17 0

行数

获取单个文件行数

awk '{print NR}' test1.sh|tail -n1
awk 'END{print NR}' test1.sh
grep -n "" test1.sh|awk -F: '{print '}|tail -n1
sed -n '$=' test1.sh
wc -l test1.sh
cat test1.sh |wc -l

获取特定目录所有文件的行数

针对本目录:./count.sh

统计多个目录:./count.sh /tmp ~

#!/bin/bash
filesCount=0
linesCount=0
function funCount()
{
    for file in ` ls $1 `
    do
        if [ -d $1"/"$file ];then
            funCount $1"/"$file
        else
            declare -i fileLines
            fileLines=`sed -n '$=' $1"/"$file`
            let linesCount=$linesCount+$fileLines
            let filesCount=$filesCount+1
        fi
    done
}

if [ $# -gt 0 ];then
    for m_dir in $@
    do
        funCount $m_dir
    done
else
    funCount "."
fi
echo "filesCount = $filesCount"
echo "linesCount = $linesCount"

获取特定目录特定扩展名文件的行数

#!/bin/bash
extens=(".c" ".cpp" ".h" ".hpp")

filesCount=0
linesCount=0
function funCount()
{
    for file in ` ls $1 `
    do
        if [ -d $1"/"$file ];then
            funCount $1"/"$file
        else
            fileName=$1"/"$file
           
            EXTENSION="."${fileName##*.}
            echo "fileName = $fileName  Extension = $EXTENSION"

            if [[ "${extens[@]/$EXTENSION/}" != "${extens[@]}" ]];then
                declare -i fileLines
                fileLines=`sed -n '$=' $fileName`
                echo $fileName" : "$fileLines
                let linesCount=$linesCount+$fileLines
                let filesCount=$filesCount+1
            fi
        fi
    done
}

if [ $# -gt 0 ];then
    for m_dir in $@
    do
        funCount $m_dir
    done
else
    funCount "."
fi
echo "filesCount = $filesCount"
echo "linesCount = $linesCount"

输出文件的指定行

tail date.log               输出文件末尾的内容,默认10行
tail -20  date.log        输出最后20行的内容
tail -n -20  date.log    输出倒数第20行到文件末尾的内容
tail -n +20  date.log   输出第20行到文件末尾的内容
tail -f date.log            实时监控文件内容增加,默认10行。
head date.log           输出文件开头的内容,默认10行
head -15  date.log     输出开头15行的内容
head -n +15 date.log 输出开头到第15行的内容
head -n -15 date.log  输出开头到倒数第15行的内容
sed -n 文件名 //"开始行,结束行p"
sed ‘/pattern/!p’ infile //匹配pattern的行不输出
sed -n ‘1,2p’ infile //print line 1 to 2
sed -n '6p;260,400p; ' 文件名    //输出第6行 和 260到400行
sed -n ‘2,$p’ file //print line 2 to end of line
sed -n 5p 文件名                       //输出第5行

tail 和 head 加上 -n参数后 都代表输出到指定行数,tail 是指定行数到结尾,head是开头到指定行数

+数字 代表整数第几行, -数字代表倒数第几行

$ cat jf_count.txt 
01	15
02	30
03	44
04	59
05	73
06	87
07	102
08	116
09	131
10	145
11	159
12	174
13	188
14	203
15	217
16	231
17	246
18	260
19	275
20	289
21	303
22	318
23	332
24	347
25	361
26	375
27	390
28	404
29	419
30	433
31	447
$ head -n 5 test.txt 
时间	总带宽带宽(Mbps)
2021-11-18 19:05:00	26.08
2021-11-15 16:55:00	25.51
2021-11-18 19:00:00	24.75
2021-11-09 03:30:00	22.07
$ arr=();for i in `awk '{print $1}' jf_count.txt`;do count=`grep '^$i'$'\t' jf_count.txt|awk '{print $2}'` ; j="^2021-11-$i"; arr[${#arr[*]}]=${j}; grep_arr=`echo ${arr[*]}|sed 's/ /|/g'`; grep -E $grep_arr test.txt |sed -n ${count}p;done
2021-11-01 10:35:00	10.96
2021-11-01 13:40:00	9.42
2021-11-02 18:20:00	5.23
2021-11-04 15:00:00	11.09
2021-11-04 18:30:00	11.26
2021-11-04 23:30:00	11.19
2021-11-06 01:25:00	11.14
2021-11-08 13:05:00	11.19
2021-11-05 08:30:00	11.18
2021-11-05 17:30:00	11.15
2021-11-05 17:55:00	11.13
2021-11-05 10:15:00	11.15
2021-11-04 18:40:00	11.13
2021-11-05 08:15:00	11.11
2021-11-12 12:15:00	11.15
2021-11-08 11:55:00	11.21
2021-11-16 11:05:00	11.3
2021-11-15 11:15:00	11.43
2021-11-18 14:55:00	11.48
2021-11-20 11:35:00	11.58
2021-11-19 13:20:00	11.53
2021-11-18 14:00:00	11.63
2021-11-17 17:25:00	11.61
2021-11-04 19:35:00	11.54
2021-11-15 10:30:00	11.53
2021-11-12 10:00:00	11.46
2021-11-15 11:15:00	11.43
2021-11-05 14:35:00	11.41
2021-11-05 03:25:00	11.39
2021-11-08 10:55:00	11.36
2021-11-16 15:20:00	11.34
$ arr=();for i in `awk '{print $1}' jf_count.txt`;do count=`grep -P ^"$i\t" jf_count.txt|awk '{print $2}'` ; j="^2021/11/$i"; arr[${#arr[*]}]=${j}; grep_arr=`echo ${arr[*]}|sed 's/ /[[:space:]]|/g;s/$/[[:space:]]/g'`;  egrep -E "$grep_arr" test.txt |sed -n ${count}p;done
2021/11/1 10:55	10.97
2021/11/1 9:55	9.48
2021/11/1 14:10	5.34
2021/11/4 15:00	11.09
2021/11/4 15:20	11.26
2021/11/4 23:40	11.2
2021/11/5 13:30	11.14
2021/11/5 8:05	11.19
2021/11/5 2:40	11.18
2021/11/5 10:15	11.15
2021/11/4 18:40	11.13
2021/11/5 2:45	11.15
2021/11/4 18:35	11.13
2021/11/5 8:10	11.11
2021/11/12 11:45	11.15
2021/11/8 11:40	11.21
2021/11/15 10:10	11.3
2021/11/4 10:35	11.43
2021/11/17 9:25	11.48
2021/11/19 15:30	11.58
2021/11/17 16:55	11.53
2021/11/1 9:15	11.64
2021/11/22 14:20	11.62
2021/11/17 10:00	11.55
2021/11/8 15:45	11.53
2021/11/5 0:00	11.46
2021/11/4 10:35	11.43
2021/11/5 9:05	11.41
2021/11/5 3:25	11.39
2021/11/26 23:55	11.37

只输出最后一个字符

STR=123456abc
FINAL=`echo ${STR: -1}`
或者
FINAL=${STR: -1}

去掉最后一个字符

Last Updated: 2022/09/13 18:51:18
linux基础命令_eval linux基础命令_test