使用 sed 跟 awk 將一列資料改成一行

dpkg -l | grep 5.4.0 | awk '/ii/{print $2}' | sed ':a ; N;s/\n/ / ; t a ; '

範例:查詢系統安裝了哪些跟 nvidia 有關的套件

ubuntu@guestOS:~$ dpkg -l | grep nvidia | grep ii
ii  libnvidia-cfg1-460:amd64                      460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA binary OpenGL/GLX configuration library
ii  libnvidia-common-460                          460.39-0ubuntu0.18.04.1                                   all          Shared files used by the NVIDIA libraries
ii  libnvidia-compute-460:amd64                   460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA libcompute package
ii  libnvidia-compute-460:i386                    460.39-0ubuntu0.18.04.1                                   i386         NVIDIA libcompute package
ii  libnvidia-decode-460:amd64                    460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA Video Decoding runtime libraries
ii  libnvidia-decode-460:i386                     460.39-0ubuntu0.18.04.1                                   i386         NVIDIA Video Decoding runtime libraries
ii  libnvidia-encode-460:amd64                    460.39-0ubuntu0.18.04.1                                   amd64        NVENC Video Encoding runtime library
ii  libnvidia-encode-460:i386                     460.39-0ubuntu0.18.04.1                                   i386         NVENC Video Encoding runtime library
ii  libnvidia-extra-460:amd64                     460.39-0ubuntu0.18.04.1                                   amd64        Extra libraries for the NVIDIA driver
ii  libnvidia-fbc1-460:amd64                      460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA OpenGL-based Framebuffer Capture runtime library
ii  libnvidia-fbc1-460:i386                       460.39-0ubuntu0.18.04.1                                   i386         NVIDIA OpenGL-based Framebuffer Capture runtime library
ii  libnvidia-gl-460:amd64                        460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA OpenGL/GLX/EGL/GLES GLVND libraries and Vulkan ICD
ii  libnvidia-gl-460:i386                         460.39-0ubuntu0.18.04.1                                   i386         NVIDIA OpenGL/GLX/EGL/GLES GLVND libraries and Vulkan ICD
ii  libnvidia-ifr1-460:amd64                      460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA OpenGL-based Inband Frame Readback runtime library
ii  libnvidia-ifr1-460:i386                       460.39-0ubuntu0.18.04.1                                   i386         NVIDIA OpenGL-based Inband Frame Readback runtime library
ii  nvidia-compute-utils-460                      460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA compute utilities
ii  nvidia-dkms-460                               460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA DKMS package
ii  nvidia-driver-460                             460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA driver metapackage
ii  nvidia-kernel-common-460                      460.39-0ubuntu0.18.04.1                                   amd64        Shared files used with the kernel module
ii  nvidia-kernel-source-460                      460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA kernel source package
ii  nvidia-prime                                  0.8.15.3~0.18.04.1                                        all          Tools to enable NVIDIA's Prime
ii  nvidia-settings                               440.82-0ubuntu0.18.04.1                                   amd64        Tool for configuring the NVIDIA graphics driver
ii  nvidia-utils-460                              460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA driver support binaries
ii  xserver-xorg-video-nvidia-460                 460.39-0ubuntu0.18.04.1                                   amd64        NVIDIA binary Xorg driver

使用 awk 搜尋擷取套件名稱

ubuntu@guestOS:~$ dpkg -l | grep nvidia | awk '/ii/{print $2}' 
libnvidia-cfg1-460:amd64
libnvidia-common-460
libnvidia-compute-460:amd64
libnvidia-compute-460:i386
libnvidia-decode-460:amd64
libnvidia-decode-460:i386
libnvidia-encode-460:amd64
libnvidia-encode-460:i386
libnvidia-extra-460:amd64
libnvidia-fbc1-460:amd64
libnvidia-fbc1-460:i386
libnvidia-gl-460:amd64
libnvidia-gl-460:i386
libnvidia-ifr1-460:amd64
libnvidia-ifr1-460:i386
nvidia-compute-utils-460
nvidia-dkms-460
nvidia-driver-460
nvidia-kernel-common-460
nvidia-kernel-source-460
nvidia-prime
nvidia-settings
nvidia-utils-460
xserver-xorg-video-nvidia-460

使用 sed 將列改成行,並用空格分開

ubuntu@guestOS:~$ dpkg -l | grep nvidia | awk '/ii/{print $2}' | sed ':a ; N;s/\n/ / ; t a ; '
libnvidia-cfg1-460:amd64 libnvidia-common-460 libnvidia-compute-460:amd64 libnvidia-compute-460:i386 libnvidia-decode-460:amd64 libnvidia-decode-460:i386 libnvidia-encode-460:amd64 libnvidia-encode-460:i386 libnvidia-extra-460:amd64 libnvidia-fbc1-460:amd64 libnvidia-fbc1-460:i386 libnvidia-gl-460:amd64 libnvidia-gl-460:i386 libnvidia-ifr1-460:amd64 libnvidia-ifr1-460:i386 nvidia-compute-utils-460 nvidia-dkms-460 nvidia-driver-460 nvidia-kernel-common-460 nvidia-kernel-source-460 nvidia-prime nvidia-settings nvidia-utils-460 xserver-xorg-video-nvidia-460

發表迴響

回到頂端