![](/img/trans.png)
[英]How to use cut and paste commands as a single line command without using grep,sed awk, perl?
[英]How to use cut and paste commands as a single line command?
在Unix中,我試圖編寫一系列剪切和粘貼命令(將每個命令的結果保存在文件中),以反轉文件(在以下列表中)的每個名稱,並在姓氏后放置一個逗號(例如,Bill johnson)成為約翰遜,比爾)。
這是我的文件清單:
2233:charles harris :g.m. :sales :12/12/52: 90000
9876:bill johnson :director :production:03/12/50:130000
5678:robert dylan :d.g.m. :marketing :04/19/43: 85000
2365:john woodcock :director :personnel :05/11/47:120000
5423:barry wood :chairman :admin :08/30/56:160000
我可以從清單中剪切出來,但是不確定如何在同一命令行中將其粘貼到我的filenew文件中。 這是我剪切的代碼:
cut -d: -f2 shortlist
結果:
charles harris
bill johnson
robert dylan
john woodcock
barry wood
現在,我希望將其粘貼到我的filenew文件中,當我選擇filenew時,結果應如下所示,
harris, charles
johnson, bill
dylan, robert
woodcock, john
wood, barry
請指導我。 謝謝。
您可以用一個awk來做到這一點:
awk -F: '{split($2,a, / /); if(a[2]) l=a[2] ", "; print l a[1]}' shortlist
我假設如果您沒有第二個名字,那么您不想打印逗號(並且名字中的字數不能超過2個)。
使用cut
分割字符串后,使用awk
比paste
來生成所需結果要容易得多:
$ cut -d":" -f2 shortlist | awk '{printf "%s, %s\n", $2, $1}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.