Reputation: 4511
i want to do something like this:
I have a script :
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"
This code is writing the content of ec2-describe-snapshots
command in a file. The content of the file is
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)
I want to add headings to the file at run time like field names Snapshot, snapshot id,volume id etc with correct formatting. How can i achieve that?
output after @janos answer :
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)
Thanks
Upvotes: 1
Views: 96
Reputation: 124646
Assuming you want to get an output like this:
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
You can use printf
to format your header columns with the right amount of spaces like this:
printf "%-12s%-12s%-12s%-12s%-28s%-8s\n" Label SnapshotID VolID Status Date
For example the formatting placeholder %-12s
means: format as a 12-character wide string, aligned to the left.
You could create your file with such header followed by the list of snapshots by first redirecting the header line with >
and then the list of snapshots by >>
to append to the same file:
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"
But even better to put all the printing in one block and redirect the whole thing in one swift move:
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"
Ok I see the problem. The columns in the output of ec2-describe-snapshots
are separated by a tab. You could print a header where the labels are separated by a tab like this:
printf "Label\tSnapshotID\tVolID\tStatus\tDate\n"
But I'm not sure if that will be good enough for you.
If that's not good enough, then I think you'll have to reformat the output of ec2-describe-snapshots
. Since you are in Linux, the column
command can help you here. Here's an imperfect but easily readable solution:
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"
UPDATE
Although you say the above is not working, I cannot reproduce what you're getting. I'm getting good columnized formatting here, like in the sample at the top of my answer. So I think you're doing something wrong.
Anyway, here's an alternative, uglier solution:
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"
This may not work well with all possible outputs of ec2-describe-snapshots
. For example this last script assumes that the SnapshotID column is always less than 16 characters. If you notice that some columns are printed without a space between them, then adjust the format string in the printf
statement there to increase the width of columns.
Upvotes: 1
Reputation: 912
Yo can do like this with awk with redirecting the output to file. I have put your output of command in a file aa.
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}') > "$EC2_HOME/Working/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)
Upvotes: 0
Reputation: 798686
echo "headers go here" > "$EC2_HOME/Working/SnapshotsLatest_$today_date"
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" >> "$EC2_HOME/Working/SnapshotsLatest_$today_date"
Note the change to the redirection operator.
Upvotes: 0