Bash字符串处理(与Java对照) - 18.格式化字符串

From: http://codingstandards.iteye.com/blog/1198098

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

 

1.  System.out.printf('%b', String str )
2.  System.out.printf('%c', char ch )
3.  System.out.printf('%03d', int i )
4.  System.out.printf('%e', float )
5.  System.out.printf('%03f', float f)
6.  System.out.printf('%.2f', float f )
7.  System.out.printf('{%07.3f}', float f )
8.  System.out.printf('%f', float f )
9.  System.out.printf('%g', float f )
10.  System.out.printf('%h', float f )
11.  System.out.printf('%s', 5)
12.  System.out.printf('%s://%s/%s\n', String str1, String str2, String str3)
13.  System.out.printf('%1 s...', String str )
14.  System.out.printf('%5s', String str)
15.  System.out.printf('%-5s', String str) (2)
16.  System.out.printf( '%-10.10s %s', String word, int length )
17.  System.out.printf('%.5s', String str) (3)
18.  System.out.printf('%s', Date date )
19.  System.out.printf('%tc', Date date ) (lowercase t, lowercase c)
20.  System.out.printf('%tC', Date date ) (lowercase t, uppercase C)
21.  System.out.printf('%tD', Date date )
22.  System.out.printf('%tF', Date date )
23.  System.out.printf('%tr', Date date )
24.  System.out.printf('%tR',Date date )
25.  System.out.printf('%tT', Date date )
26.  System.out.printf('%tz', Date date )
27.  System.out.printf('%Tc', Date date ) (Uppercase T, lowercase c)
28.  System.out.printf('%1x, %1X', 0xCAFE )
29.  System.out.printf( Locale.CHINA, '%tc', Date date )
30.  System.out.printf( Locale.ITALIAN, '%tc', Date date )

 

In Bash

printf

man bash 写道
printf [-v var] format [arguments]
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.
 
man 1 printf 写道
FORMAT controls the output as in C printf. Interpreted sequences are:

转义字符:
\" 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

[zcm@bash #86]$./a.sh

 ITEM NAME   ITEM ID      COLOR       PRICE
===========================================
 Triangle   00000013        red       20.00
 Oval       00204449  dark blue       65.66
 Square     00003145     orange        0.70
[zcm@bash #87]$



你可能感兴趣的:(java,Date,String,bash,character,output)