簡體   English   中英

Bash:更改postscript文件的“標題”

[英]Bash: Change “Title” of postscript file

我有一個postscript文件,我想在從它生成pdf之前更改“Title”屬性。

在文件開頭之后:

%!PS-Adobe-3.0
%%BoundingBox: 0 0 595 842
%%HiResBoundingBox: 0 0 595 842
%%Title: GMT v5.1.1_r12693 [64-bit] Document from pscoast
%%Creator: GMT5
[…]

我現在匹配%%Title: GMT v5.1.1_r12693 [64-bit] Document from pscoast^%%Title:\\s.*並且喜歡用冒號內容替換冒號后的所有內容。

到目前為止我的非工作代碼:

sed "s/\(^%%Title:\)\s.*$/\1 $title/g" test_file.ps

我的sed知識非常有限,到目前為止我的實驗並沒有產生任何有用的東西 - 非常感謝你的幫助。

一切順利,克里斯

編輯:添加了我的非工作代碼

sed正常工作的一個技巧就是讓shell引用正確。 這將創建一個帶有新標題的postscript文件:

newtitle="Shiny New Title"
sed 's/^%%Title:.*/%%Title: '"$newtitle/" sample.ps >new.ps

這會更新后記:

newtitle="Shiny New Title"
sed -i 's/^%%Title:.*/%%Title: '"$newtitle/" sample.ps

一個人在sed表達式中使用的許多字符,比如$(* ,都是shell-active。為了保護它們免受可能的shell擴展,它們應該是單引號。但是,因為有人希望shell擴展$newtitle變量,它不能用單引號。因此,如果仔細觀察,你會看到上面的替換表達式分為兩部分,一部分是單引號,一部分是雙引號。在它們之間加一個空格來制作它更清晰:

's/^%%Title:.*/%%Title: ' "$newtitle/" # Do not use this form.

因此,shell-active字符受單引號保護,只有我們希望shell搞亂的部分是雙引號

也許這就是你要找的東西:

myvar="some content"
sed -e "s/^\(%%Title:\).*/\1 $myvar/" < inputfile

# output
...
%%Title: some content
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM