在GNU / Linux上,我通过运行ls
命令跟随输出:
$ ls -l /dev/disk/by-label/
total 0
lrwxrwxrwx 1 root root 10 Mar 4 18:21 Documents+Edu -> ../../sda4
lrwxrwxrwx 1 root root 10 Mar 4 18:17 Ext4 -> ../../sda3
lrwxrwxrwx 1 root root 10 Mar 4 18:21 Game+Movie -> ../../sda6
lrwxrwxrwx 1 root root 10 Mar 4 18:42 OS -> ../../sda7
lrwxrwxrwx 1 root root 10 Mar 4 18:20 Software+Video -> ../../sda5
我想要以下输出:
sudo mount /dev/sda4 /media/$USER/Documents+Edu
sudo mount /dev/sda3 /media/$USER/Ext4
sudo mount /dev/sda6 /media/$USER/Game+Movie
sudo mount /dev/sda7 /media/$USER/OS
sudo mount /dev/sda5 /media/$USER/Software+Video
换句话说: -
sudo mount /dev/
之前的每个/sdax
和/media/$USER/
之前添加文字Documents+Edu
,如:grep
等。如何使用以下命令在Linux上实现所需的输出:cut
,awk
,sed
,{{1}}等?
答案 0 :(得分:1)
由于您不需要除名称及其符号链接之外的任何内容,因此我最好只需ls -1
(一个而不是L
)来获取最后一列。然后,管道到此awk
:
awk -F"->|/" '{printf "sudo mount /dev/%s /media/$USER/%s\n", $NF, $1}'
它会在->
或/
分隔符中对文本进行切片。基于此,它获取最后一个和第一个并相应地填充字符串。
$ ls -1 /dev/disk/by-label/ | awk -F"->|/" '{printf "sudo mount /dev/%s /media/$USER/%s\n", $NF, $1}'
sudo mount /dev/sda4 /media/$USER/Documents+Edu
sudo mount /dev/sda3 /media/$USER/Ext4
sudo mount /dev/sda6 /media/$USER/Game+Movie
sudo mount /dev/sda7 /media/$USER/OS
sudo mount /dev/sda5 /media/$USER/Software+Video
答案 1 :(得分:1)
另一种选择可能是避免ls
使用readlink
完全迭代文件:
$ cat ../nols.sh
cd /dev/disk/by-label
for f in *
do
v=$(basename $(readlink $f))
echo "sudo mount /dev/$v /media/\$USER/$f"
done
$ sh ../nols.sh
sudo mount /dev/sda4 /media/$USER/Documents+Edu
sudo mount /dev/sda3 /media/$USER/Ext4
sudo mount /dev/sda6 /media/$USER/Game+Movie
sudo mount /dev/sda7 /media/$USER/OS
sudo mount /dev/sda5 /media/$USER/Software+Video
答案 2 :(得分:0)
# -- Prepare ---
Folder='/dev/disk/by-label/'
HeaderLen=$(ls -ld . | wc -c )
# -- execute ---
ls -l "${Folder}" | sed "1d;s#.\{${HeaderLen}\}\(.*\) -> .*\(/[^/]*\)#sudo mount /dev\2 /media/\$USER/\1#;t;d"
->
分开的两组文字分开,以新格式重新填写