1

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-^
Alex Turner
  • 470
  • 1
  • 4
  • 14
  • Try `sed '/^__DATA__$/,+1d' file` – Wiktor Stribiżew May 04 '20 at 08:52
  • Or, `sed '/^__DATA__$/{N;/\n$/d}' file` – Wiktor Stribiżew May 04 '20 at 08:59
  • Unfortunately not. I tried adding the 1,/ to the start of the regex also with out success – Alex Turner May 04 '20 at 09:27
  • But why do you need that `1,/`? Do you remove a range then? From the first line till the `__DATA__` line + blank line after? – Wiktor Stribiżew May 04 '20 at 09:28
  • 1
    I read your question and comments a few times, and I still don't understand what you are trying to do. You explained the context, and your code attempt, but not what it is that you wish to do. From your attempt it seems pretty clear that you want to delete something. What, *exactly*? –  May 04 '20 at 13:30
  • wrt `Most text editors seem to append a new line to the end of the script.` - I'd hope so because otherwise, by definition, the content isn't a text file. Per POSIX every text file (and every line within a text file) ends with `\n` and that is the only type of file that standard UNIX text processing tools and editors are required to work on. Anything else is undefined behavior. Please [edit] your question to include the expected output given your posted sample input. – Ed Morton May 04 '20 at 18:21
  • Thanks all for the comments- recognise that the question was poorly worded and I've updated it – Alex Turner May 05 '20 at 07:26
  • @WiktorStribiżew that's exactly what I'm trying to do, delete the first line to the `__DATA__` and the new line after that – Alex Turner May 05 '20 at 07:27
  • Try `sed -n '/__DATA__/{n;:1;n;p;b1}' file` – Wiktor Stribiżew May 05 '20 at 18:16
  • 1
    Hi, did it finally help? – Wiktor Stribiżew May 09 '20 at 10:00
  • Perfect - it worked with `-re` but not `-n` thanks so much @WiktorStribiżew!! – Alex Turner May 11 '20 at 12:39
  • Please check the below answer, I explained how it works. Not sure how you made it work with `-re` – Wiktor Stribiżew May 11 '20 at 12:45

1 Answers1

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-^
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks for this Wiktor. This appears to work fine in Ubuntu however not in BusyBox. Perhaps slightly off topic but wondered if you had any idea? ```sed: unterminated { BusyBox:/ # sed -v sed: invalid option -- v BusyBox v1.25.1 (2020-04-29 14:15:19 AEST) multi-call binary.``` – Alex Turner May 11 '20 at 13:12
  • @AlexTurner It is probably due to the `;` inside curly brackets. Try `sed -n -e '/__DATA__/{' -e n -e:1 -e n -e p -e b1 -e '}' file` – Wiktor Stribiżew May 11 '20 at 14:40