In Unix, I am trying to write a sequence of cut and paste commands (saving result of each command in a file) that inverts every name in the file(below) shortlist and places a coma after the last name(for example, bill johnson becomes johnson, bill).
在Unix中,我尝试编写一个剪切和粘贴命令的序列(保存在一个文件中的每个命令的结果),该命令将文件中的每个名称(下面的)都转化为短列表,并在最后一个名称之后放置一个昏迷(例如,bill johnson成为johnson, bill)。
here is my file shortlist:
以下是我的文件清单:
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
I am able to cut from shortlist but not sure how to paste it on to my filenew file in same command line. Here is my code for cut:
我可以从快捷列表中剪切,但是不知道如何在命令行中将它粘贴到我的ew文件中。以下是我的剪切代码:
cut -d: -f2 shortlist
result:
结果:
charles harris
bill johnson
robert dylan
john woodcock
barry wood
Now I want this to be pasted in my filenew file and when I cat filenew, result should look like below,
现在我想把它粘贴到我的文件名文件中当我文件名ew时,结果应该如下所示,
harris, charles
johnson, bill
dylan, robert
woodcock, john
wood, barry
Please guide me through this. Thank you.
请带我过去。谢谢你!
2 个解决方案
#1
2
You could do it with a single awk:
你可以用一个awk:
awk -F: '{split($2,a, / /); if(a[2]) l=a[2] ", "; print l a[1]}' shortlist
I am assuming that if you don't have a second name, you don't want to print the comma (and you don't have more than 2 words in the name).
我假设如果你没有第二个名字,你不会想要打印逗号(你的名字中没有超过两个词)。
#2
1
Once you've used cut
to split up the string, it may be easier to use awk
than paste
to produce the result you want:
一旦你使用剪切来分割字符串,使用awk比粘贴更容易产生你想要的结果:
$ cut -d":" -f2 shortlist | awk '{printf "%s, %s\n", $2, $1}'
#1
2
You could do it with a single awk:
你可以用一个awk:
awk -F: '{split($2,a, / /); if(a[2]) l=a[2] ", "; print l a[1]}' shortlist
I am assuming that if you don't have a second name, you don't want to print the comma (and you don't have more than 2 words in the name).
我假设如果你没有第二个名字,你不会想要打印逗号(你的名字中没有超过两个词)。
#2
1
Once you've used cut
to split up the string, it may be easier to use awk
than paste
to produce the result you want:
一旦你使用剪切来分割字符串,使用awk比粘贴更容易产生你想要的结果:
$ cut -d":" -f2 shortlist | awk '{printf "%s, %s\n", $2, $1}'