sed '/^$/d' Add.asm

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

sed '/^$/d' Add.asm

linse
I'm writing my assembler in bash using the command 'sed' and regex to remove blank lines. Strangely, the code in the title doesn't effect the desired change. Does anyone know why the command might not be working properly?
Reply | Threaded
Open this post in threaded view
|

Re: sed '/^$/d' Add.asm

cadet1620
Administrator
linse wrote
I'm writing my assembler in bash using the command 'sed' and regex to remove blank lines. Strangely, the code in the title doesn't effect the desired change. Does anyone know why the command might not be working properly?
Great project! I wonder if anyone's written a Universal Turing Machine in bash, thus proving that any program can be written in bash?

I suspect that Add.asm has MSDOS line endings, \r\n, and the \r causes the ^$ to not match.
Try using fromdos to convert it to Linux line endings.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: sed '/^$/d' Add.asm

linse
cadet1620 wrote
Great project! I wonder if anyone's written a Universal Turing Machine in bash, thus proving that any program can be written in bash?
Thanks. No idea if there's a Turing machine in bash. Seems feasible.

cadet1620 wrote
I suspect that Add.asm has MSDOS line endings, \r\n, and the \r causes the ^$ to not match.
Try using fromdos to convert it to Linux line endings.
Thanks. I'll look into it and post my results here.

--Philip.
Reply | Threaded
Open this post in threaded view
|

Re: sed '/^$/d' Add.asm

linse
Your suggestion worked. Thank you.

To download the package

sudo apt-get install tofrodos

Interestingly, I viewed the Add.asm file with vi on both Linux (3.5.0-47-generic) and BSD (Mac OS X 10.7.5). The DOS new line characters appeared as ^M on Linux. With BSD they didn't appear at all.

The difference may lie in different vi versions. Any input on this would interest me.
Reply | Threaded
Open this post in threaded view
|

Re: sed '/^$/d' Add.asm

cadet1620
Administrator
Line endings have always been a cross-platform problem. It was even worse before Macs moved to Unix; before that they used \r as new-line (without \n) so there were three line ending styles.  fgets() on Unix wouldn't see the \r as a new line so it would keep reading until your buffer was full.

My ANSI C Hack assembler uses this code to read lines so that it can read both styles of new-lines when running on Windows and Linux (and Macs I assume; I don't have one to test on).
    if (fgets(sourceLine, sizeof(sourceLine), inFile)) {
        int i = strlen(sourceLine)-1;
        while (i >= 0 && (sourceLine[i] == '\n' || sourceLine[i] == '\r'))
            sourceLine[i--] = '\0';
        ...
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: sed '/^$/d' Add.asm

linse
Thanks for the reply.

ANSI C is lost on me I'm afraid. I've seen it on the 'Everything a CS Major Should Know'-list. I should get to it one day.

Having said that, the idea isn't lost. I hadn't considered including a dos-to-linux or linux-to-dos end-of-line converter in my code. I'm still not sure if I will. But the lesson, in my mind at least, is assume nothing.

Before you offered your solution, I assumed that, well, text is text. Obviously not.

Thanks again.

--Philip.