Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello,

I am on a Mac and trying to clean up some monthly files with a very simple SED:

sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt

(from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file)
then output to output.txt

Even when I try just one of the above, that doesn't work, either.

sed '3,10d' input.txt > output.txt

To me, this looks like it should be pretty basic, but I am new to command line and am probably doing something wrong. Or, I'm told there are differences between Mac Linux and other PC Linux.

I'd rather it be me making the mistake, than different flavors of Linux.

Any help appreciated.

First things first: welcome to the forum. Please use CODE-tags when posting code, data or terminal output, like the ones i have edited into your post - it will make it stand out nicely and will prevent ambiguities caused by how HTML is handling white space too. Furthermore: please always state your complete problem! I take from the title and your posting that you are on MacOS and your sed script is not working - but HOW is it not working? Did you get some error message? Did the wrong output show up? Did no output show up? Something else? "Doesn't work" can have so many different meanings that i simply don't have enough data to properly analyse what is going wrong.

A small correction: it means "delete every line which either has a line number 3-10 OR is in the range of a line containing "<ACROSS>" and the last line. Depending on the structure of your file that may make no difference or it may do so, i.e. if the line containing "<ACROSS>" is at the first line - then all lines (also line 1 and 2) would be deleted.

For this you need to understand how sed works: it reads in the input line after line and to every line one rule after the other is applied. You have two rules there: delete a line if its line number is 3-10 and delete a line if it is in the block from a line containing '<ACROSS>' and the last line. sed reads the first line, applies rule 1 to it - no, so don't delete. Then the second rule - maybe no, so still don't delete. Then it reads the next line, etc..

This code looks correct and should do what you expect it to do. Still, to understand why it doesn't work i would need to know how it didn't work.

One misconception i already found: MacOS is NOT a Linux! You are partially right that MacOS is a UNIX with some graphical lumber on top of it. The underlying UNIX is in fact not a Linux but a FreeBSD, which is a different kind of UNIX-derivative. The difference on your problem is zero, though, so you still have a problem for some not understood reason, but i think it is worthwhile to clear these things up immediately and not after they "settled" into your thinking as they might hurt your understanding later.

I hope this helps.

bakunin

1 Like

On top of what bakunin already said, it would be helpful to see a (or maybe even several) small sample input files with or without "ACROSS" in different positions, and the results (output / error msgs) of your scripts applied to them.
Where and how are your files generated? What are the line terminators?