Deleting first two lines from a text-file

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Deleting first two lines from a text-file Sat Dec 29, 2012 12:32 pm • by thoresson
Hi,

I want to remove the first two line from a textfile. Trying with

Code: Select all
sed '1,2d' "$1"


as a shell script doesn't work. Why not?

Thanks,
Anders
thoresson
 
Posts: 4
Joined: Sat Dec 29, 2012 12:30 pm

You need to write back to the file - without that your file content is going to STDOUT (i.e. Hazel logs):

Code: Select all
sed '1,2d' "$1" > "$1"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Of course, to write the result to disk makes sense :-) Problem is that the suggested command gives me an empty file, with a size of zero bytes. Why?
thoresson
 
Posts: 4
Joined: Sat Dec 29, 2012 12:30 pm

Perhaps the rule is running multiple times such that the top two lines are deleted until the file is empty.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Possible, but I don't think so. The script is as follows:

Code: Select all
sed '1,2d' "$1" > "$1"
cat "$1" >> "/Users/anders/Dropbox/Private/ideas.md"


Nothing is added to ideas.md. But if I only use cat, excluding sed, content is appended to ideas.md.
thoresson
 
Posts: 4
Joined: Sat Dec 29, 2012 12:30 pm

Why not this:

Code: Select all
sed '1,2d' "$1" >> "/Users/anders/Dropbox/Private/ideas.md"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Probably because my scripting skills aren't that good. :)

Thanks, now it works as I want it to.
thoresson
 
Posts: 4
Joined: Sat Dec 29, 2012 12:30 pm


Return to Support