題目描述

輸入文字檔案內容,並輸出最後20個字元。



原題網址

https://www.hackerrank.com/challenges/text-processing-tail-2

輸入格式

輸入一個文字檔案。

輸出格式

輸出文字檔案的最後20個字元。

範例輸入

New York is a state in the Northeastern and Mid-Atlantic regions of the United States. 
New York is the 27th-most extensive, the third-most populous populated of the 50 United States. 
New York is bordered by New Jersey and Pennsylvania to the south.
About one third of all the battles of the Revolutionary War took place in New York.
Henry Hudson's 1609 voyage marked the beginning of European involvement with the area.

範例輸出

ent with the area.

解題概念

利用while和「read」指令,來一行一行讀取從標準輸入傳進來的檔案內容。每讀取到一行資料,就儲存到變數C之中,再串接到TEST變數之後,以此來讀取完整的檔案內容。接著將TEST變數作為「tail」指令的輸入來輸出結果。「tail」指令可以讀取文字資料,並利用「-c」選項指定來輸出資料最後幾個字元。

參考答案

#!/bin/bash

read C
while read C; do
    TEXT="${TEXT}${C}\n"
done
printf "${TEXT}" | tail -c 20