Skip to content

Instantly share code, notes, and snippets.

@qingniufly
Created March 10, 2017 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qingniufly/24ddb1ad3932cfccf7e474dfa86648d7 to your computer and use it in GitHub Desktop.
Save qingniufly/24ddb1ad3932cfccf7e474dfa86648d7 to your computer and use it in GitHub Desktop.
shell, sed, awk
# 循环
for i in {1..100}; do echo $i; done
for i in $(seq 1 100); do echo $i; done
for ((i = 0; i < 100; i++)); do echo $i; done
# sed 读取文件 a,b行的内容
sed -n '5,10p' test.txt
# 删除空行
sed '/^$/d' file
# 删除不包含中文的行
sed '/^[a-zA-Z0-9]*$/d' file
# 批量修改文件名后缀
for i in `ls *.job`
do
newfile=`echo $i|sed 's/\.job$/\.job\.bak/'`
mv $i $newfile
done
# xargs -t 先打印,再执行 -i或者-I 将每项内容赋值给{}
ls |xargs -t -I mv {} {}.bak
# 将Linux下的换行符替换为Windows下的回车换行
sed -e 's/$/\r/' linux.txt > dos.txt
# 将Windows下的回车换行替换为Linux的换行
sed -e 's/.$//' dos.txt > linux.txt
sed -e 's/^M//' dos.txt > linux.txt # 其中 ^M输入方式为:先按CTRL+v,接着按CTRL+SHIFT+m
# Mac -> UNIX:
tr "\r" "\n" <macfile >unixfile
# UNIX -> Mac:
tr "\n" "\r" <unixfile >macfile
# Microsoft DOS/Windows 约定,文本的每行以回车字符(\r)并后跟换行符(\n)结束。为了纠正这个问题,可以使用下列命令:
# DOS -> UNIX:
tr -d "\r" <dosfile >unixfile
# UNIX -> DOS:在这种情况下,需要用awk,因为tr不能插入两个字符来替换一个字符。
# 要使用的 awk 命令为
awk '{ print $0"\r" }' <unixfile >dosfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment