RegEx Matching on text and new line

馋奶兔 提交于 2020-06-01 02:17:30

问题


I have a file which appends a binary file to a bash script (in this case an ISO). It ultimately gets pulled apart, executing the bash script to install the ISO. The bash script ends in a unique string; "DATA" and during its execution the script deletes everything above and including that line.

It doesn't however remove the terminating new line which means the new file (which should just be the binary ISO data) begins with a newline, breaking the ISO.

The regex deleting the data is as follows

sed -re '1,/^__DATA__$/d'

Viewing the file with cat -e I'm trying to match and delete the $ after __DATA__$

#cat -et file.bin

echo "I: Installation finished!"$
exit 0$
$
__DATA__$
$
3<ED>M-^PM-^PM-^PM-^PM-^

回答1:


I suggest using

sed -n '/__DATA__/{n;:1;n;p;b1}' file

Here, -n suppresses the default output of lines, then the first line containing __DATA__ is found, and then a line is read into the pattern space, and then all subsequent lines are read and printed. So, we skip the part of the file before __DATA__ line and the next line.

See the online sed demo

s='#cat -et file.bin

echo "I: Installation finished!"
exit 0

__DATA__

3<ED>M-^PM-^PM-^PM-^PM-^'

sed -n '/__DATA__/{n;:1;n;p;b1}' <<< "$s"

Output:

3<ED>M-^PM-^PM-^PM-^PM-^


来源:https://stackoverflow.com/questions/61588091/regex-matching-on-text-and-new-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!