定期放出自己学习shell中的点滴,都是自己写的,不好之处还望海涵
去掉awk中单引号的特殊性
zj@zj:~/Script/cushell/08.10.21$ awk -v hq="'" '{$2=hq$2hq;print $0}' a
John '9'
Bob '9'
John '10'
多个空格替换为一个
zj@zj:~/Script/cushell/08.10.21$ cat a
John 9
Bob 9
John 10
zj@zj:~/Script/cushell/08.10.21$ cat a | tr -s ' '
John 9
Bob 9
John 10
zj@zj:~/Script/cushell/08.10.21$ sed 's/ \{2,\}/ /g' a
John 9
Bob 9
John 10
[[:space:]],\ ,
将多个空格和多个tab都替换为一个
zj@zj:~/Script/cushell/08.10.21$ sed 's/\([ \t]\)\{2,\}/\1/g' a
John 9 tab tab space space
Bob 9 tab tab
John 10 tab tab
如果我有文件,内容如下:
[a
asdd
fdfddfsd
sdfd
[b
fffsd
ggffddf
ggfg
a和b对应的是名字。而我希望输出
[a
asddfdfddfsdsdfd
[b
fffsdggffddfggfg
也就是一个名字下多行合并,请问怎么办?
sed '/\[/{:a;n;:c;N;/\[/ba;s/\n//;$!bc}' urfile
[. sh bash] pro.sh与./pro.sh执行权限是不同的,前面只需要read权限,而./pro.sh需要读和执行权限
read的时候由于默认的IFS为空格,所以多个空格会被忽略.而特殊变量REPLY输出的确实实际的输入.
zj@zj:~/Script/cushell/08.10.22$ read a
hello zj
zj@zj:~/Script/cushell/08.10.22$ echo $a
hello zj
zj@zj:~/Script/cushell/08.10.22$ read a
hello zj
zj@zj:~/Script/cushell/08.10.22$ echo $a
hello zj
zj@zj:~/Script/cushell/08.10.22$ read
hello zj
zj@zj:~/Script/cushell/08.10.22$ echo $REPLY
hello zj
zj@zj:~/Script/cushell/08.10.22$ echo "$REPLY"
hello zj
zj@zj:~/Script/cushell/08.10.22$ IFS=";"
zj@zj:~/Script/cushell/08.10.22$ read a
hello zj
zj@zj:~/Script/cushell/08.10.22$ echo $a
hello zj
tac - 反转显示文件
zj@zj:~/Script/cushell/08.10.22$ cat test
<body>
This is
hello
</body>
zj@zj:~/Script/cushell/08.10.22$ tac test
</body>
hello
This is
<body>
sed ':a;N;$!b a;' 将文本中的所有内容都读到缓冲区
zj@zj:~/Script/cushell/08.10.24$ cat lines
asad
HH
das sadas asdasd
asdasd
//
HH
a
//
zj@zj:~/Script/cushell/08.10.24$ sed '/HH/{n;:a;N;/\/\//b;s/\n/ /;b a}' lines
asad
HH
das sadas asdasd asdasd
//
HH
a
//
zj@zj:~/Script/cushell/08.10.24$ awk '/HH/,/\/\//{i++;if(/\/\//){a=a"\n"$0;gsub(/ /,"",a);print a;a="";i=0}else {if(i==1)a=$0"\n";else a=a""$0};next};1' lines
asad
HH
dassadasasdasdasdasd
//
HH
a
//
|