shell

lc192

# cat words.txt
the day is sunny the the
the sunny is is

统计一个文本文件words.txt中每个单词出现的频率

# cat words.txt | tr ' ' '\n' | grep -v '^$' | awk '{ ++S[$1] } END { for ( a in S) print a,S[a]}' | sort -r -n -k2
the 4
is 3
sunny 2
day 1
# awk '{ for (i=1;i<=NF;i++) ++S[$i]} END { for (a in S) print a,S[a]}' words.txt | sort -r -n -k2
the 4
is 3
sunny 2
day 1
# cat words.txt | xargs -n1 | sort | uniq -c | awk '{print $2,$1}' | sort -r -n -k2
the 4
is 3
sunny 2
day 1

lc 195
打印文件的第10行
file.txt

# cat a.bash
rowCount=$(awk 'END {print NR}' file.txt)

if [ $rowCount -lt 10 ];then
    echo "less than 10 lines"
else
    awk '{ if (NR==10) print $0}' file.txt
fi

lc 194
给定一个文件,转置它的内容

# cat file.txt
name age
alice 21
ryan 30
# cat file.txt | awk '{ for (i=1;i<=NF;i++) { if (NR==1) { row[i]=$i} else {row[i]=row[i]" "$i}}} END { for (a in row) {print row[a]}}'
name alice ryan
age 21 30
# cat file.txt
name age
alice 21
ryan 30
# cat a.bash
colCount=$(awk 'END {print NF}' file.txt)

for ((i=1;i<=$colCount;i++));do
    cat file.txt | cut -d ' ' -f $i | xargs
done

假设 file.txt 内容如下:

987-123-4567
123 456 7890
(123) 456-7890
你的脚本应当输出下列有效的电话号码:

987-123-4567
(123) 456-7890

# cat file.txt
987-123-4567
123 456 7890
(123) 456-7890
# cat file.txt | grep -E '([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}'
987-123-4567
(123) 456-7890

查看timewait阶段的socket的数量

netstat -antup | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a,S[a]}'

6.查看 打开文件句柄数最多的进程

lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr -k1 | head -n1

abcdefghijklmn
一个文本有很多这样的字符串,每一行都删除c和j之间的字符

# echo abcdefghijklmn | sed 's/\([^cj]*\)c[^cj]*j\([^cj]\)/\1\2/g'
abklmn

find . -type f -exec ls {} ;

查看打开文件句柄数最多的进程的PID

lsof | awk 'NR>1 {++S[$2]} END { for(a in S) {print a,"\t",S[a]}}'|sort -n -k 1

1        21
2        3
3        3
4        3
5        3
6        3
7        3
8        3
9        3
10       3
11       3
12       3
13       3

lsof | awk 'NR>1 {++S[$2]} END { for(a in S) {print a,"\t",S[a]}}'|sort -n -k 2|tail -n 1

2157     85

查看各个状态的TCP Socket的个数

netstat -ant | awk 'NR>2 {++S[$NF]} END {for(a in S) {printf "%-12s %-5s\n",a,S[a]}}'

TIME_WAIT    24   
ESTABLISHED  25   
LISTEN       16  

10.linux中想对一个文件中内容相同的行去重(即多行内容一致就只保留一行)应该输入什么命令执行。

# cat hello.txt
hello world
awk
coding ants
hello world
awk
hello world
awk
coding ants
coding ants
# awk '!a[$0]++' hello.txt
hello world
awk
coding ants

你可能感兴趣的:(shell)