我有这个输入:
XX3083136|bla-bla texthere texttt|[email protected]|SADFdsafsafd|ASDfasfdsafd|DSFSAFD|dsafasfd|sadfsad|
XX3083372|bla-bla-bla this is a text bla-bla|[email protected]|SDFsafas|SADFsa|DFSsdf|asdasdf|sadfdsafsaf|asdfsadf|
我需要这个输出:
XX3083136|bla-bla texthere textt|[email protected] |SADFdsafsafd|ASDfasfdsafd|DSFSAFD|dsafasfd|sadfsad|
XX3083372|bla-bla-bla this is a te|[email protected] |SDFsafas|SADFsa|DFSsdf|asdasdf|sadfdsafsaf|asdfsadf|
所以区别在于我需要控制“|”之间的最大文本长度。如果它比给定的行长度短,则需要输入“”。在此示例中,第二行限制为最多 24 个字符,第三行必须至少为 30 个字符。不同的行需要不同的最大/最小限制。
我怎样才能在 bash 中做到这一点?
答案1
稍微补充一下:
printf
文本的基本格式是:
%s # Print as is
%10 # Right justify minimum width print 10
%-10 # Left justify minimum width print 10
%.10 # Max width 10
%10.10 # Max width 10, min width print 10
%-10.10 # Left justify, max width 10, min width print 10
%*s # Same as above, but get number from arguments
%-*s # Same as above, but get number from arguments
...
当一个人得到很长的模式时,跟踪位置和哪些等可能会变得有点混乱。一种让它变得更容易的方法可能是这样的:
#!/bin/bash
usage()
{
printf "Usage: %s <FILE>\n" "$(basename "$0")" >&2
[[ -n "$1" ]] && echo "$1"
exit 1
}
if [[ ! -t 0 ]]; then
: # Piped to
elif [[ $# -eq 0 ]]; then
usage "Missing file."
elif [[ ! -r "$1" ]]; then
usage "Unable to read \`$1'."
else
# Accept input from file to file descriptor 0, aka stdin
exec 0< "$1"
fi
fmt=""
fmt="$fmt%s|" # F1
fmt="$fmt%-24.24s|" # F2
fmt="$fmt%-30s|" # F3
fmt="$fmt%-10.10s|" # F4
fmt="$fmt%-10.10s|" # F5
fmt="$fmt%-10s|" # F6
fmt="$fmt%-2.2s|" # F7
fmt="$fmt%-2.2s|\n" # F8
# Set IFS to newline and bar and read fields
# assigning them to f1 ... f8
#
while IFS=$'\n'"|" read f1 f2 f3 f4 f5 f6 f7 f8; do
printf "$fmt"\
"$f1" "$f2" "$f3" "$f4" \
"$f5" "$f6" "$f7" "$f8"
done <&0
或者。例如:
while IFS=$'\n'"|" read f1 f2 f3 f4 f5 f6 f7 f8; do
printf "%s|%-*.*s|%-*s\n" \
"$f1" \
24 24 "$f2" \
30 "$f3"
done < "input"
如果只是行解析,那么 awk 是一个不错的选择。例如:
#!/usr/bin/awk -f
BEGIN {FS="|"}
/^/ {
printf "%s|%-24.24s|%-30s|%-10.10s|%-10.10s|%-10s\n",
$1, $2, $3, $4, $5, $6
}
或者:
#!/usr/bin/awk -f
BEGIN {
FS="|"
}
/^/ {
printf "%s|%-*.*s|%-*s|%-*.*s|%-*.*s|%-*s\n",
$1,
24, 24, $2,
30, $3,
10, 10, $4,
10, 10, $5,
10, $6
}
答案2
您可以使用printf
内置函数来剪切字符串:
$ printf '%.2s\n' 'azerty'
az
并用它把分隔符放在|
你想要的地方,例如:
$ printf '%10s%10s%10s%10s%10s%10s\n' '|' '|' '|' '|' '|' '|'
| | | | | |
看http://www.bash-hackers.org/wiki/doku.php/commands/builtin/printf