sed的一個問題 - Linux

Callum avatar
By Callum
at 2018-10-22T22:08

Table of Contents

我手邊有一個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

--
Tags: Linux

All Comments

Caroline avatar
By Caroline
at 2018-10-24T17:38
你把該行清空了,但行首 ^ 還是存在,還是取代成功
Connor avatar
By Connor
at 2018-10-29T08:01
所以要把行首^取代為空白 多做這個步驟?
我還在想這個要怎麼寫orz
Charlotte avatar
By Charlotte
at 2018-10-31T09:35
sed -e "s/#.*//" -e "/^$/d" -e "s/^/${...}/"
Hedwig avatar
By Hedwig
at 2018-11-01T02:14
sed -e "/^#/d" -e "s/#.*//" -e "s/^/${...}/"
Charlie avatar
By Charlie
at 2018-11-02T10:55
還有, 其實有正規表示式版 (RegExp)
Andy avatar
By Andy
at 2018-11-03T01:42
一是刪掉 #.* 後刪除空白行; 二是先把 # 開頭的行刪掉
Kumar avatar
By Kumar
at 2018-11-03T06:59
不過這類用途應該只會用一兩次, 其實沒必要寫成script
Heather avatar
By Heather
at 2018-11-04T22:44
隨便 grep / awk 兜一兜就好了. (預期套件名沒有空白)
Frederic avatar
By Frederic
at 2018-11-09T20:20
cat txt |egrep -v "^#|^$" |awk '{print "ooxx "$1}'
Hedda avatar
By Hedda
at 2018-11-14T11:25
另外, 空白行 ^$ 可以考慮改用 ^\s*$
Bennie avatar
By Bennie
at 2018-11-17T05:23
${...} = ${pacman_cmd}, 太長推文擺不下 @@

linux sever網路驅動問題

Harry avatar
By Harry
at 2018-10-22T20:09
andlt;!--各位好--andgt; 最近研究室一台新的linux server同學剛灌 應該是灌ubuntu server 完全沒圖形介面 可是卡在網路驅動一直搞不定 lspci下去應該是Intel I210這張網卡 去官網載了驅動,可是安裝就一直失敗 tar.gz放在隨身碟插進去安裝 好像缺少什麼檔 ...

Ubuntu有的程式點開啟/另存會卡住

Suhail Hany avatar
By Suhail Hany
at 2018-10-22T04:23
更新18.04之後的症狀,試了全新安裝也一樣。 在點開啟舊檔、另存新檔、或是其他會開啟資料夾選單的功能時, 有的程式會卡住不再回應。 目前觀察到有這問題的: Luminance HDR, Mendeley, mGBA, OpenShot 沒問題的: GIMP, firefox, GnuCash, Skyp ...

不同軟體音量異常互相影響

Tracy avatar
By Tracy
at 2018-10-20T16:09
大家好,我遇到這個問題大約一兩個月了,起先試圖自行找出原因,但實在找不出來。 原本預期的音效系統應該要這樣運作 === 1. 打開 Firefox 用 YouTube 聽音樂,從 YouTube 介面把音量降到一定程度 2. 打開 KNetWalk 小玩一下,不修改這個軟體的音量大小,理應會是 100% ...

thinkpad trackpoint 小紅點速度很慢

Edward Lewis avatar
By Edward Lewis
at 2018-10-19T21:19
是 thinkpad 13 , 在 windows 下可以用 lenovo 附的驅動調整, 但在 linux 下會變很慢, (我是 debian ,試了 ubuntu live cd 也差不多。) 如果是用 libinput 當驅動,可以調的區間只有 ±1 , 就算調到 +1 還是很慢。 google 很多 ...

https://www.sslforfree.com/出現錯誤

Wallis avatar
By Wallis
at 2018-10-19T09:38
Dear All, https://www.sslforfree.com/ test是我改過的,出現錯誤如下,不曉得可能是什麼問題呢? Domain and#34;cms.test.comand#34; challenge3 failed. Response from and#34;https://ac ...