Sed to remove only first line erroneously removes last line too

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed to remove only first line erroneously removes last line too
# 1  
Old 08-04-2011
Question 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. Smilie

I appreciate any help!
# 2  
Old 08-04-2011
I believe you do not have the last newline char in your file. Check whether this works:
 { cat INPUTFILE; echo; } | sed '1d'

# 3  
Old 08-04-2011
Hello yazu,

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

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. Smilie

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.

if [ -s /test/dir/a00/datafiles/emf/emfdata.asc ]
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

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! Smilie
# 4  
Old 08-05-2011
Well... Look at this examples (where <ENTER> and <CTRL-D> are keys):
$ cat >file1
$ cat >file2
$ 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

# 5  
Old 08-05-2011
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.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newline to space code removes 0 from the first line

I am having a peculiar problem. First I run the code below to append 0 at the start of each line in some hundreds of files that I have in a directory. These files have each word in a newline. for f in *.dat; do echo "0" > tmpfile cat $f >> tmpfile mv tmpfile $f done Then I run this... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

2. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

remove particular line from a file using sed

Hi i need to remove all the lines staring with 'printf("\n' from a file, example : the file tmp.txt contains printf("\n "); printf("\n good"); printf("\n "); printf("\n "); printf(""); printf( m_sprintf(for printf("\n "); i have tried with following commands but... (5 Replies)
Discussion started by: mprakasheee
5 Replies

4. Shell Programming and Scripting

SED to remove a line above and lines below.

:confused:Hi All, I need help on removing lines in a text file. Sample file : When there is a match ip for IPAddress in my `cat ip.out`, proceed delete line above until string "Comp" is found. Thank you very much. ---------- Post updated at 12:56 AM ---------- Previous update was... (4 Replies)
Discussion started by: chiewming
4 Replies

5. Shell Programming and Scripting

sed -e remove line from file

Hi Trying to remove line from file log_January_1_array and code below doesn't work. $(sed -e '/"$n"/d' <log_January_1_array >log_January_1_array_1) sed doesn't know what is in $n variable and nth happens. Please advice how to make sed running this. thx (2 Replies)
Discussion started by: presul
2 Replies

6. Shell Programming and Scripting

sed: remove first character from particular line

Hello Experts, I have a file "tt.txt" which is like: #a1=a2 b1=b2 #c1=c2 I need to remove the pound (#) sign from a particular line. In this case let us assume it's 3rd line : "#c1=c2" I can do it through: sed "s/#c1=c2/c1=c2/" tt.txtbut it is possible that I may not know the value... (6 Replies)
Discussion started by: hkansal
6 Replies

7. Shell Programming and Scripting

Remove blank line - SED

HI, I have this list of apps like so: DivX DivX Uninstall DivX for Build Install Flash Player 8 ... (10 Replies)
Discussion started by: pcwiz
10 Replies

8. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies

9. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies
Login or Register to Ask a Question