当前位置: 首页 > article >正文

牛客linux

1、 统计文件的行数

# 方法 1
wc -l ./nowcoder.txt | awk '{print $1}'
# 方法 2 ,awk 可以打印所有行的行号, 或者只打印最后一行
awk '{print NR}' ./nowcoder.txt |tail -n 1
awk 'END{print NR}' ./nowcoder.txt
# 方法 3 grep -c 、-n等等
grep -c "" ./nowcoder.txt 
grep -n "" ./nowcoder.txt  | awk -F ":" '{print $1 }' | tail -n 1
# 方法 4 sed
sed -n '$=' ./nowcoder.txt

awk常用的变量

$0 表示当前处理的整个一行
$1 是分割后的第一个字段,$n 每行第n个字段
-F 用来指定分隔符
NF 字段数量变量(Number Of Field,当前行的字段的个数,(当前行被分成了几列),字段数量)
NR 行号,当前处理的文本行的行号,会连续记录行号
# 输出全部内容
awk 
BEGIN 模式指定了处理文本之前需要执行的操作(第一行)
END 模式指定了处理完所有行之后需要执行的操作


在这里插入图片描述

2、 打印文件的最后5行 和 5个字节

# 
tail -n 5 nowcoder.txt [或者:tail -5 nowcoder.txt] : 输出文件的最后5行
tail -n +5 nowcoder.txt : 输出从第5行开始到文件结尾的内容;
tail -n -5 nowcoder.txt : 输出从倒数 第五行开始到文件结尾的内容;
tail -c 5 nowcoder.txt : 表示输出文件最后5个字节;

3、输出 0 到 500 中 7 的倍数

# 方法 1
for item in {0..500..7}
do 
    echo $item
done
# 方法 2, seq命令用于输出 连续的数字、 固件间隔的数字、指定格式的数字
seq 0 7 500

# 带循环list的for循环
实现一:依次输出训练list中的内容 【list内容可以是数字也可以是串】
for item in 1 4 5  hello world
 do 
    echo $item
done
运行结果:
1
4
6
hello
world


实现二:依次输出一个范围内的值,如下为输出1到5的值
for item in {1..5}
do
    echo $item
done
运行结果:
1
2
3
4
5

实现三: 设置输出的间隔值,如下为间隔2输出【也就是输出10以内的所有奇数】
for item in {1..10..2}
do
    echo $item
done
运行结果:
1
3
5
7
9

实现四: 输出当前目录下所有的文件和文件夹
for file in $(ls)
do 
    echo $file
done

for file in *
do 
    echo $file
done

# 不带循环list的for循环
将输入的参数循环输出【这里使用@来获取参数列表】,脚本test.sh 内容如下:

echo "input the world:"
for item in "$@"
do 
    echo $item
done
echo "total param : $#" 

运行: sh test.sh  hello world   【带了两个参数】
运行结果: 
intput the world
hello
world
total param : 2

【备注】 $@ 获取参数列表内容;  $# 获取输入参数的个数;

一、输出连续的数字
   seq 1 100 
     表示: 输出所有1到100之间的数字;

二、输出固定间隔的数字
   seq 0 7 500
     表示: 输出所有 0到500内 7个倍数;

三、输出指定格式的数字 
  1、【-s 用于使用指定的字符串分割数字】
   seq -s "+"  1 100
    表示:输出1到100之间的数字,每个数字间由+号间隔;
  
  2、【-f 使用print 样式的浮点格式输出,默认使用 %g 】
   seq -f "file%g"  1 10
    表示:输出给是为: file1  到 file10 ;  如下:
   file1
   file2
   file3
   file4
   file5
   file6
   file7
   file8
   file9
   file10

4、输出第5行的内容

# 方法 1
head -n 5 nowcoder.txt | tail -n 1
# 方法 2 sed -n :仅将 sed 动作 编辑后的数据进行输出。"n1,n2动作":对n1行到n2行进行操作。p:动作,表示print,打印输出。
sed -n 5p
sed -n "5,5p" nowcoder.txt
# 方法 3 
awk -n "NR==5" ./nowcoder.txt
# NR==5 匹配第五行;{}中放匹配后要执行的命令, 0输出行的所有内容
awk 'NR==5{print 0}' ./nowcoder.txt

5、打印空行的行号

# 方法1 grep, ^代表行首,$代表行尾,然后 grep -n捕获该行,并且打印行号; sed 's/a/b/' 会将a替换成b,因此sed 's/://' 就实现了去掉:的效果
grep -n '^$' $1 | sed 's/://'

# 方法 2 /^$/表示空字符串即该行是空字符串即空行;=输出行数
sed -n '/^**$/= ' nowcoder.txt 
sed -n '/^$/=' nowcoder.txt
# 方法 3
awk '/^$/ {print NR}' nowcoder.txt 

6、去掉空行

# 方法 1
grep -v '^$'
# 方法 2 判断当前行的内容然后输出
awk '{if($0 != "") {print $0}}' ./nowcoder.txt
# 方法 3 awk 做非空校验然后输出
cat ./nowcoder.txt | awk NF

# 方法 4 sed 是流式编辑器,主要用来对文件做增删改操作;sed是对文件做逐行读取,逐行匹配,匹配通过,执行sed命令,匹配不通过原样输出,默认不对源文件做修改,要修改加 -i 

sed '/^$/d' nowcoder.txt

7、打印字母数小于8的单词

# 方法 1
#!/bin/bash
# NF是当前记录的字段数
awk -F" " '{for(i=1;i<=NF;i++){if(length($i) < 8){print $i}}}' nowcoder.txt

# 方法 2
IFS=" "
words=($(cat nowcoder.txt))
for word in ${words[@]}
do
    [ ${#word} -lt 8 ] && echo $word
done < nowcoder.txt
exit 0

# 方法3、4、5
cat nowcoder.txt | sed 's/ /\n/g' | awk '{if(length($0)<8) print}'

cat nowcoder.txt | awk 'BEGIN{RS=" "} {if(length($0)<8) print $0;}'

cat nowcoder.txt | xargs -n 1 | awk '{if(length($0)<8) print;}'

cat nowcoder.txt | sed 's/ /\n/g' | awk '/^.{1,7}$/'

8、统计所有进程占用内存百分比的和

# 方法 1
awk -F" " '{sum+=$4} END{print sum}' nowcoder.txt

# 方法 2
 awk 'BEGIN{sum=0}{sum+=$4}END{print sum}' ./nowcoder.txt

http://www.kler.cn/a/427926.html

相关文章:

  • Linux: shell: bash: Makefile:5: *** missing separator. Stop.
  • 过期策略、内存淘汰机制
  • 深入浅出:使用 Gin 框架生成 API 文档
  • 算法日记 41 day 图论
  • Mysql数据库指令(持续积累)
  • STM32移植文件系统(FatFs)
  • 3D数据大屏实现过程,使用echarts、Next.js
  • IS-IS三
  • 在 Ubuntu 中解决 Python 程序中 DataFrame 图表中文乱码问题
  • LabVIEW调用Thorlabs的动态库进行开发
  • 【精选】AI Coding 新范式:Windsurf、Cursor、Coze齐上阵
  • 兔子的寿命有多长?
  • 数据库-mysql(基本语句)
  • 四十一:Web传递消息时的编码格式
  • Scala中条件守卫
  • 基于Matlab特征提取与浅层神经网络的数字图像处理乳腺癌检测系统(GUI界面+训练代码+数据集)
  • 架构07-从类库到服务
  • 最优质量运输概述(自用)——一、蒙日问题、Kantorovich问题
  • 数据结构 ——无头单链表
  • 装饰器—购物打折