remove spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove spaces
# 1  
Old 07-08-2012
remove spaces

Hi folks,
I need to remove spaces at the end of each line in a *.txt file. it looks like this

word 1[space]
word 2[space]
.
.
.
word n[space]

i found some sed commands but any of them didnt work so far

thank you for your posts
# 2  
Old 07-08-2012
for a text file named test.txt please try below:

cat test.txt|sed 's/ $//g'
This User Gave Thanks to krg.sati For This Post:
# 3  
Old 07-08-2012
Quote:
Originally Posted by krg.sati
cat test.txt|sed 's/ $//g'
It doesn't hurt anything, but the global flag, g, in that substitution command is pointless. There can be only one match when the pattern is anchored.

Regards,
Alister
# 4  
Old 07-08-2012
Thank you for your response, but it just didnt work for me again. the spaces are still at the end of each line.
# 5  
Old 07-08-2012
Quote:
Originally Posted by Jimmy7
Thank you for your response, but it just didnt work for me again. the spaces are still at the end of each line.
Please, for future reference, "it just didnt work", is an unhelpful response. If you don't provide any feedback, it's unlikely that people will be able to help you out. What operating system and shell are you using? Exactly what command did you run? Did it generate an error message? Was there any output?

If I had to guess, you are expecting sed to modify the file when what sed does is read the file and write the changes its made to standard output, not to the original file itself. If that guess is correct, the following should work (back up your file first, just in case, or work with a copy of it):
Code:
printf '%s\n' '1,$s/ $//' w q | ed -s file

If that didn't work, are you sure that those are spaces and not some other blank character? You can verify using hd/hexdump/od. If you don't understand the output of those commands, simply paste them here. Don't forget to use code tags for code and data (to preserve formatting).

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 07-08-2012
Quote:
Originally Posted by alister
Please, for future reference, "it just didnt work", is an unhelpful response. If you don't provide any feedback, it's unlikely that people will be able to help you out. What operating system and shell are you using? Exactly what command did you run? Did it generate an error message? Was there any output?

If I had to guess, you are expecting sed to modify the file when what sed does is read the file and write the changes its made to standard output, not to the original file itself. If that guess is correct, the following should work (back up your file first, just in case, or work with a copy of it):
Code:
printf '%s\n' '1,$s/ $//' w q | ed -s file

If that didn't work, are you sure that those are spaces and not some other blank character? You can verify using hd/hexdump/od. If you don't understand the output of those commands, simply paste them here. Don't forget to use code tags for code and data (to preserve formatting).

Regards,
Alister
When I created a .txt file whit words and spaces and then used
Code:
cat /root/input.txt | sed 's/ *$//g' > output.txt

it removed all spaces at the end of each line
but when I apply this to my file its not working. when I used the command
Code:
 cat /root/myfile.txt | sed 's/ *$/A/g' > myfile1.txt

I got this:
word1[space]
A
word2[space]
A

...and so on
but it supposed to be:
word1A
word2A...

I suppose there's something wrong with this file. I merged 2 txt files and created one I want to remove spaces there now
there are no error messages, I always get an output file, but with no result
I didnt get it how can I verify whether there are some other blank characters
cant explain you better, sorry,I am a noob with poor eng.

---------- Post updated at 08:56 PM ---------- Previous update was at 08:25 PM ----------

I changed encoding of the whole document to ISO 8859-15 and performed
Code:
 cat input.txt | sed 's/ *$//g' > output.txt

and it finally worked
Thank You again for your responses
# 7  
Old 07-08-2012
Thank you for reporting back with your solution. Welcome to the forums.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove spaces on a line?

Hi, suppose I have the following data: albert music=top40 age=20 bob music=punk rock age=25 candy music=r n b age=22 dave music=mozart or bach only age=30 I want to extract and manipulate the music column but it's got spaces in it. How can I substitute the space with an underscore... (2 Replies)
Discussion started by: almonds
2 Replies

2. Shell Programming and Scripting

Remove spaces in a file

Hi friends, I have a file1.txt 1 | a | 4757634 | jund jdkj | erhyj 2 | a | 4757634 | jnd jdkj | rhje hjrhwj i have used tr -d '\040' to remove the spcaes output file cat file1.txt | tr -d '\040' 1|a|4757634|jundjdkj|erhyj... (5 Replies)
Discussion started by: i150371485
5 Replies

3. Linux

How to remove spaces in the file?

hiii i have a file that contains spaces in the begining of a file till the middle the from there the txt would appear. hw can i remove those spaces and bring the text to the begining portion file1 text starts from here (12 Replies)
Discussion started by: anurupa777
12 Replies

4. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

5. Shell Programming and Scripting

Not able to remove leading spaces

Hi Experts, In a file tht i copied from the web , i am not able to remove the leading white spaces. I tried the below , none of them working . I opened the file through vi to check for the special characters if any , but no such characters found. Your advice will be greatly appreciated. sed... (5 Replies)
Discussion started by: panyam
5 Replies

6. Shell Programming and Scripting

Remove Spaces

Hi All, I have a comma seperated file. I wanna remove the spaces from column 2. I mean i don't wanna remove the spaces those are presnt in column 1. ex: emp name, emp no, salary abc k, abc 11, 1000 00 bhk s, bhk 22, 2000 00 the output should be: emp name, emp no, salary abc k, abc11,... (4 Replies)
Discussion started by: javeed7
4 Replies

7. Shell Programming and Scripting

Remove spaces before a delimiter

Hi All, I need to modify a script to remove spaces from a csv file. The csv file is delimited by the '~' character and I need to remove the spaces which appear before this character. i.e Sample input: LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011 Desired... (5 Replies)
Discussion started by: SRyan84
5 Replies

8. Shell Programming and Scripting

Remove spaces from columns

I have a file with only one field something like this : 213.45 220.65 240.47 345.67 320.45 I want to remove all spaces in between. Is there any unix command for that ?Thanks in advance.. (2 Replies)
Discussion started by: jacks
2 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

10. Shell Programming and Scripting

need help to remove spaces from first column

Hi, Here is sample data which I have: column#1 column#2 column#3 001A 50005 ROCKER ADJ 00010000100018UTIRR 001A 50020 CRANKSHAFT 0003445ES 001A 52201 SPARKPLUG ... (4 Replies)
Discussion started by: tayyabq8
4 Replies
Login or Register to Ask a Question