rev 的功能很單純:它會針對檔案或輸入內容,逐行把字元順序倒轉。
不過它只是把「每行的字元」倒轉,不是整份檔案的行順序。
常見用途
語法 : rev [file]
rev file.txt
# file.txt
apple
banana
carrot
輸出:
elppa
ananab
torrac
echo "hello" | rev
輸出:
olleh
rev file.txt > reversed.txt
rev
對多位元組(Unicode)支援有限,特殊字元可能顯示怪怪的。tac 的功能是反轉檔案的行順序:
最後一行先輸出,再輸出倒數第二行…直到第一行。
( 小知識:名字是 cat 倒過來 )
語法 : tac [options] [file]
-s '分隔符
':指定分隔符(預設是換行符 \n
)-b
:分隔符附在「前面」而不是「後面」-r
:讓分隔符支援正則表達式tac file.txt
# file.txt
first
second
third
輸出:
third
second
first
printf "one\ntwo\nthree\n" | tac
輸出:
three
two
one
printf "apple banana cherry" | tac -s ' '
輸出:
cherry banana apple
printf "one,two,three" | tac -s ',' # 預設
printf "one,two,three" | tac -s ',' -b # 分隔符在前
three,two,one
b
輸出:,three,two,one
tac file.txt > reversed.txt
# file.txt
line1
line2
line3
# reversed.txt
line3
line2
line1
rev file.txt > new.txt
→ 每一行文字的字元都反轉tac file.txt > new.txt
→ 檔案的行順序被反轉