linux练习-2

方法一:

bash 复制代码
#!/bin/bash
wc -l ./nowcoder.txt | awk '{print $1}'
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

方法二:

bash 复制代码
#!/bin/bash
awk '{print NR}' ./nowcoder.txt | tail -n1
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

方法三:

bash 复制代码
#!/bin/bash
awk 'END{print NR}' ./nowcoder.txt 
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

方法四:

bash 复制代码
#!/bin/bash
grep -c "" ./nowcoder.txt
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

方法五:

bash 复制代码
#!/bin/bash
sed -n '$=' ./nowcoder.txt
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

Linux 文本处理四剑客详细对比表

工具 主要功能 核心用途 常用选项/命令 典型应用场景
wc (Word Count)​ 文本统计 统计行数、单词数、字节数 -l统计行数 -w统计单词数 -c统计字节数 -m统计字符数 • 统计代码行数 • 计算日志文件大小 • 检查文档单词数量
grep (Global Regular Expression Print)​ 文本搜索 基于模式匹配搜索文本行 -i忽略大小写 -v反向匹配 -n显示行号 -c统计匹配数 -r递归搜索 • 日志错误排查 • 代码中查找函数 • 过滤命令输出
awk (Aho, Weinberger, Kernighan)​ 文本分析 结构化数据处理和报表生成 {print $1}打印第一列 NR行号变量 NF列数变量 -F指定分隔符 • 处理CSV/日志数据 • 数据统计计算 • 报表生成
sed (Stream Editor)​ 文本编辑 流式文本查找替换和编辑 s/old/new/替换 d删除行 p打印行 -i原地编辑 • 批量文本替换 • 配置文件修改 • 数据清洗
bash 复制代码
#!/bin/bash
tail -n 5 ./nowcoder.txt 
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

查看文件的前5行,可以使用head命令,如

head -5 filename

查看文件的后5行,可以使用tail命令,如:

tail -5 filename 或 tail -n 5 filename

查看文件中间一段,你可以使用sed命令,如:

sed -n '5,20p' filename

这样你就可以只查看文件的第5行到第20行。

bash 复制代码
#!/bin/bash
seq 0 7 500
read -a arr
while [ ${#arr[@]} -eq 2 ]
    do
        sum=$((${arr[0]} + ${arr[1]}))
        echo $sum
        read -a arr
    done
exit 0

一、输出连续的数字

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

相关推荐
Lzc7741 小时前
Linux网络的HTTPS
linux·https
运维李哥不背锅2 小时前
Ansible 模块详解:高效管理你的 IT 基础设施
服务器·网络·ansible
我爱钱因此会努力2 小时前
ansible自动化运维入门篇
linux·运维·服务器·centos·自动化·ansible
CIb0la2 小时前
能保持精神专注的爱好能给生活带来种种积极的转变
运维·学习·生活
---学无止境---3 小时前
Linux中系统调用sys_symlink和sys_unlink函数的实现
linux
代码程序猿RIP3 小时前
【Etcd 】Etcd 详解以及安装教程
linux·数据库·etcd
tb_first4 小时前
Linux入门1(2/2)
linux
cellurw4 小时前
Day72 传感器分类、关键参数、工作原理与Linux驱动开发(GPIO/I²C/Platform/Misc框架)
linux·c语言·驱动开发
NextZeros4 小时前
基于CentOS安装LNMP
linux·centos