[工具] Shell 读取一个文件 (以列的方式)

介绍

作者:朱明宇
名称:读取 1 个文件(以列的方式)
作用:读取 1 个文件,并把里面的内容以空格作为分割符,分成 3 列显示出来

使用方法:
1. 给此脚本添加执行权限
2. 执行此脚本,并在输入此命令时,在后面添加要被读取文件

脚本

#!/bin/bash

file=$1

if [[ $# -lt 1 ]];then
        echo "This file does not exist"
        exit
fi

while read -r f1 f2 f3
do
        echo "file 1:$f1 ==> file 2:$f2 ==> file 3:$f3"

done < "$file"

[工具] Shell 读取一个文件 (以行的方式)

介绍

作者:朱明宇
名称:读取 1 个文件(以行的方式)
作用:读取 1 个文件(以行的方式),并把里面的内容显示出来

使用方法:
1. 给此脚本添加执行权限
2. 执行此脚本,并在输入此命令时,在后面添加要被读取文件

脚本

#!/bin/bash

file=$1

if [[ $# -lt 1 ]];then
        echo "This file does not exist"
        exit
fi

while read line
do
        echo "$line"

done < "$file"

[内容] Python splite 字符串切割

内容一:splite 的格式

<character string>.splite('<separator>', <split several times>)[<which number of word place>]

内容二:splite 的使用案例

2.1 案例一:以一行行的方式进行分割

# string = string.splite('\n')

(补充:这里以给 string 字符串以一行行的方式进行分割为例)

2.2 案例二:制作表格

# string = string.splite('\t')

(补充:这里以给 string 字符串制成表格为例)

2.3 案例三:以空格的方式进行分割

# string = string.splite()

或者:

# string = string.splite(, -1)

(补充:这里以给 string 字符串以空格的方式进行分割为例)

2.4 案例四:以空格的方式进行分割,并截取第 1 个单词

# string = string.splite()[0]

(补充:这里以给 string 字符串以空格的方式进行分割,并截取第 1 个单词为例)

2.5 案例五:以空格的方式进行分割,并截取最后 1 个单词

# string = string.splite()[-1]

(补充:这里以给 string 字符串以空格的方式进行分割,并截取最后 1 个单词为例)

2.6 案例六:以字母进行分割

# splite = re.split(r"[^A-Za-z]", line.strip())

(补充:这里以给 string 字符串以字母的方式进行分割为例)

(注意:要先导入 re 函数)

2.7 案例七:以冒号 “:” 进行分割

# splite = string.splite(':')

(补充:这里以给 string 字符串以冒号 “:” 的方式进行分割为例)

2.8 案例八:以冒号 “:” 进行分割,只分割 1 次,并从开头开始计数

# splite = string.splite(':', 1)

(补充:这里以给 string 字符串以冒号 “:” 的方式进行分割,只分割 1 次,并从开头开始计数为例)

2.9 案例九:以冒号 “:” 进行分割,只分割 1 次,并从末尾开始计数

# splite = string.rsplite(':', 1)

(补充:这里以给 string 字符串以冒号 “:” 的方式进行分割,只分割 1 次,并从末尾开始计数为例)

[工具] Python 截取某一个 Linux 命令所有行的第一列

介绍

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 用 python3 命令执行此脚本

脚本分割线里的变量

command=’df -h’ #要执行的 Linux 命令

脚本

import subprocess
  
####################### Separator ########################

command='df -h' #Linux command to execute

####################### Separator ########################

info = subprocess.check_output('%s'%command, shell=True)
info = info.decode().strip()

lines = info.split('\n')
for line in lines:

    word = line.split()[0]
    print(word)