簡體   English   中英

如何在不使用 grep、sed awk、perl 的情況下將剪切和粘貼命令用作單行命令?

[英]How to use cut and paste commands as a single line command without using grep,sed awk, perl?

注意:避免命令grep、sed awk、perl

在 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 文件中,當我 cat filenew 時,結果應如下所示,

harris, charles
johnson, bill
dylan, robert
woodcock, john
wood, barry

請指導我完成這個。 謝謝你。

使用awkcolumn

awk -F'[[:space:]]*|:' '{$2=$2","$3;$3=""}' file | column -t

使用cutpaste (和過程替換<(cmd) ):

$ paste -d, <(cut -d: -f2 file | cut -d' ' -f2) <(cut -d: -f2 file | cut -d' ' -f1)
harris,charles
johnson,bill
dylan,robert
woodcock,john
wood,barry

如果進程替換在您的 shell 中不可用(因為它沒有在 POSIX 中定義,但在bashzshksh受支持),您可以使用命名管道,或者更簡單,將中間結果保存到文件( first持有名字, last持有僅姓氏):

$ cut -d: -f2 file | cut -d' ' -f1 >first
$ cut -d: -f2 file | cut -d' ' -f2 >last
$ paste -d, last first

如果您還需要在姓氏和名字之間包含一個空格,您可以從三個來源paste (中間一個是空來源,如/dev/null ,或更短的<(:) - 進程替換中的空命令),並重用兩個列表中的分隔符(逗號和空格):

$ paste -d', ' <(cut -d: -f2 file | cut -d' ' -f2) <(:) <(cut -d: -f2 file | cut -d' ' -f1)
harris, charles
johnson, bill
dylan, robert
woodcock, john
wood, barry

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM