Linux常用命令

Database and Ruby, Python, History


awk

-F '='=分割字符串,$0对应该字符串,$1对应分割后的第一个字符。 '/=/' {print $1} 如果改行有=,打印$1 NR 是内置变量,行号 toupper 是函数,转换为大写,此外还有 length(), tolower(), substr()。完整函数参考1 还可以用if...else

awk -F '=' '/=/ {print toupper(FILENAME) "#" NR ":" $1 $2}' config.gradle
CONFIG.GRADLE#4:    customerName  "aspfizerus" // default value for as-customer
CONFIG.GRADLE#7:    archiveVersion  "1.3.4-SNAPSHOT"
CONFIG.GRADLE#10:    customerObj  null
CONFIG.GRADLE#12:    dependencies  ["aktana-gradle-helper"  : 'com.aktana:aktana-gradle-helper:1.0.101-SNAPSHOT',

拼接字符串

ls src/main/resources/db/migration/V3_cdf_ei_usage/ | awk '{print "src/main/resources/db/migration/V3_cdf_ei_usage/"$1}'

搜索文件,查询内容

awk '/ersion/ {print FILENAME "#" NR ":" $0 }' *.gradle

xargs

逐个调用命令

ls src/main/resources/db/migration/V3_cdf_ei_usage/ | awk '{print "src/main/resources/db/migration/V3_cdf_ei_usage/"$1}' | xargs -I {} misc list {}

echo

截断

# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
thisfile='foobar.txt.bak'
foo="${thisfile%.*}"   # removes shortest part of value in $thisfile matching after '%' from righthand side
bar="${thisfile%%.*}"  # removes longest matching

for item in "$foo" "$bar"; do echo "$item"; done
foobar.txt
foobar

替换

foobar='Simplest, least effective, least powerful'
# ${var/find/replace_with}
foo="${foobar/least/most}"   #single occurrence
bar="${foobar//least/most}"  #global occurrence (all)

for item in "$foobar" "$foo" "$bar"; do echo "$item"; done
Simplest, least effective, least powerful
Simplest, most effective, least powerful
Simplest, most effective, most powerful
alpha=$(ls tasks)
echo ${alpha}
# common
# project
# tasks.gradle
alpha=($(ls tasks))
echo ${alpha}
# common project tasks.gradle
echo "${#alpha}"
# 3
echo "${alpha[1]}"
# common

sed

替换special character。$会escape特殊字符,比如这里的\x01

sed $'s/,/\x01/g' src.csv > src_new.csv

cat

echo -e "\x01" > tmp.txt
cat -v tmp.txt
^A

date

https://ss64.com/bash/date.html

date +%B
# February
date "+%Y/%m/%d %H:%M:%S"
# 2021/02/24 09:12:56

获取昨天,明天

date --date='yesterday'
date --date='1 day ago'
date --date='10 day ago'
date --date='10 week ago'
date --date='10 month ago'
date --date='10 year ago'

On MacOS

date -v -1d

test

https://ss64.com/bash/test.html