Computing.Net > Forums > Unix > Delete last character in a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Delete last character in a file

Reply to Message Icon

Name: Sam
Date: July 15, 2003 at 17:52:31 Pacific
OS: Solaris
CPU/Ram: 6400/20GB
Comment:

How can I delete the last character in a file. I dont care what the character is - all i want to do is delete the last character.

Whats the easiest way to do this?

Any help is appreciated




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: July 15, 2003 at 19:47:38 Pacific
Reply:

Hi:

Here's one way:

#!/bin/ksh
nr=$(wc -l filename)

nawk '
{ if(NR nn)
print $0
else
{
str=substr($0, 1, length($0)-1)
print str
}
} ' nn="$nr" filename

You could read the whole file into an array, but I chose to find the number of lines before processing

Regards,

Nails


0

Response Number 2
Name: WilliamRobertson
Date: July 16, 2003 at 03:08:01 Pacific
Reply:

How about:

sed '$s/.$//' filename.dat


0

Response Number 3
Name: WilliamRobertson
Date: July 16, 2003 at 03:10:14 Pacific
Reply:

...though that will only make a difference if the last line of the file contains a character to delete. I'm not sure what you want to happen in that situation though.


0

Response Number 4
Name: Sam
Date: July 16, 2003 at 03:28:29 Pacific
Reply:

Here is what I want..

I have files that contain data in this format

name1,
name2,
name3,

I just want to get rid of comma after "name3". Note each file may contain different number of records - also each record maybe of varying length..


0

Response Number 5
Name: WilliamRobertson
Date: July 16, 2003 at 06:05:56 Pacific
Reply:

Then the sed command should work, i.e:

sed '$s/.$//' filename.dat

or to be safe you could specify a literal comma rather than "any character":

sed '$s/,$//' filename.dat

If the final line of the file doesn't match the pattern it will have no effect, which is what I meant above. In this case it sounds like what you'd want to happen anyway.


0

Related Posts

See More



Response Number 6
Name: samitk
Date: July 16, 2003 at 06:59:31 Pacific
Reply:

This is great - does the job..

Let me try to get more out of this..

Here is the bigger pisture..

I have a file test.dat which contains

Test.dat
--------
This directory contains file.dat which is very useful.

I want to get list of files from the current directory and replace the word file.dat with the list of files separated by comma

So if my directory has:
file1.dat
file2.dat
file3.dat

My end result of test.dat should be

test.dat
--------
This directory contains file1.dat,file2.dat,file3.dat which is very useful.

How can this be done?

The way I think it may work is:
1. awk to get list of files serataed by comma
2. sed to delete last comma
3. perl to search and replace

Is there a better/easier/another way to do it???


0

Response Number 7
Name: James Boothe
Date: July 16, 2003 at 15:16:12 Pacific
Reply:

There are a lot of ways to go about building the list of comma-delimited filenames. You said the files were in your current directory, and I assume that test.dat is there also. In the following, I went after the fileset file*dat. I use korn shell syntax here.

flist=$(ls file*dat|sed -e '{H;$!d;}' -e 'x;s/\n/,/g;s/^,//')

sed "s/file.dat/$flist/" test.dat > test.new


0

Response Number 8
Name: Sam
Date: July 16, 2003 at 17:21:57 Pacific
Reply:

Thanks a ton - does the job for me

Appreciate ur help..


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Delete last character in a file

Last character in a string www.computing.net/answers/unix/last-character-in-a-string/3979.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

Remove Last Word in File www.computing.net/answers/unix/remove-last-word-in-file/7535.html