Sed to remove only first line erroneously removes last line too

Hello everyone,

This is my first posting. I have read the rules of this forum. I have searched many various threads and haven't found one that applies to my situation or suggestions to fix the issue. I do appreciate the help.

I am trying to execute a basic UNIX script in a Solaris environment using the Korn shell.

I simply want to use sed to delete the first line in my file (the column headings row) and output to a file - that's it.

I've tried the following and they all delete the first row as desired, but the last row is erroneously deleted for some reason:

sed "/SpBene/"d /path/data.txt

sed -e '1d' /path/data.txt

sed -e '1d' /path/data.txt > data.tmp
mv data.tmp > data.txt

Whether the output is to screen or file, the results are the same. :wall:

I appreciate any help!

I believe you do not have the last newline char in your file. Check whether this works:

 { cat INPUTFILE; echo; } | sed '1d'

Hello yazu,

When I execute your command on the console, it is successful in producing the desired results. :b:

However, I am having difficulty incorporating it into my final script. I have copied a version of it below for review. I've tried several ways and when executing the script, I am getting errors such as "broken pipe" or "garbled." The last row is still being removed. :confused:

Can someone please review my script and let me know how I am supposed to incorporate the proper syntax so my last record is not deleted?

In a nutshell, this script is to take the original file, emfdata.asc, replace all 3-char delimiters (*~|) with only pipes...Then delete the first row (column headings)...Finally write it to a file called emfdata.dat.

#!/bin/sh
if [ -s /test/dir/a00/datafiles/emf/emfdata.asc ]
then
sed -e 's/~|/|/g' -e 's_._|&_' /test/dir/a00/datafiles/emf/emfdata.asc > /test/dir/a00/datafiles/emf/emfdata.tmp
sed '1d' /test/dir/a00/datafiles/emf/emfdata.tmp > /test/dir/a00/datafiles/emf/emfdata.dat
mv /test/dir/a00/datafiles/emf/emfdata.asc /test/dir/a00/datafiles/emf/emfdata.asc.old
rm /test/dir/a00/datafiles/emf/emfdata.tmp
fi

A couple of additional things: I think maybe the last line being deleted is occuring after the first sed command too...not the second, as I found out when I did the output to screen. Also, my script produces the correct results for a test file with only 5 records, but is not for a file containing 185 records...Why is that?

Thank you in advance! :slight_smile:

Well... Look at this examples (where <ENTER> and <CTRL-D> are keys):

$ cat >file1
1<ENTER>
2<CTRL-D>
$ cat >file2
1<ENTER>
2<ENTER>
<CTRL-D>
$ od -c file1
0000000   1  \n   2
$ od -c file2
0000000   1  \n   2  \n

As you can see the last file contains the newline char as the last char and the first do not.
I was told according POSIX standard the first file is not a proper text file.
You can verify your file with:

od -c YOURFILE | tail

I believe all GNU tools can deal with such files but you are on an old Solaris system or something like. So what can you do?
If it is the isolated case then you can just fix your file in your text editor. How to do this depends on your editor. If it is not then you can try filter your file (before processing) through the compound command that I gave like so:

{ cat /test/dir/a00/datafiles/emf/emfdata.asc; echo; } |
sed -e 's/*~|/|/g' -e 's_.*_|&_'  > /test/dir/a00/datafiles/emf/emfdata.tmp

Regards,
jazy.

Thanks Yazu! The information was very helpful.

However, my script successfully worked as is when I retried it (for the 50th time).

I discovered the test files (about 4 different ones, same format/data type, just different number of records) I was attempting to convert with my script were apparently not the same (maybe extra blank line at the bottom or something?). When I deleted the "bad" test file and uploaded a new one of that file, my script worked.

Thanks for your time.:slight_smile: