Bash 脚本:将文件指定内容行移动到第一行
以下是一个 Bash 脚本,它将指定的内容行移动到文件的第一行:
#!/bin/bash
# 输入文件名和指定内容行
read -p '请输入文件名: ' filename
read -p '请输入要移动的内容行: ' content
# 将指定行提取到临时文件中
grep '$content' '$filename' > temp.txt
# 删除原文件中的指定行
sed '/$content/d' '$filename' > temp2.txt
# 将提取的指定行插入到文件第一行
cat temp.txt temp2.txt > '$filename'
# 删除临时文件
rm temp.txt temp2.txt
echo '成功将内容行移动到第一行!'
使用方法:
- 将上述脚本保存为一个文件,例如
move_line_to_top.sh。 - 执行
chmod +x move_line_to_top.sh,将脚本设置为可执行。 - 执行
./move_line_to_top.sh,按照提示输入文件名和指定内容行即可。
原文地址: https://www.cveoy.top/t/topic/oOvF 著作权归作者所有。请勿转载和采集!