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

Delete last character in a file

Reply to Message Icon

Original Message
Name: Sam
Date: July 15, 2003 at 17:52:31 Pacific
Subject: Delete last character in a file
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



Report Offensive Message For Removal


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

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


Report Offensive Follow Up For Removal

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

How about:

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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.


Report Offensive Follow Up For Removal


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

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???


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

Thanks a ton - does the job for me

Appreciate ur help..


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Delete last character in a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge