LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cannot delete certain files with sed (https://www.linuxquestions.org/questions/linux-general-1/cannot-delete-certain-files-with-sed-124557/)

ganninu 12-10-2003 08:56 AM

cannot delete certain files with sed
 
Is this a bug??

On certain files I can use this command correctly:
sed 1d file1

However on other files (extracted from logs), I cannot remove the first line... sed 1d somefile seems to be performing nothing. This typical file, when viewed by vi, has a lot of "@" which disappear when i scroll downwards... What are these "@" ??? This file was extracted from a parsed logfile.


I need to delete the first line of this file since an output of a "diff file1 file2" will pre-append some string which i don't need.

thanks for your help, ganninu.

jim mcnamara 12-10-2003 10:37 AM

sed 1d filename > newfile
will delete the first line of a text file.

If the file has a binary format (some log files do) then it will not work the way you want.

A binary file is one where the logical record is NOT delimited by a newline.

Code:

wc -l filename
will give you the number of newlines in the file. However, binary files can have ANY character embedded in the logical record, even newlines -- which are ignored.

ganninu 12-10-2003 10:53 AM

so what do you suggest? :)

jim mcnamara 12-10-2003 11:00 AM

I do not know much about the file you are working with


What are you trying to do with the file? There are lots of ways to do things.

ganninu 12-10-2003 11:12 AM

The idea is this. I have a big growing logfile on a server. I'm grabbing the last 100 lines of this logfile and then I send it to a remote server. This remote server will have already a previous copy of this extracted file. Then I do a "diff" between these two versions. The problem is that 'diff -n file1 file2" returns me a reduntant line at the head of the file and i want to remove it. So, to cut it short, I'm using sed d1 file2 to remove the first line (generated by diff) of file2...

ganninu.

jim mcnamara 12-10-2003 01:19 PM

You're using
Code:

tail -100 /somewhere/mylogfile > tmpfile
and the first line of tmpfile always has a bunch of garbage in it?

For this file with garbage in it (using the filenames in the example I gave above) execute these commands (you can use vim or vi)
vi tmpfile

:set nu
G

You should now be at the last line of the file.
Quote:

100 bsauve 7126 1 0 Nov 17 ? 7:49 uzposs2
You should see a 100 over in the left column - meaning your file has 100 lines. What do you see? Do you see 100?

Plus, what does the garbage first line look like?

ganninu 12-10-2003 03:31 PM

erm... There is no garbage... everything is text... The first line i was talking abt was that one generated by DIFF - "d1 26" ... is there some special argument given to 'diff' to get rid of that???


...likewise, is there another fool-proof method to delete some lines out of a file?

thanks, ganninu


All times are GMT -5. The time now is 04:41 PM.