PDA

View Full Version : how to remove last character of a string



finer recliner
March 6th, 2008, 02:19 AM
I have a file of many words that looks something like this:


word1
word2
word3
etc


each word is a different length, and they all seem to have a trailing space (or some sort of non-printable character). Whats the easiest way to strip the last character (the space) from each line?

stylishpants
March 6th, 2008, 02:34 AM
perl -ple 'chomp' /tmp/file

"chomp" only removes a single whitespace char if the string ends with one, so it's safer than 'chop', which always removes one char regardless of what it is.

mike_g
March 6th, 2008, 02:40 AM
Depend what sort of language you are using. But if you want to modify the file itself you will need to write it out again (for simplicity sake).

In C you could do something like: Read in each character of the file, keeping track of the character before it. Basically you only write a character before once you have read the character infront of it first. If the front character is a newline and the back character is some form of whitespace then don't write it to the file, otherwise write it.

It would be easier in a higher level languages like Java. Then you could just read lines in as strings, trim them, then write back to a file.

Can+~
March 6th, 2008, 02:57 AM
In C you could do something like: Read in each character of the file, keeping track of the character before it. Basically you only write a character before once you have read the character infront of it first. If the front character is a newline and the back character is some form of whitespace then don't write it to the file, otherwise write it.

In C you could, instead of looping for each character, using strlen (string.h) for each line and going backwards until you find the first non-space character and remove it.

I guess it would be even easier in python, as python comes with the OS and has all those fancy string functions.

imdano
March 6th, 2008, 03:04 AM
Python's .strip() method or Java's .trim() method would probably be much easier to use than C.

Can+~
March 6th, 2008, 03:27 AM
Python's .strip() method or Java's .trim() method would probably be much easier to use than C.

Uhm, python .strip() removes whitespaces (or any character specified on the parameters), he wants to remove a certain character at the end.

imdano
March 6th, 2008, 03:37 AM
It only removes leading or trailing whitespace (or leading/trailing characters of the person's choice). You can use rstrip() to only remove trailing (or lstrip() for leading). It seems like that would fit his needs here.

Can+~
March 6th, 2008, 03:50 AM
It only removes leading or trailing whitespace (or leading/trailing characters of the person's choice). You can use rstrip() to only remove trailing (or lstrip() for leading). It seems like that would fit his needs here.

Oh wait, I just re-read the initial post and yeah, I misunderstood. I thought he wanted to remove the "1, 2, 3" from the words. Duh.

Then it's a for line in file: line.rstrip() kind of thing.

slavik
March 6th, 2008, 04:01 AM
perl -ple 'chomp' /tmp/file

"chomp" only removes a single whitespace char if the string ends with one, so it's safer than 'chop', which always removes one char regardless of what it is.
the answer has been given. :)

finer recliner
March 6th, 2008, 04:19 AM
perl -ple 'chomp' /tmp/file

"chomp" only removes a single whitespace char if the string ends with one, so it's safer than 'chop', which always removes one char regardless of what it is.

i dont think the trailing character was a space (idk what it was), but using chop with your perl call did the trick.

thank you all for your suggestions!:guitar:

ghostdog74
March 6th, 2008, 04:51 AM
I have a file of many words that looks something like this:


word1
word2
word3
etc


each word is a different length, and they all seem to have a trailing space (or some sort of non-printable character). Whats the easiest way to strip the last character (the space) from each line?




awk '{sub(/ +$/,"",$0)}1' file > newfile

onbongos
April 3rd, 2008, 03:32 PM
you can strip any last character with just string[:-1]

TeckniX
April 16th, 2008, 11:12 PM
could you provide an example of how you'd use


string[:-1]


I'm trying to remove the * from an executable filename when doing an ls -l

ghostdog74
April 17th, 2008, 01:46 AM
could you provide an example of how you'd use


string[:-1]


I'm trying to remove the * from an executable filename when doing an ls -l

how does your file name look like ?

stylishpants
April 17th, 2008, 02:27 AM
could you provide an example of how you'd use


string[:-1]


I'm trying to remove the * from an executable filename when doing an ls -l

Maybe you don't need to remove the last char at all.
It's possible that you're getting the trailing * on some files because your "ls" command has the "-F" option, which appends a filetype classifier character to the filename.

This may be the case even if you are not explicitly providing the "-F" yourself, often ls gets aliased to something that has some useful default options.

Try using "\ls -l" (ls with a leading backslash) to get an unaliased ls, and see if that solves your issue.

mssever
April 17th, 2008, 04:47 AM
could you provide an example of how you'd use


string[:-1]
I'm trying to remove the * from an executable filename when doing an ls -l

You shouldn't be parsing the output of ls in the first place. ls is designed for human readers; it's impossible to reliably parse its output regardless of which options you pass to it because you can't always tell where filename boundries are. Remember, a filename can contain ANY whitespace character, including \n. Instead, use your language's facility for handling filenames. E.g.,
#Bash
for i in *; do
# do stuff
done

#Ruby
Dir.open(some_dir) do |dir|
dir.each do |file|
next if file =~ /^[.]+$/
# do stuff
end
end

Wybiral
April 17th, 2008, 06:51 PM
What language was this supposed to be in anyway?

Mickeysofine1972
April 17th, 2008, 09:15 PM
Indeed, tell us what language and we will be glad to help

MIke

ghostdog74
April 18th, 2008, 12:49 AM
he is doing a ls -l. I am certain that's not a shell command.

toxygen
May 14th, 2008, 10:51 PM
sed -ie 's/.$//' filename