sh 查找,过滤,搜索,替换和正则表达式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 查找,过滤,搜索,替换和正则表达式相关的知识,希望对你有一定的参考价值。
#! /bin/bash
# SED (Stream Editor; search and replace)
sed 's/oranges/bananas/g' mysampledata.txt # s/ = mode search; /g global (not just first)
...
# Find & replace
sed -i 's/OLD/NEW/g' file.txt
# AWK (can execute certain control sequences)
awk '{print $2}' # prints second argument
# while, return, ...
ls files_list | awk '{print "mv "$1" "$1".new"}' | sh
# EGREP / GREP
grep -v needle haystack.file # inverts query
# DIFF
# Difference between two directories (-q omits diff content)
diff -rq dir1/ dir2/
# FIND
find . -name output.log -exec grep "something" {} \; -print
find . -name '*.java' -and -not -path "*generated*" | xargs wc -l # count all lines
# Find all files that are...
# newer than 3 days...
find . -type f -mtime -3 # -1 less than one day, 1 exact one day, +1 more than one day
# newer than 2016-08-13
find . -type f -newermt 2016-08-13
# between 2016-08-13 and 2016-08-20
find . -type f -newermt 2016-08-13 \! -newermt 2016-08-20
# -type d (directory)
# -empty
# -delete
# -exec rm {} \;
# XARGS - construct argument list(s) and execute utility
find . -type file -print | xargs rm # uses SPACE, TAB, SEMICOLON for delimiter;
# print0 prints without newline
-maxdepth 1 # no subdirs
-size +10000c # size (bytes)
以上是关于sh 查找,过滤,搜索,替换和正则表达式的主要内容,如果未能解决你的问题,请参考以下文章
VSCode 正则表达式查找和替换子匹配数学?
Vim查找替换及正则表达式的使用
正则表达式中的 Eclipse 更改案例查找和替换
在多个文件中查找和替换正则表达式的最佳工具是啥?
Word中使用正则表达式进行查找和替换与难题征解
Bash之正则表达式