已知在当前文件夹有个文件名为file
内容如下
3 14
3 23
3 25
3 12
1 16
1 24
1 32
1 25
0 14
0 23
0 25
0 12
要求将以上数字输出下面格式
3 14 1 16 0 14
3 23 1 24 0 23
3 25 1 32 0 25
3 12 1 25 0 12
答案1:
awk '{c=(NR-1)%l;a[c]=a[c]?a[c]"\t"$0:$0}END{for(i=0;i<l;i++)p
3 14 1 16 0 14
3 23 1 24 0 23
3 25 1 32 0 25
3 12 1 25 0 12
答案2:
sed -n '1,4p' file>1;sed -n '5,8p' file>b;sed -n '9,12p' file |paste 1 b -
3 14 1 16 0 14
3 23 1 24 0 23
3 25 1 32 0 25
3 12 1 25 0 12
答案3:
awk 'BEGIN{RS="";FS="\n"}{for(i=1;i<=4;i++) print $i,$(i+4),$(i+4*2)}' file
3 14 1 16 0 14
3 23 1 24 0 23
3 25 1 32 0 25
3 12 1 25 0 12
---------------------linux系统管理知识----------------------
显示/test目录下的所有目录
ls -F | grep '/' /test
lsof `which httpd`
哪个进程在占用/etc/passwd
lsof /etc/passwd
显示出那些文件被以k打头的进程名的进程打开,以bash打头,和以init打头
lsof -c k
lsof -c bash
lsof -c init
显示那些文件被pid为30297的进程打开:
lsof +p 30297
打印出占用httpd可执行文件的进程的进程号
lsof -t `which httpd
--------------- shell 面试题-----------------------------------------
cat gaby
history | cut -c 8- | column -t | sort | uniq -u
统计文件gaby有多少行
[root@localhost ~]# cat gaby
1
2
3
4
5
6
awk 'END{print NR}' gaby
grep -c "" gaby
wc -l gaby | awk '{print $1}'
每隔10秒执行一个脚本 下一个10秒执行另一个脚本(需要执行的脚本房子/gaby目录下)
while read gabylinux
do
cd /gaby
/bin/bash -x $gabylinux
/bin/sleep 10
done << EOF
`ls /gaby/`
EOF
huhu
hehe
haha
gaga
xixi
yaya
下面给出三种方式,欢迎继续添加
awk '{for(i=1;i<=NF;i++)print $i}' test
awk '{i=1;while(i<=NF){print $i;i++}}' test
处理以下文件内容,将域名取出并进行计数排序,如处理:
http://www.baidu.com/index.html
http://www.baidu.com/1.html
http://post.baidu.com/index.html
http://mp3.baidu.com/index1.html
http://www.baidu.com/3.html
http://post.baidu.com/2.html
得到如下结果:
域名的出现的次数 域名
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
可以使用bash/perl/php/c 任意一种
grep -n ^$ file |awk 'BEGIN{FS=":"}{print $1}'
head -3 /etc/fstab
awk 'BEGIN{i=1}''{if(NR<=3){print }{i++}}' /etc/fstab
ps -auxf |sort -nr -k 3 |head -10
------------------------- iptables-------------------------
iptables在生产环境中不常用,一般用于面试时候,常见用法如下。
iptables -t nat -F PREROUTING ##把nat表中的PREROUTING链中的规则清空
iptables -F ##清空所有的链中的规则-F 仅仅是清空链中规则,并不影响 -P 设置的默认规则
iptables -t filter -A INPUT -s 172.16.0.0/16 -p udp --dport 53 -j DROP ##A插入一条规则默认放在最后一条
iptables -t filter -I INPUT -s 172.16.0.0/16 -p udp --dport 53 -j DROP ##I插入一条规则默认放在第一条
iptables -t filter -I INPUT 3 -s 172.16.0.0/16 -p udp --dport 53 -j DROP ##插入一条规则放在第三条
##根据状态防止c/s木马攻击
iptables -A INPUT -s 172.16.0.0/24 -d 172.16.100.1 -p tcp --dport 22 -m state --state NEW,ESTABLSHED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT
##禁止ping
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all ##这样的结果是自己也ping不通别人,如果自己想ping通别人,可以用下面规则
iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
##多端口匹配
iptables -A FORWARD -p tcp -m multiport --destination-port 22,25,80,110 -m state --state NEW,ESTALISHED -j ACCEPT
##防止SYN攻击
iptables -A INPUT -i eth0 -p tcp --dport 80 --syn -m connlimit --connlimit-above 80 -j DROP
iptables -A INPUT -i eth0 -p tcp --dport 443 --syn -m connlimit --connlimit-above 80 -j DROP
##如何将本地80 端口的请求转发到8080 端口
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.2.55:8080
---- 不小心在系统下执行了chmod -x/sbin/chmod 怎么办?
[root@localhost ~]# rpm -qf /bin/chmod
coreutils-5.97-34.el5_8.1
[root@localhost ~]# yum -y install coreutils
具体思想如上,具体操作可以由下面命令(测试过不能100%,rpm能100%实现)实现
--------------------- 一道shell面试题--------------------------------
3
8
21
55
144
#/bin/sh
x=0
y=1
for ((i=1;i<=100;i++))
do
z=$((x+y))
x=$y
y=$z
done
echo $z
下面是自己写的
#bin/bash
s[1]=1
s[2]=2
for n in {3..100};do
let x=$[$n-2]
let y=$[$n-1]
let s[$n]=$((s[$x]+s[$y]))
echo $[s[$n]]
done
--------------------- ##/#-----------------------------------------
linux 下/etc/rc.d/init.d/functions里面的函数daemon在sv脚本中很常见到,其中该函数有一句while [ "$1" != "${1##[-+]}" ]; do 中的##迷惑我好久,这个迷惑在我看netkiller(一个10年工作经验的技术大牛的博客后豁然开朗)
#!/bin/bash
if [ "${1##*.}" = "tar" ]
then
echo This appears to be a tarball.
else
echo At first glance, this does not appear to be a tarball.
fi
$ ./mytar.sh thisfile.tar
This appears to be a tarball.
$ ./mytar.sh thatfile.gz
At first glance, this does not appear to be a tarball.
以上内容来自http://netkiller.sourceforge.net/shell/bash.variable.html#idp221856,上面还有更多用法
---------------------强悍的awk-----------------------------------
今天在论坛上看见这样一个帖子:
我看过第一印象写脚本,但是看完大牛们的恢复,我只想说,awk你等着我。
来自大牛的回复,
awk 'BEGIN{print "name average total"}{a[$1]+=$2;b[$1]++};END{for(i in a)print i"\t"a[i]/b[i]"\t"a[i]}' test|column -t
不会数组的需要了解下数组才能看懂。
----------------------linux信号------------------------------------
linux进程间通讯有两种方式:共享内存和信号中断。所有linux中的信号(都是一些短小的信息,发往另一个进程传递控制信息)对linux系统来说是很重要的,查看linux系统都有哪些信号也有两种方式:man 7 signal 和kill -l
从上面两个命令可以看出linux下信号有64种之多,但是我们常用或者是说需要熟悉的有信号1,2,9,15,18,19,
1) SIGHUP(用于唤醒一个进程,重读配置文件) 2) SIGINT(中断,终止=ctrl+c键) 9) SIGKILL(立即结束无视该进程打开的文件或者操作)15) SIGTERM (终止结束一个进程,等待该进程操作完成)18) SIGCONT(将其唤醒)19) SIGSTOP(让该进程处于停止态)
向一个进程传递信号时使用kill/killall,如kill 123 默认用信号15把进程号为123的kill掉
kill -signal PID 只能向对方的进程的进程号传递信号。
killall -signal 进程名
kill -HUP httpd=kill -1 httpd 意思重读http配置文件相当于service httpd reload
------ ----------------------- mail- ---------------------------------
在linux系统会给用户发送邮件,用mail命令可以查看系统当前用户发的邮件:
表示当然系统上给root用户发送的邮件,在“& ”后面输入相对应的数字可以查看相对应的邮件。在“& ”后面输入“?”号可以查看mail的更多功能,输入q回车后推出命令查看。
----------------------------------------------------------------------
本文出自 “gabylinux” 博客,请务必保留此出处http://gabylinux.blog.51cto.com/1593644/833497