博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
seq妙用一则
阅读量:2388 次
发布时间:2019-05-10

本文共 2086 字,大约阅读时间需要 6 分钟。

[haoren@IM-SJ01-Server01 gongsi]$ seq -f '201110%02g' 1 26

20111001

20111002

20111003

20111004

20111005

20111006

20111007

20111008

20111009

20111010

20111011

20111012

20111013

20111014

20111015

20111016

20111017

20111018

20111019

20111020

20111021

20111022

20111023

20111024

20111025

20111026

等同于 {20111001..20111026}

-s 指定分隔符,默认是换行


-w 等位补全,就是宽度相等,不足的前面补 0


-f 格式化输出,就是指定打印的格式


可以不指定起始数值,则默认为 1


另外,不用 seq 的话还可以这样:


 for i in {1..10};do echo $i;done


1 和 10 之间是两个半角的点



从1循环到100的两种方法



for x in `seq 1 100`;do echo $x;done



for x in {1..100};do echo $x;done



输出1-100中,不包含数字7,且不能被7整除的数



seq 100 | grep -v "7" | awk '$0%7!=0{print}'



seq -f"%3g" 9 11

9

10

11



% 后面指定数字的位数 默认是"%g", 



"%3g"那么数字位数不足部分是空格



sed -f"%03g" 9 11 这样的话数字位数不足部分是0


% 前面制定字符串


seq -f "str%03g" 9 11


str009

str010

str011



-w 指定输出数字同宽 不能和-f一起用 



输出是同宽的-s 指定分隔符 默认是回车



seq -s" " -f"str%03g" 9 11


str009 str010 str011


要指定\t 做为分隔符号


seq -s"echo -e "\t"" 9 11指定\n\n作为分隔符号



seq -s " " 1 10


1 2 3 4 5 6 7 8 9 10



seq -s ", " 1 10


1, 2, 3, 4, 5, 6, 7, 8, 9, 10



请用linux shell 写一段脚本,实现从1..1000中所有偶数的和值   

通过while 循环得到需要的结果:

​start=1;
 
total=0;
 
while [ $start -le 1000 ];do
 
    [[ $(($start%2)) == 0 ]]&&total=$(($total+$start));
 
   start=$(($start+1));
 
done;
 
echo $total;


​[chengmo@centos5 ~]$ start=1;total=0;while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1));done;echo $total;

250500
​​
通过 for 循环得到结果:

​​
​start=0;
 
total=0;
 
for i in $(seq $start 2 1000); do
 
    total=$(($total+$i));
 
done;
 
echo $total;


[chengmo@centos5 ~]$ start=0;total=0;for i in $(seq $start 2 1000); do total=$(($total+$i));done;echo $total; 

250500
​​​​

比较性能:


[chengmo@centos5 ~]$ time (start=0;total=0;for i in $(seq $start 2 1000); do total=$(($total+$i));done;echo $total;) 250500

real 0m0.016s

user 0m0.012s

sys 0m0.003s



[chengmo@centos5 ~]$ time (start=1;total=0;while [ $start -le 1000 ];do [[ $(($start%2)) == 0 ]]&&total=$(($total+$start)); start=$(($start+1));done;echo $total;) 

250500

real 0m0.073s

user 0m0.069s

sys 0m0.004s

for i in ` seq -f '1704%02g' 1 17` ;do echo -n $i "  " ;mysql -D JSDB   -e "select count(*) from JIESUANTJ_$i;"|awk 'NR>1' ;done 

转载地址:http://rcpab.baihongyu.com/

你可能感兴趣的文章
ceph集群的扩展(centos7环境)
查看>>
linux命令之top(查看cpu、内存等负载)
查看>>
linux_详解find命令
查看>>
openstack的swift组件详解
查看>>
两大主流开源分布式存储的对比:GlusterFS vs. Ceph
查看>>
面试笔试动态规划问题--python篇
查看>>
linux下的svn常用命令使用指南
查看>>
阿里云iot事业部一面面经
查看>>
《云计算架构技术与实践》
查看>>
《云计算架构技术与实践》序言(李德毅院士)
查看>>
《云计算架构技术与实践》连载(2):1.2 云计算的发展趋势
查看>>
《跨界杂谈》企业商业模式(七):其他
查看>>
STL介绍 - map
查看>>
ssh 命令的用法
查看>>
scp 命令的用法
查看>>
ldcofig 命令的用法
查看>>
tar 命令的用法
查看>>
mount 命令的用法
查看>>
fdisk 命令的用法
查看>>
ln 命令的用法
查看>>