一些sed命令

最近用了几个命令,记录一下

sed -i "s/\(.*\).sh/\1,\0/g" filename //替换,-i 表示直接修改文件
sed -i '1i#key,value,description' filename  //在文件首行插入
awk '{match($0,/href="([^"]+)"[^\>]+>([^\<]+)/,a);print a[1]"\t"a[2]}'
sed '/baidu/d' baidu_list.txt #删除包含baidu的行

match两种用法:

  1. 普通用法
    match(字符串,正则表达式)
    内置变量RSTART表示匹配开始的位置,RLENGTH表示匹配的长度
    如果匹配到了,返回匹配到的开始位置,否则返回0
    $ awk 'BEGIN{start=match("Abc Ef Kig",/ [A-Z][a-z]+ /);print RSTART,RLENGTH}'
    4 4

  2. 建立数组(If array a is provided, a is cleared and then elements 1 through n are filled with the portions of s that match the corresponding
    parenthesized subexpression in r. The 0'th element of a contains the portion of s matched by the entire regular
    expression r. Subscripts a[n, "start"], and a[n, "length"] provide the starting index in the string and length
    respectively, of each matching substring.)

echo "foooobazbarrrrr | gawk '{ match($0, /(fo+).+(bar*)/, arr) #匹配到的部分自动赋值到arr中,下标从1开始
print arr[1], arr[2]
print arr[1, "start"], arr[1, "length"] #二维数组arr[index,"start"]值=RSTART
print arr[2, "start"], arr[2, "length"] #二维数组arr[index,"length"]值=RLENGTH}'

foooo barrrrr
1 5
9 7

在vim中插入当前打开文件名

Ctrl+r, 光标会变为 “
然后键入%
即可插入当前文件的名字

你可能感兴趣的:(一些sed命令)