Linux

awk -F”[[]]”和awk -F”[][]“分割出的串为什么不一样?

0

按理说 -F”[]“,[]表示匹配括号中的任何一个字符,[]中间的字符应该是和顺序无关的,可是我实验的结果却不一样,这是为什么呢?

 

echo “1[2]3[4]” | awk -F”[\\\\[\\\\]]” ‘{print $1,$2,$3,$4}’

 

有时候用6个^做分隔符的时候,就需要采用awk -F”[\\\\^\\\\^\\\\^\\\\^\\\\^\\\\^]” ‘{print $1}’

while循环中碰到ssh的话,如何避免退出 zt

0

先看一个例子:

#!/bin/sh
. /root/.bash_profile

cat /home/yejr/alldb|while read LINE
do
   #取得IP和组号
   IP=`echo $LINE | awk '{print $1}'`
   NU=`echo $LINE | awk '{print $2}' | awk -F '-' '{print $1}'`

   cnt=`ssh root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"`

   echo "$IP $NU $cnt"
done

看起来没有问题吧,实际上,执行的时候只循环了一次,就退出while循环了,为什么呢?

这是因为ssh需要从输入终端来读取数据,在第一次循环时ssh就把 read 读到的数据也给读取了,相当于是被他”吃”了.
解决办法是,指定 ssh 的输入终端,有3种方法:

ssh -f
-f Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for
   passwords or passphrases, but the user wants it in the background.  This implies -n.  The recommended way to
   start X11 programs at a remote site is with something like ssh -f host xterm.

或者:

ssh -n
-n Redirects stdin from /dev/null (actually, prevents reading from stdin).  This must be used when ssh is run in
   the background.  A common trick is to use this to run X11 programs on a remote machine.  For example, ssh -n
   shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be automati-
   cally forwarded over an encrypted channel.  The ssh program will be put in the background.  (This does not
   work if ssh needs to ask for a password or passphrase; see also the -f option.)

或者,将ssh放到后台中执行. 以下是几种写法的综合:

#!/bin/sh
. /root/.bash_profile

cat /home/yejr/alldb|while read LINE
do
   #取得IP和组号
   IP=`echo $LINE | awk '{print $1}'`
   NU=`echo $LINE | awk '{print $2}' | awk -F '-' '{print $1}'`

   #-f
   #cnt=`ssh -f root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"`

   #-n
   #cnt=`ssh -n root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"`

   #放后台
   #cnt=`ssh root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1" &`

   #指定输入设备
   cnt=`ssh root@$IP "mysql -e 'select count(*) from yejr.tbl1'|tail -n 1"

不知道是否还有其他方法呢?

按格式批量修改文件名

0

按固定格式批量修改文件名,如:

XXX-001.txt

XXX-002.txt

XXX-003.txt

 

首先进入文件目录,然后利用语句;如果不是在文件目录下,就得改下命令,在mv时,新旧文件都能有对应的绝对路径。

 

for $file (`ls x*`){chomp $file;$new=sprintf “XXX-%03d.txt”,++$i;system “mv”,”$file”,”$new”}

加速 MySQL 导入导出的方法

0

MySQL导出的SQL语句在导入时有可能会非常非常慢,在处理百万级数据的时候,可能导入要花几小时。在导出时合理使用几个参数,可以大大加快导 入的速度。

-e 使用包括几个VALUES列表的多行INSERT语法;
–max_allowed_packet=XXX 客户端/服务器之间通信的缓存区的最大大小;
–net_buffer_length=XXX TCP/IP和套接字通信缓冲区大小,创建长度达net_buffer_length的行。

注意:max_allowed_packet 和 net_buffer_length 不能比目标数据库的设定数值 大,否则可能出错。

首先确定目标数据库的参数值

mysql> show variables like ‘max_allowed_packet’;
mysql> show variables like ‘net_buffer_length’;
根据参数值书写 mysqldump 命令,如:

# mysqldump -uroot -p123 21andy -e –max_allowed_packet=16777216 –net_buffer_length=16384 > 21andy.sql
OK,现在速度就很快了,主要注意的是导入和导出端的 max_allowed_packet 和 net_buffer_length 这2个参数值设定,弄大点就OK了

其实,最快的方法,是直接COPY数据库目录,不过记得先停止 MySQL 服务。

获得Shell脚本所在目录的绝对路径

0

要得到正在执行的程序/脚本自身所存放的绝对路径,在 PHP 里面可以用 dirname(realpath(__FILE__)) ; C# 则有 System.Windows.Forms.Application.StartupPath ; java 似乎没有什么比较直接的方法,只能利用 CodeSource 来间接获取 。而在 linux shell 脚本里面如果想得到当前脚本文件存放的绝对路径,也没有太现成的命令可以调用,不过可以通过下面的语句来获取:

baseDirForScriptSelf=$(cd “$(dirname “$0″)”; pwd)
echo “full path to currently executed script is : ${baseDirForScriptSelf}”

虽说大部分情况下我们并不需要这样的绝对路径来完成工作;但如果要把多个脚本、数据文件等内容打包作为一个整体来交付别人使用,又希望不论用户拷贝到哪个目录下执行脚本都能够正确的找到这个包里面的其他内容的话,用这样的脚本来自动定位包的根目录应该是个比较鲁棒的做法。

make -j 的并行任务个数选择

0

在多CPU上编译Linux内核时可以用 make -jn 多个任务并行编译加快速度。印象中在某个文档看到过 n 选择为 ncpu + 1,但不清楚理论依据何在。查了一下也没看到这个说法的原始来源,因此在一个四核的CPU上做了一下简单的测试,结论是在 n 等于 CPU 个数时的速度最快。下面是结果数据:

n system user real
1 52.477 662.534 693.391
2 48.227 606.096 318.699
3 40.066 500.220 184.886
4 38.619 474.832 140.242
5 38.360 479.650 141.821
6 37.721 480.068 141.241
7 38.188 481.979 141.872
8 38.685 483.338 142.171
用到的脚本如下(为降低其他因素的干扰,所有的文件及编译过程都放在内存之中进行):
#!/bin/sh
tar -C /dev/shm -xjf linux-2.6.34.tar.bz2 

mount -o remount,exec /dev/shm

pushd /dev/shm/linux-2.6.34

for i in `seq 1 8`; do
[ -d ../${i} ] && rm -fr ../${i}/* || mkdir ../${i}
cp /boot/config ../${i}/.config
time make O=../${i} -j ${i} oldconfig > /dev/null
time make O=../${i} -j ${i} > /dev/null
rm -fr ../${i}
done

popd

mount -o remount,noexec /dev/shm

CentOS yum 更新 网易源(mirrors.163.com)

0

如果你安装了CentOS系统,并且yum更新慢的话,可以考虑将更新源更换为 网易提供的源:mirrors.163.com

具体操作如下:

cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.bak

vi CentOS-Base.repo

加入如下内容:

CentOS-Base.repo
# CentOS-Base.repo
#
# This file uses a new mirrorlist system developed by Lance Davis for CentOS.
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.    You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever – Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
#released updates
[updates]
name=CentOS-$releasever – Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever – Addons
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons
#baseurl=http://mirror.centos.org/centos/$releasever/addons/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
#additional packages that may be useful
[extras]
name=CentOS-$releasever – Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever – Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

安装mysql,在./configure时出现错误:error: No curses/termcap library found的解决办法

0

在./configure后,make时出现以下错误:
make: *** No targets specified and no makefile found. stop.

在网上找到相关资料,确认是./configure出了问题,于是回头查看,果然发现问题:
最后几行出了错,完整错误信息如下:
checking for tgetent in -lncurses… no
checking for tgetent in -lcurses… no
checking for tgetent in -ltermcap… no
checking for tgetent in -ltinfo… no
checking for termcap functions library… configure: error: No curses/termcap library found

原因:
缺少ncurses安装包

解决办法:
下载安装相应软件包
一、如果你的系统是RedHat系列:
yum list|grep ncurses
yum -y install ncurses-devel
yum install ncurses-devel

二、如果你的系统是Ubuntu或Debian:
apt-cache search ncurses
apt-get install libncurses5-dev

待安装completed!之后,再./configure,顺利通过,然后make && make install,成功安装,一切OK!~~~

对于IFS的一点总结

0

(1)什么是IFS ?
IFS是bash的内部变量,称为内部域分隔符.这个变量用来决定Bash在解释字符串时如何识别域,或者单词边界.

(2)如何查看当前的IFS值?
[root@Dong tmp]# echo "$IFS"

由于IFS默认为空白(空格,tab和新行),所以使用以上的命令似乎看不到字符。没关系,你可以用od命令看16进制,或是2进制值:
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004

注意:$*使用$IFS 中的第一个字符,比如:
[root@Dong tmp]# set w x y z;echo "$*"
w x y z

(3)如何修改IFS值?
普通的赋值命令即可:
[root@Dong tmp]# IFS=":"
[root@Dong tmp]# echo "$IFS"
:
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 3a 0a
0000002
[root@Dong tmp]# set w x y z;echo "$*"
w:x:y:z

(4)实验:$*使用$IFS 中的第一个字符
[root@Dong tmp]# IFS="\\:;"
[root@Dong tmp]# echo "$IFS"
\:;
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 5c 3a 3b 0a
0000004
[root@Dong tmp]# set w x y z;echo "$*"
w\x\y\z
[root@Dong tmp]# IFS=":;\\"
[root@Dong tmp]# echo "$IFS"
:;\
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 3a 3b 5c 0a
0000004
[root@Dong tmp]# set w x y z;echo "$*"
w:x:y:z

(5)备份IFS
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004
[root@Dong tmp]# OLDIFS="$IFS"
[root@Dong tmp]# echo "$OLDIFS" | od -t x1
0000000 20 09 0a 0a
0000004
[root@Dong tmp]# IFS=":"
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 3a 0a
0000002
[root@Dong tmp]# IFS="$OLDIFS"
[root@Dong tmp]# echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004

各种工具之正则表达式语法比较[zt]

0

在各种常用的工具中,
正则表达式如此的相似却又不同。
下表列出了一些常用的正则表达式,以及其不同之处。
项目总多,遗漏必有不少,请各位看官不吝指出。
以perl的正则为基准,不同的用法以粉红色标出。

grep 2.5.1 egrep 2.5.1 sed 3.02
sed 4.07
awk 3.1.1 perl 5.8.0 vim 6.1 JavaScript ??
转义
行头 ^ ^ ^ ^ ^ ^ ^
行尾 $ $ $ $ $ $ $
n个 {n} {m,n} {m,} {,n} {n} {n} {n} {n}或{n} 仅定义 –posix 或 –re-interval有效(要表达}和{,得用{和} 没有定义–posix或–re-interval时,不能用{n}的语法, }{和}{同义 {n} {n} {n}
{0,} * * * *或*, (要表达*,得用*) * * *
{1,} + + + +或+, (要表达+, 得用+) + + +
{0,1} ? ? ? ?或?, (要表达?, 得用?) ? ? ?
任意字符 . . . . 含n. . /s修饰后则含n . 除n . 除n
(pat) 匹配并获结果 (pat) (pat) (pat) (pat)或(pat) (要表达括号,用( ) ) (pat) (pat) (pat)
(?:pat) 匹配但不获结果 不支持 不支持 不支持 不支持 (?:pat) 不支持 (?:pat)
(?=pat) 等于预查 不支持 不支持 不支持 不支持 (?=pat) 不支持 (?=pat)
(?!pat) 不等预查 不支持 不支持 不支持 不支持 (?!pat) 不支持 (?!pat)
| 或 | | | |或| (要表达|,得用|) | | |
其中任意字符 [xyz] [xyz] [xyz] [xyz] [xyz] [xyz] [xyz]
[.ch.] [=ch=] 不支持 不支持 [.ch.] 不支持 不支持 不支持 不支持
单词边界 b b b b 不支持 b 不支持 b
非单词边界 B B B B 不支持 B 不支持 B
单词左右边界 <> < > < > < > 不支持 (><和><和><同义 不支持(><和><同义 < > 不支持(><和><同义
控制字符 /cx 不支持 不支持 cx 不支持 cx 不支持 cx
数字d 不支持 不支持 不支持 不支持 d d d
非数字D 不支持 不支持 不支持 不支持 D D D
换页 f 不支持 不支持 高版本支持 f f 另义 f表示文件名字符 f
换行 n 不支持 不支持 不支持 n n n n
回车 r 不支持 不支持 r r r r r
空白 s 不支持 不支持 不支持 不支持 s s s
非空白 S 不支持 不支持 不支持 不支持 S S S
制表符 t 不支持 不支持 高版本支持 t t t t
垂直制表符 v 不支持 不支持 高版本支持 v v 另义 v表示very magic v
单词字符 w [A-Za-z0-9_] w w w 不支持 w w w
非单词字符 W [^A-Za-z0-9] W W W 不支持 W W W
xn 16进制 不支持 不支持 高版本支持 xn xn 另义 x表示[0-9A-Za-z] xn
n 八进制 不支持 不支持 不支持 n n 不支持 n
n 后向引用 n n n n 仅取结果可用 n n 仅取结果可用 n
[:alnum:] 字母和数字 [:alnum:] [:alnum:] [:alnum:] [:alnum:] [:alnum:] [:alnum:] 不支持
[:alpha:] 字母 [:alpha:] [:alpha:] [:alpha:] [:alpha:] [:alpha:] [:alpha:] 不支持
[:cntrl:] 控制字符 [:cntrl:] [:cntrl:] [:cntrl:] [:cntrl:] [:cntrl:] [:cntrl:] 不支持
[:digit:] 数字 [:digit:] [:digit:] [:digit:] [:digit:] [:digit:] [:digit:] 不支持
[:graph:] 可打印字符(不含空格) [:graph:] [:graph:] [:graph:] [:graph:] [:graph:] [:graph:] 不支持
[:lower:] 小写 [:lower:] [:lower:] [:lower:] [:lower:] [:lower:] [:lower:] 不支持
[:print:] 可打印字符(含空格) [:print:] [:print:] [:print:] [:print:] [:print:] [:print:] 不支持
[:punct:] 标点 [:punct:] [:punct:] [:punct:] [:punct:] [:punct:] [:punct:] 不支持
[:space:] 空格 [:space:] [:space:] [:space:] [:space:] [:space:] [:space:] 不支持
[:upper:] 大写字母 [:upper:] [:upper:] [:upper:] [:upper:] [:upper:] [:upper:] 不支持
[:xdigit:] 16进制数字 [:xdigit:] [:xdigit:] [:xdigit:] [:xdigit:] [:xdigit:] [:xdigit:] 不支持
[:return:] 不支持 不支持 不支持 不支持 不支持 [:return:] 不支持
[:tab:] 不支持 不支持 不支持 不支持 不支持 [:tab:] 不支持
[:escape:] 不支持 不支持 不支持 不支持 不支持 [:escape:] 不支持
[:backspace:] 不支持 不支持 不支持 不支持 不支持 [:backspace:] 不支持
Go to Top