我想做这样的事情:
我有一个脚本:
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" > "$EC2_HOME/Working/SnapshotsLatest_$today_date"
此代码将ec2-describe-snapshots
命令的内容写入文件中。该文件的内容是
SNAPSHOT snap-1062e vol-aef8 completed 2013-12-12T05:38:28+0000 100% 109030037527 20 2013-12-12: Daily Backup for i-39 (VolID:vol-aef8 InstID:i-3e2)
SNAPSHOT snap-1c422 vol-f66a0 completed 2013-12-12T05:38:27+0000 100% 109030037527 10 2013-12-12: Daily Backup for i-211 (VolID:vol-9a0 InstID:i-211)
我想在运行时向文件添加标题,例如字段名称Snapshot,snapshot id,volume id等,格式正确。我怎样才能实现这一目标?
在@janos回答后输出:
Label SnapshotID VolID Status Date
SNAPSHOT snap-626fee5cvol-aecbbcf8completed 2013-12-13T04:53:18+0000 100% 109030037527 20 2013-12-13: Daily Backup for Jst with i-3d09 (Vol ID:vol-af8 Inst ID:i-3e209)
SNAPSHOT snap-686fee56vol-f66409a0completed 2013-12-13T04:53:16+0000 100% 109030037527 10 2013-12-13: Daily Backup for_Test_Machine with i-26011 (Vol ID:vol-f66a0 Inst ID:i-260111)
由于
答案 0 :(得分:1)
假设你想获得这样的输出:
Label SnapshotID VolID Status Date
SNAPSHOT snap-1062e vol-aef8 completed 2013-12-12T05:38:28+0000 100% 109030037527 20
SNAPSHOT snap-1c422 vol-f66a0 completed 2013-12-12T05:38:27+0000 100% 109030037527 10
您可以使用printf
使用适当数量的空格格式化标题列,如下所示:
printf "%-12s%-12s%-12s%-12s%-28s%-8s\n" Label SnapshotID VolID Status Date
例如,格式化占位符%-12s
表示:格式为12个字符的宽字符串,与左侧对齐。
您可以通过首先使用>
重定向标题行,然后通过>>
将快照列表附加到同一文件来创建包含此类标题后跟快照列表的文件:
out="$EC2_HOME/Working/SnapshotsLatest_$today_date"
printf "%-12s%-12s%-12s%-12s%-28s%-8s\n" Label SnapshotID VolID Status Date > "$out"
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" >> "$out"
但更好的做法是将所有打印放在一个区块中并将整个内容重新定向为一个快速移动:
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
out="$EC2_HOME/Working/SnapshotsLatest_$today_date"
{
printf "%-12s%-12s%-12s%-12s%-28s%-8s\n" Label SnapshotID VolID Status Date
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate"
} > "$out"
好的,我看到了问题。 ec2-describe-snapshots
输出中的列由制表符分隔。您可以打印一个标题,标题由标签分隔,如下所示:
printf "Label\tSnapshotID\tVolID\tStatus\tDate\n"
但我不确定这对你来说是否足够好。
如果这还不够好,那么我认为你必须重新格式化ec2-describe-snapshots
的输出。由于您使用的是Linux,因此column
命令可以帮助您。这是一个不完美但易于阅读的解决方案:
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
out="$EC2_HOME/Working/SnapshotsLatest_$today_date"
{
echo Label SnapshotID VolID Status Date
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate"
} | column -t > "$out"
<强>更新强>
虽然你说上面的内容不起作用,但我无法重现你所得到的内容。我在这里得到了很好的列式格式,比如我答案顶部的示例。所以我认为你做错了什么。
无论如何,这是另一种更丑陋的解决方案:
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
out="$EC2_HOME/Working/SnapshotsLatest_$today_date"
{
echo Label SnapshotID VolID Status Date
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate"
} | awk '{printf "%-12s%-16s%-16s%-12s%-26s%-6s%-14s%-4s", $1, $2, $3, $4, $5, $6, $7, $8; for (i=9; i<=NF; ++i) printf $i " "; print ""; }' > "$out"
这可能不适用于ec2-describe-snapshots
的所有可能输出。例如,最后一个脚本假定SnapshotID列始终少于16个字符。如果您注意到某些列打印时它们之间没有空格,则在那里调整printf
语句中的格式字符串以增加列的宽度。
答案 1 :(得分:0)
echo "headers go here" > "$EC2_HOME/Working/SnapshotsLatest_$today_date"
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" >> "$EC2_HOME/Working/SnapshotsLatest_$today_date"
请注意对重定向运算符的更改。
答案 2 :(得分:0)
latestdate = $(ec2-describe-snapshots | grep ^ SNAPSHOT | sort -rk 5 | awk'{print substr($ 5,1,10); exit}')&gt; “$ EC2_HOME /工作/ SnapshotsLatest_ $ today_date”
> cat aa | grep "^SNAPSHOT.*$latestdate" | awk ' BEGIN { printf "%-12s%-12s%-12s%-12s%-28s%-8s\n", "SNAPSHOT", "SnapShotID", "VolumeID"," etc"," etc"," etc"," etc"} 1
SNAPSHOT SnapShotID VolumeID etc etc etc
SNAPSHOT snap-1062e vol-aef8 completed 2013-12-12T05:38:28+0000 100% 109030037527 20 2013-12-12: Daily Backup for i-39 (VolID:vol-aef8 InstID:i-3e2)
SNAPSHOT snap-1c422 vol-f66a0 completed 2013-12-12T05:38:27+0000 100% 109030037527 10 2013-12-12: Daily Backup for i-211 (VolID:vol-9a0 InstID:i-211)