如何使用剪切和粘贴命令作为单行命令而不使用grep,sed awk,perl?
问题描述:
注意:避免命令的grep,sed的awk的,perl的如何使用剪切和粘贴命令作为单行命令而不使用grep,sed awk,perl?
在Unix中,我试图写剪切和粘贴命令序列(保存在一个文件中的每个命令的结果),该文件中的反转每一个名字(下面)候选名单并在姓氏后面加上昏迷(例如,比尔约翰逊成为约翰逊,比尔)。
这里是我的文件名单:
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
和column
:
awk -F'[[:space:]]*|:' '{$2=$2","$3;$3=""}' file | column -t
答
随着cut
和paste
仅(和process substitution <(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中没有定义但在bash
,zsh
和ksh
支持),您使用命名管道,或更容易,保存中间结果的文件(first
持有名字,last
控股姓氏只):
$ cut -d: -f2 file | cut -d' ' -f1 >first
$ cut -d: -f2 file | cut -d' ' -f2 >last
$ paste -d, last first
如果您需要还有包括最后一个名字和一个名字之间的空格,您可以从三个来源(中间一个为空来源,如/dev/null
或更短的<(:)
- 空过程替换中的命令)paste
,并重复使用两个列表中的分隔符(逗号和空格):
$ 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
以下答案有什么好运气? – randomir