How to remove last character in a string read from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove last character in a string read from file
# 1  
Old 09-02-2010
How to remove last character in a string read from file

Hello,

The last character is a comma ,

I have tried the following:

Code:
sed -e 's/\,$//' filename-to-read

however - there are still commas at the end of each line...Smilie
# 2  
Old 09-02-2010
Hi.

You don't have to escape the comma (\,) although it seems to make no difference.

The sed code you are using will not write back to the file.

If your sed has the -i option use that, otherwise write to a temporary file, and then copy the new file back over the original.

To remove multiple comma's at the end of a line, use something like:

Code:
sed -e 's/,*$//' file2


Last edited by Scott; 09-02-2010 at 02:03 PM.. Reason: Removed second comma - not required
This User Gave Thanks to Scott For This Post:
# 3  
Old 09-02-2010
Quote:
Originally Posted by learning
Hello,

The last character is a comma ,

I have tried the following:

Code:
sed -e 's/\,$//' filename-to-read

however - there are still commas at the end of each line...Smilie
In the file or on the screen?
# 4  
Old 09-02-2010
Yep - that seems to have worked. Apparently my input has some spaces at the end so i had to execute it twice:

Code:
sed -e 's/ *$//g' file.out > no-space.out
sed -e 's/,*$//g' no-space.out

Thank you!

---------- Post updated at 11:54 AM ---------- Previous update was at 11:52 AM ----------

Quote:
Originally Posted by Franklin52
In the file or on the screen?
Was outputting to file. Ty.
# 5  
Old 09-02-2010
Hi.

You could probably do that in one step.

i.e.
Code:
sed -e 's/[, ]*$//g'

This User Gave Thanks to Scott For This Post:
# 6  
Old 09-03-2010
Quote:
Originally Posted by learning
Yep - that seems to have worked. Apparently my input has some spaces at the end so i had to execute it twice:

Code:
sed -e 's/ *$//g' file.out > no-space.out
sed -e 's/,*$//g' no-space.out

Thank you!

---------- Post updated at 11:54 AM ---------- Previous update was at 11:52 AM ----------



Was outputting to file. Ty.
Read this too.
http://www.grymoire.com/Unix/Sed.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove string between number and character

hello ! I have to remove string between a number and set of characters. For example, 35818 -stress - - -stress - - - - - - DB-3754 44412 caul kid notify DB-3747 54432 roberto -, notify DB-3725 55522 aws _ _int _ _classified 2_a _a 2_m _m 2_classified 2_search... (7 Replies)
Discussion started by: ManoharMa
7 Replies

2. Shell Programming and Scripting

gawk to remove last character in a line or string

I am outputting a line like this print $2 "/" $4The last character though is a ":" and I want to remove it. Is there any neat way to remove it? Or am I forced to do something like this: print $2 "/" substr($4, 1, length($4) - 1)Thanks. (6 Replies)
Discussion started by: benalt
6 Replies

3. Shell Programming and Scripting

Bash: How to remove the last character of a string?

In bash, how can one remove the last character of a string? In perl, the chop function would remove the last character. However, I do not know how to do the same job in bash. Many thanks in advance. (12 Replies)
Discussion started by: LessNux
12 Replies

4. Shell Programming and Scripting

remove the first and last character of a string

How can i remove the first and last character of strings like below: "^^^613*" "admt130" "^^^613*" "123456" "adg8484" "DQitYV09dh1C" Means i wanna remove the quotes(""). Please help (17 Replies)
Discussion started by: proactiveaditya
17 Replies

5. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

6. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

7. Shell Programming and Scripting

shell script to remove the last character(.) of a string

hi I have a list of words in a text file. these words are appended by "." at their end. They look something like this. word1. word2. word3. word4. word5. I need to remove the last character "." from all the words. The output must look something like this. word1 word2 word3... (7 Replies)
Discussion started by: ss3944
7 Replies

8. Shell Programming and Scripting

bash while read how to remove \n character

Hi, I've made a script to grep a file for i in `cat filename.txt` do strings ./binfile | grep "$i" 2>&1 > /dev/null done this works fine as long as in filename.txt i don't have any entries with spaces. But in my case i want to grep something with spaces like "lala tata" and... (3 Replies)
Discussion started by: papasj
3 Replies

9. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

10. Shell Programming and Scripting

read and assign each character from the string to a variable

How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you! (1 Reply)
Discussion started by: Tek-E
1 Replies
Login or Register to Ask a Question