- 浏览: 4733213 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
Bash字符串处理(与Java对照) - 18.格式化字符串
In Java
class Formatter
参见:http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax
String.format
static String format(String format, Object... args)
使用指定的格式字符串和参数返回一个格式化字符串。
参见:String.format函数使用方法介绍 http://blog.csdn.net/andycpp/article/details/1749700
System.out.printf
参见:http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm
In Bash
printf
Write the formatted arguments to the standard output under the control of the format. The format is a
character string which contains three types of objects: plain characters, which are simply copied to
standard output, character escape sequences, which are converted and copied to the standard output, and
format specifications, each of which causes printing of the next successive argument. In addition to
the standard printf(1) formats, %b causes printf to expand backslash escape sequences in the correspond-
ing argument (except that \c terminates output, backslashes in \', \", and \? are not removed, and octal
escapes beginning with \0 may contain up to four digits), and %q causes printf to output the correspond-
ing argument in a format that can be reused as shell input.
The -v option causes the output to be assigned to the variable var rather than being printed to the
standard output.
The format is reused as necessary to consume all of the arguments. If the format requires more argu-
ments than are supplied, the extra format specifications behave as if a zero value or null string, as
appropriate, had been supplied. The return value is zero on success, non-zero on failure.
转义字符:
\" double quote
\NNN character with octal value NNN (1 to 3 digits)
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\xHH byte with hexadecimal value HH (1 to 2 digits)
\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)
\UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits)
%% a single %
%b ARGUMENT as a string with ‘\’ escapes interpreted,
except that octal escapes are of the form \0 or \0NNN
and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs converted to proper type
first. Variable widths are handled.
如果你想对printf命令更深入的了解,参见 http://wiki.bash-hackers.org/commands/builtin/printf
打印换行
示例来自 http://ss64.com/bash/printf.html
# Use \n to start a new line
$ printf "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]# printf "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]# echo "Two separate\nlines\n"
Two separate\nlines\n
[root@jfht ~]# echo -e "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]# echo -n -e "Two separate\nlines\n"
Two separate
lines
[root@jfht ~]#
用0填充(Zero Padding)
技巧来自 Zero Padding in Bash http://jonathanwagner.net/2007/04/zero-padding-in-bash/
创建从1到31为名的目录
for ((x=1;x<=31;x+=1)); do mkdir $x; done
一位数字前面加0
for ((x=1;x< =31;x+=1)); do mkdir `printf "%02d" $x`; done
例子来自 http://ss64.com/bash/printf.html
# Echo a list of numbers from 1 to 100, adding 3 digits of Zero padding
# so they appear as 001, 002, 003 etc:
$ for ((num=1;num<=100;num+=1)); do echo `printf "%03d" $num`; done
设置打印宽度、用空白填充
示例来自 http://linuxconfig.org/bash-printf-syntax-basics-with-examples
#/bin/bash divider=============================== divider=$divider$divider header="\n %-10s %8s %10s %11s\n" format=" %-10s %08d %10s %11.2f\n" width=43 printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE" printf "%$width.${width}s\n" "$divider" printf "$format" \ Triangle 13 red 20 \ Oval 204449 "dark blue" 65.656 \ Square 3145 orange .7
[root@jfht ~]# ./table.sh
Triangle 00000013 red 20.00
Oval 00204449 dark blue 65.66
Square 00003145 orange 0.70
[root@jfht ~]#
本文链接:http://codingstandards.iteye.com/blog/1198098 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
下节内容:Bash字符串处理(与Java对照) - 19.查找字符的位置
发表评论
-
Bash字符串处理(与Java对照) - 22.判断字符串是否数字串
2011-10-25 09:25 5423Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11005Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6734Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 19.查找字符的位置
2011-10-18 09:06 5900Bash字符串处理(与Java对照) - 19.查找字符的位置 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 6929Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8300Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3367Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
2011-09-27 13:22 8268Bash字符串处理(与Java对照) - 14.判断是否包含另 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5116Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6314Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5646Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6842Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3692Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5592Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 3893Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7155Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-13 09:28 3883Bash字符串处理(与Java对照) - 5.字符串输入(读取 ... -
Bash字符串处理(与Java对照) - 4.字符串输出
2011-09-08 09:30 3697Bash字符串处理(与Java对照) - 4.字符串输出 I ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6836Bash字符串处理(与Java ... -
Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)
2011-09-06 09:18 6079Bash字符串处理(与Java ...
相关推荐
9.2. 操作字符串 9.3. 参数替换 9.4. 指定类型的变量:declare 或者typeset 9.5. 变量的间接引用 9.6. $RANDOM: 产生随机整数 9.7. 双圆括号结构 10. 循环和分支 10.1. 循环 10.2. 嵌套循环 10.3. 循环控制 10.4. ...
使用模式匹配来分析比较特殊的字符串 9-20. 对字符串的前缀或后缀使用匹配模式 9-21. 使用declare来指定变量的类型 9-22. 间接引用 9-23. 传递一个间接引用给awk 9-24. 产生随机数 9-25. 从一副扑克牌中取出一张...
4.查找字符串 5.显示文件头部 6.显示文件尾部 7.忽略文件中的重复行 8.比较两个文件 9.按顺序显示文件内容 三、进程间通信命令----------------------------------------------------------------------------...
有关诸如对字符串执行shell扩展之类的高级操作,请参见。 shfmt GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt shfmt格式化外壳程序。 请参阅以快速了解其默认样式。 例如: shfmt -l -wscript.sh 有关更多...
有关诸如对字符串执行shell扩展之类的高级操作,请参见shell示例。 shfmt GO111MODULE =运行时获取mvdan.cc/sh/v3/cmd/shfmt shfmt格式的shell程序。 它可以使用制表符或任意数量的空格缩进。 请参阅canonical.sh以...
1.1. 初始化字符串 1.2. 取字符串的长度 1.3. 拷贝字符串 1.4. 连接字符串 1.5. 比较字符串 1.6. 搜索字符串 1.7. 分割字符串 2. 标准I/O库函数 2.1. 文件的基本概念 2.2. fopen/fclose 2.3. stdin/stdout/stderr ...
/字符串 从上往下查找匹配的字符串; ?字符串 从下往上查找匹配的字符串; n 继续查找。 四、退出命令 exit 退出; DOS内部命令 用于退出当前的命令处理器(COMMAND.COM) 恢复前一个命令处理器。 Ctrl+d 跟exit...
last login:Tue ,Nov 18 10:00:55 on vc/1 上面显示的是登录星期、月、日、时间和使用的虚拟控制台。 4.应用技巧 Linux 是一个真正的多用户操作系统,可以同时接受多个用户登录,还允许一个用户进行多次登录。这...
start 程序名或命令 /max 或/min 新开一个新窗口并最大化(最小化)运行某程序或命令 mem 查看cpu使用情况 attrib 文件名(目录名) 查看某文件(目录)的属性 attrib 文件名 -A -R -S -H 或 +A +R +S +H 去掉...
输出格式化程序 注释/前缀命令输出。 仅适用于安装了 bash 的计算机。 安装 在 Mac 上,您可以执行以下操作: brew install ...
一个实验性的Oberon-7模块,提供INTEGER和REAL值的格式功能。 它受KarlLandström的OBNC oberon编译器及其扩展库的启发。 安装 先决条件 该存储库假定您已安装的编译器v0.16.1或更高版本。 假定您已配置了有效的C...
Chapter 6 变量与字符串操作 Chapter 7 高级变量 Chapter 8 算术运算 Chapter 9 流程控制 Chapter 10 函数 Chapter 11 转向 Chapter 12 trap——陷阱触发 Chapter 14 进程管理和工作控制 Chapter 15 历史...
useradd -c "Name Surname " -g admin -d /home/user1 -s /bin/bash user1 创建一个属于 "admin" 用户组的用户 useradd user1 创建一个新用户 userdel -r user1 删除一个用户 ( '-r' 排除主目录) usermod -c ...
Chapter 6 变量与字符串操作 Chapter 7 高级变量 Chapter 8 算术运算 Chapter 9 流程控制 Chapter 10 函数 Chapter 11 转向 Chapter 12 trap——陷阱触发 Chapter 14 进程管理和工作控制 Chapter 15 历史...
本书全面系统地介绍了Red Hat Linux 6。全书共分为五个部分,包括35章和四个...14.9.18 load printers=(G) 257 14.9.19 null passwords=(G) 257 14.9.20 password level和username level(G) 257 14.9.21 security=(G...
(awk)挂接到解析器并输出事件实用回调(可选) 捕获无效的JSON输入以进行进一步处理选择MIT或Apache 2许可证兼容JSON.sh(自2013-03-13起)默认输出格式非功能转换输入值,例如字符串/数字归一化与Awk实现的兼容性...
脚本去除了不合规则的数据行,并且将时间字符串转换为了时间戳格式。 使用方法 ```bash python3 csv2libsvm.py input_file output_file 79 其中79是label的列号(第80列) ``` analyse_dataset.py 用于分析、...
id一般要求4个字符以内,对于getty或其他login程序项,要求id与tty的编号相同,否则getty程序将不能正常工作。 runlevel 是init所处于的运行级别的标识,一般使用0-6以及S或s。0、1、6运行级别被系统保留,0作为...