How do I run multiple Unix commands? [closed]

无人久伴 提交于 2019-12-13 09:36:02

问题


So I have to run a cut and a sort file at once. I'm using this command:

sort -k2 customers;cut -f7-31 customers

The objective is to cut lines 7-31 then organize them in alphabetical order but I am not sure how to achieve it. Any input will help, thank you.


回答1:


The objective is to cut lines 7-31 ...

You can use sed:

sort -k2 customers | sed '7,31!d'

This would pipe the output of sort to sed which would display lines 7 - 31 (both inclusive) from the sort output.


In order to select a character range, you need to supply the -c option to cut and not -f (which selects fields):

sort -k2 customers | cut -c7-31

which would select characters 7 - 31 in each line from the sort output.


In order to sort the final output, use another pipeline:

sort -k2 customers | cut -c7-31 | sort



回答2:


The key thing here is to use pipes which are one of the singularly most powerful things in the shell. See Unix Pipes

Pipes in unix are wonderful; powerful. Did I already mention this?

To answer your question; change the ; to a | and remove the filename

sort -k2 customers | cut -f7-31 

will run sort on customers file and send the output of that to the input of cut; no temporary files.

Generally you'd do

sort -k2 customers | cut -f7-31 > customers.modified

You can have as many commands in the pipeline as you like; so to remove duplicates you'd to

sort -k2 customers | uniq | cut -f7-31 > customers.modified


来源:https://stackoverflow.com/questions/19487836/how-do-i-run-multiple-unix-commands

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!