sed - delete first line of text file

sed - delete first line of text file

Post by Rob » Wed, 06 Oct 2004 19:56:53



I'm trying to delete the first line of my text file using:-

sed -n '1,1d' $TEMPFILE > $TEMPFILE

but, this deletes all text from the file.

I think i'm missing carridge returns or new lines from the end of each line
so the I think sed sees all text in the file as one line.  Although, when I
use vi I can see that the file seems normal (i.e. there are lines in it).
Should I convert the file somehow?  I created the file by running a command
and redirecting it's output to a file using >.

 
 
 

sed - delete first line of text file

Post by Chris Thomps » Wed, 06 Oct 2004 20:14:54




>I'm trying to delete the first line of my text file using:-

>sed -n '1,1d' $TEMPFILE > $TEMPFILE

>but, this deletes all text from the file.

>I think i'm missing carridge returns or new lines from the end of each line
>so the I think sed sees all text in the file as one line.  Although, when I
>use vi I can see that the file seems normal (i.e. there are lines in it).
>Should I convert the file somehow?  I created the file by running a command
>and redirecting it's output to a file using >.

Your problem is that $TEMPFILE is emptied by the output redirection even
before sed is called. Don't try to read from and write to the same file
in a single command unless you know exactly what you are doing.

Also, I recommend using a shell (bash, ksh, or even csh) that has the
noclobber option, and setting it.

Chris Thompson
Email: cet1 [at] cam.ac.uk

 
 
 

sed - delete first line of text file

Post by Stephane CHAZELA » Wed, 06 Oct 2004 20:23:08


2004-10-5, 20:56(+10), Rob L:
Quote:> I'm trying to delete the first line of my text file using:-

> sed -n '1,1d' $TEMPFILE > $TEMPFILE

> but, this deletes all text from the file.

[...]

Yes.

command > file

opens file for writing (and thus truncates it to 0 size), points
stdout to that file and then starts sed.

So, when sed is started, the file is empty.

in

{ rm -- "$TEMPFILE" && tail +2 > "$TEMPFILE"; } < "$TEMPFILE"

stdin is made pointing to "$TEMPFILE", then this file is
removed, then a new one is created, then tail stdout is made
pointing to it.

Beware that with that method, if the new $TEMPFILE can't be
created, then the original file is lost. That's why one
generally uses a tempfile:

cp -i -- "$TEMPFILE" "$TEMPFILE.back" &&
  tail +2 < "$TEMPFILE.back" > "$TEMPFILE" &&
  rm -f -- "$TEMPFILE.back"

You could also use ex:

ex -sc '1d|x' -- "$TEMPFILE"

--
Stephane

 
 
 

sed - delete first line of text file

Post by Rob » Wed, 06 Oct 2004 20:49:02


Thanks, that worked.

One more question.  My text file contains columns that have tabs between
them.  But, awk -F\t   doesn't work.  If I vi the text file what look like
tabs actually seem to be spaces as I can move the cursor along space by
space within the tab.  How can I convert these to tabs?


Quote:> 2004-10-5, 20:56(+10), Rob L:
> > I'm trying to delete the first line of my text file using:-

> > sed -n '1,1d' $TEMPFILE > $TEMPFILE

> > but, this deletes all text from the file.
> [...]

> Yes.

> command > file

> opens file for writing (and thus truncates it to 0 size), points
> stdout to that file and then starts sed.

> So, when sed is started, the file is empty.

> in

> { rm -- "$TEMPFILE" && tail +2 > "$TEMPFILE"; } < "$TEMPFILE"

> stdin is made pointing to "$TEMPFILE", then this file is
> removed, then a new one is created, then tail stdout is made
> pointing to it.

> Beware that with that method, if the new $TEMPFILE can't be
> created, then the original file is lost. That's why one
> generally uses a tempfile:

> cp -i -- "$TEMPFILE" "$TEMPFILE.back" &&
>   tail +2 < "$TEMPFILE.back" > "$TEMPFILE" &&
>   rm -f -- "$TEMPFILE.back"

> You could also use ex:

> ex -sc '1d|x' -- "$TEMPFILE"

> --
> Stephane

 
 
 

sed - delete first line of text file

Post by Stephane CHAZELA » Wed, 06 Oct 2004 20:57:58


2004-10-5, 21:49(+10), Rob L:
[...]
Quote:> One more question.  My text file contains columns that have tabs between
> them.  But, awk -F\t   doesn't work.  If I vi the text file what look like
> tabs actually seem to be spaces as I can move the cursor along space by
> space within the tab.  How can I convert these to tabs?

[...] please don't top-post

man unexpand

--
Stephane

 
 
 

sed - delete first line of text file

Post by Rob » Wed, 06 Oct 2004 21:14:57


Thanks Stephane, it sort of works but the tabs are not clearly defined.  How
can I replace every 2 or more white spaces with a command ","?


Quote:> 2004-10-5, 21:49(+10), Rob L:
> [...]
> > One more question.  My text file contains columns that have tabs between
> > them.  But, awk -F\t   doesn't work.  If I vi the text file what look
like
> > tabs actually seem to be spaces as I can move the cursor along space by
> > space within the tab.  How can I convert these to tabs?
> [...] please don't top-post

> man unexpand

> --
> Stephane

 
 
 

sed - delete first line of text file

Post by Stephane CHAZELA » Wed, 06 Oct 2004 21:31:06


2004-10-5, 22:14(+10), Rob L:
Quote:> Thanks Stephane, it sort of works but the tabs are not clearly defined.  How
> can I replace every 2 or more white spaces with a command ","?

[...]

You can learn about top-posting and why it seems to annoy that
much people on usenet here:

http://google.com/search?q=top-posting

tr -s ' ' ,
replaces one or more spaces with a ","

sed 's/   */,/g'
(three spaces above)
replaces two or more spaces with a ","

If you want to see which is a SPC and which is a TAB, you can
use "cat -vt" (it shows TABs as ^I).

Note that

awk -F\t

is the same as

awk -Ft

(because the shell turns \t into t before calling awk)
which is not always the same as

awk -F'\t'

(some awk implementations consider that "t" must be understood
as a TAB character, all consider that "\t" must be understood as
a TAB character).

--
Stephane

 
 
 

sed - delete first line of text file

Post by Chris Matter » Thu, 07 Oct 2004 01:34:51



> I'm trying to delete the first line of my text file using:-

> sed -n '1,1d' $TEMPFILE > $TEMPFILE

> but, this deletes all text from the file.

Well, no duh.  You used the -n flag.  That changes sed's default action on
a line from "print it" to "throw it away".  Try it again without the flag.

No duh, twice.  You redirected stdout to your input file.  That wipes
your input file empty before processing starts.  Select a *new* file
to receive your output.

Quote:

> I think i'm missing carridge returns or new lines from the end of each
> line
> so the I think sed sees all text in the file as one line.  Although, when
> I use vi I can see that the file seems normal (i.e. there are lines in
> it).
> Should I convert the file somehow?  I created the file by running a
> command and redirecting it's output to a file using >.

--
             Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"

 
 
 

sed - delete first line of text file

Post by Rob » Thu, 07 Oct 2004 11:57:24


Thanks Stephane.

Hmmm, "top posting".  I'm using Outlook Express and it always puts my reply
at the top of the email.  Sorry this has annoyed you.  I'm sure you can put
up with it for the rest of this thread!


Quote:> 2004-10-5, 22:14(+10), Rob L:
> > Thanks Stephane, it sort of works but the tabs are not clearly defined.
How
> > can I replace every 2 or more white spaces with a command ","?
> [...]

> You can learn about top-posting and why it seems to annoy that
> much people on usenet here:

> http://google.com/search?q=top-posting

> tr -s ' ' ,
> replaces one or more spaces with a ","

> sed 's/   */,/g'
> (three spaces above)
> replaces two or more spaces with a ","

> If you want to see which is a SPC and which is a TAB, you can
> use "cat -vt" (it shows TABs as ^I).

> Note that

> awk -F\t

> is the same as

> awk -Ft

> (because the shell turns \t into t before calling awk)
> which is not always the same as

> awk -F'\t'

> (some awk implementations consider that "t" must be understood
> as a TAB character, all consider that "\t" must be understood as
> a TAB character).

> --
> Stephane

 
 
 

sed - delete first line of text file

Post by Rich Tee » Thu, 07 Oct 2004 12:08:50



> Thanks Stephane.

> Hmmm, "top posting".  I'm using Outlook Express and it always puts my reply

OE is broken in many ways.  Using broken tools is no excuse for
not respecting the conventions of a newsgroup.

Quote:> at the top of the email.  Sorry this has annoyed you.  I'm sure you can put
> up with it for the rest of this thread!

When in Rome...

--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming",
published in August 2004.

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich

 
 
 

sed - delete first line of text file

Post by Rob » Thu, 07 Oct 2004 14:05:46


I do respect this newsgroup, i've posted and responded to posts for many
years!  I've never had anyone complain to me about top posting.

I know you've also been on this group for long time Rich (and helped me
several times) and I respect your posts so please tell me how to change this
in OE so I bottom post and i'll happily oblidge!



> > Thanks Stephane.

> > Hmmm, "top posting".  I'm using Outlook Express and it always puts my
reply

> OE is broken in many ways.  Using broken tools is no excuse for
> not respecting the conventions of a newsgroup.

> > at the top of the email.  Sorry this has annoyed you.  I'm sure you can
put
> > up with it for the rest of this thread!

> When in Rome...

> --
> Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming",
> published in August 2004.

> President,
> Rite Online Inc.

> Voice: +1 (250) 979-1638
> URL: http://www.rite-group.com/rich

 
 
 

sed - delete first line of text file

Post by Colin B » Fri, 08 Oct 2004 02:14:57



> I'm trying to delete the first line of my text file using:-

> sed -n '1,1d' $TEMPFILE > $TEMPFILE

> but, this deletes all text from the file.

> I think i'm missing carridge returns or new lines from the end of each line
> so the I think sed sees all text in the file as one line.  Although, when I
> use vi I can see that the file seems normal (i.e. there are lines in it).
> Should I convert the file somehow?  I created the file by running a command
> and redirecting it's output to a file using >.

In addition to the other points people have made about clobbering files, you
might be able to dump sed altogether and use "tail +2" on the file instead.

Colin

 
 
 

sed - delete first line of text file

Post by Stephane CHAZELA » Wed, 13 Oct 2004 19:52:17


2004-10-12, 02:56(-07), Michael Paoli:
[...]

Quote:> You may want to consider something that actually does an
> edit in place, such as ed.  Under a Bourne-compatible shell:

> echo '1d
> w
> q' | ed file

Note that you may encounter troubles and loose data when using
ed not interactively (and the error reporting is far from
perfect, then).

The "ex" command was standardized by POSIX and has a
non-interactive mode (the -s option).

ex -sc '1d|x' file

--
Stephane

 
 
 

1. Delete a whole line with sed, then insert diff text on same line...

Ok, I've searched through this group about 'deleting' and 'replacing'
with sed and still couldnt find the answer to my question.

Basically, i want to search for a certain string in a file, then
delete the entire line.  Immediately after this, i would like to
insert another string on the same line.  Is this possible?

So, lets say that in every file I need to search, there is one line of
code that starts with "My Text:" but the text following this string
(on the same line) is different in every file.  I would like to delete
this line entirely and insert new, different text here instead, such
as "Your Text..."

Let me know if this doesnt make sense, and i appreciate any help that
is given.  Thanks...

Matt

2. IP-Routing with Accounting

3. Sed: Replace text in one file with SPECIFIC text from another

4. Compile error - kernel 2.5.6pre2

5. Delete the first line of a 1.4 million lines file

6. 2.5 ethernet/irq problem

7. delete mutliple lines text with sed

8. WinTV - PCI drivers

9. sed/awk question : delete first blank in every line

10. getting the first X chars of a text line using sed.exe

11. Find First Match Using SED and Then Extract Text from Line

12. Find a string, delete that line, delete 1 line before it and all lines after it...

13. sed: deleting lines from a file