[問題] sed的一個問題

看板Linux作者 (捲捲毛wei)時間5年前 (2018/10/22 22:08), 5年前編輯推噓5(507)
留言12則, 3人參與, 5年前最新討論串1/1
我手邊有一個Archlinux pacman安裝清單如下 : ---------------------------------------------------------------- #install.txt atom #(see: C1. Configure Atom editor) code #(see: C4. Configure vscode that like atom editor) wget p7zip networkmanager network-manager-applet #(attach to GUI setting) gnome-nettool #(GUI network manager) chromium rsync # (rsync is a good tool for backup on the local and the remote, # It can implement copy and exclude some files or directories: ---------------------------------------------------------------- 我要使用sed正規劃處理把#註解清除 查了一下sed的用法 寫個shell如下 : ---------------------------------------------------------------- #install.sh pacman_cmd="pacman -S " install_cmd=$(sed -e "s/#.*//" -e "s/^/${pacman_cmd}/" $1) echo -e "\e[1;32m${install_cmd}\e[0m" ---------------------------------------------------------------- 最後echo那段是顯示sed之後結果 執行的指令為 : ---------------------------------------------------------------- sh install.sh install.txt ---------------------------------------------------------------- 期望的輸出如下 : ---------------------------------------------------------------- pacman -S atom pacman -S code pacman -S wget pacman -S p7zip pacman -S networkmanager pacman -S network-manager-applet pacman -S gnome-nettool pacman -S chromium pacman -S rsync ---------------------------------------------------------------- 但實際的結果如下 : ---------------------------------------------------------------- pacman -S atom pacman -S code pacman -S wget pacman -S p7zip pacman -S networkmanager pacman -S network-manager-applet pacman -S gnome-nettool pacman -S chromium pacman -S rsync pacman -S pacman -S ---------------------------------------------------------------- 多了2個空的pacman -S 我知道是最後兩行的#沒被消掉 但是sed應該有把它消掉呀QQ 其他都正確 到這邊就不會處理了orz 還請板上高手幫忙 謝謝! -- 我老婆-子瑜,不服出來灣阿 https://imgur.com/sCghzuF
https://imgur.com/JvAE0Qu
https://imgur.com/k9PCODr
https://imgur.com/CZmFmeq
https://imgur.com/S7FgYgF
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.246.112.102 ※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1540217296.A.0F1.html

10/22 22:22, 5年前 , 1F
你把該行清空了,但行首 ^ 還是存在,還是取代成功
10/22 22:22, 1F

10/22 22:34, 5年前 , 2F
所以要把行首^取代為空白 多做這個步驟?
10/22 22:34, 2F

10/22 22:34, 5年前 , 3F
我還在想這個要怎麼寫orz
10/22 22:34, 3F

10/22 22:35, 5年前 , 4F
sed -e "s/#.*//" -e "/^$/d" -e "s/^/${...}/"
10/22 22:35, 4F

10/22 22:37, 5年前 , 5F
sed -e "/^#/d" -e "s/#.*//" -e "s/^/${...}/"
10/22 22:37, 5F
請問 "s/^/${...}/" 這段是什麼意思

10/22 22:38, 5年前 , 6F
還有, 其實有正規表示式版 (RegExp)
10/22 22:38, 6F

10/22 22:41, 5年前 , 7F
一是刪掉 #.* 後刪除空白行; 二是先把 # 開頭的行刪掉
10/22 22:41, 7F

10/22 22:46, 5年前 , 8F
不過這類用途應該只會用一兩次, 其實沒必要寫成script
10/22 22:46, 8F

10/22 22:47, 5年前 , 9F
隨便 grep / awk 兜一兜就好了. (預期套件名沒有空白)
10/22 22:47, 9F

10/22 22:49, 5年前 , 10F
cat txt |egrep -v "^#|^$" |awk '{print "ooxx "$1}'
10/22 22:49, 10F

10/22 22:59, 5年前 , 11F
另外, 空白行 ^$ 可以考慮改用 ^\s*$
10/22 22:59, 11F
因為我會把它放在網頁 當作一種備忘錄 方便之後的安裝 順便學習正規化工具~~~ ※ 編輯: dzwei (111.246.112.102), 10/22/2018 23:13:19

10/22 23:15, 5年前 , 12F
${...} = ${pacman_cmd}, 太長推文擺不下 @@
10/22 23:15, 12F
好 大致了解了 感謝您~~ ※ 編輯: dzwei (111.246.112.102), 10/22/2018 23:23:52 已經更新上去我的筆記了 https://tinyurl.com/ycnkj395 這種一次安裝多個pacman package的方式在第六點 我也有附上參考連結在這篇文章 謝謝各位大大的幫忙 ※ 編輯: dzwei (111.246.112.102), 10/23/2018 01:54:47
文章代碼(AID): #1RpTdG3n (Linux)