简体   繁体   English

在bash中将一个字符串切成几行

[英]cutting a string into several lines in bash

I want to take the path of the local directory and put each directory on the path in a different line. 我想采取本地目录的路径,并将路径上的每个目录放在不同的行中。 I've tried to do it using cut: 我试过用cut剪切:

pwd | cut -f 1- -d\\/ --output-delimiter=\\n

but it doesn't change the '/'s into EOL, but puts n's instead. 但它并没有将'/'改为EOL,而是将n改为。 What am I doing wrong? 我究竟做错了什么?

This should do the trick 这应该可以解决问题

pwd | tr '/' '\n'

If you don't want an empty line in the beginning (due to the initial / ) you could do 如果您不希望在开头有一个空行(由于初始/ ),您可以这样做

pwd | cut -b2- | tr '/' '\n'

Example: 例:

#aioobe@r60:~/tmp/files$ pwd
/home/aioobe/tmp/files
#aioobe@r60:~/tmp/files$ pwd | cut -b2- | tr '/' '\n'
home
aioobe
tmp
files

你可以试试:

pwd | tr '/' '\n'

这就是你将如何完成你要做的事情(使用ANSI-C引用):

pwd | cut -f 1- -d\/ --output-delimiter=$'\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM