Removing tab spaces at the end of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing tab spaces at the end of each line
# 1  
Old 08-31-2015
Removing tab spaces at the end of each line

I have a file which contains the data lines like below.I want to remove the tab spaces at the end of each line.I have tried with the command
Code:
sed 's/[ \t]\+$//' file

.but it does not work.Can anyone help me on this?

Code:
15022   15022   15022   15022   15022   15022
15023   15023   15023   15023   15023   15023
15074   15074   15074   15074   15074
15079   15079   15079   15079   15079
15080   15080   15080   15080
15113   15113   15113   15113
15118   15118   15118
15119   15119   15119
15172   15172
15173   15173
15174
15175
15176
15401   15401   15401   15401   15401   15401
15402   15402   15402   15402   15402   15402


Last edited by Don Cragun; 08-31-2015 at 04:58 AM.. Reason: Add CODE tags.
# 2  
Old 08-31-2015
Please use code tags as required by forum rules!

Well, seems to depend on the system that you use. It works for me as is on my linux system. On FreeBSD, this sed -r 's/[ ]+$//' file works, i.e. it uses the -r option to switch to EREs, and it needs an explicit <TAB> char inserted on the (bash) command line by Ctrl-V <TAB>.
# 3  
Old 08-31-2015
In addition to what RudiC suggested, the following should work on most systems:
Code:
sed 's/[[:space:]]*$//' file

If you want to it on a Solaris/SunOS system, use /usr/xpg4/bin/sed instead of sed.

Note, however, that the sample input you supplied does not contain any trailing spaces or tabs on any of the lines.
# 4  
Old 08-31-2015
Thanks both of you for answering my question. I am using PuTTY (which is a free implementation of Telnet and SSH for Windows and Unix platforms).
I have tried the commands which you both suggested but it did not work.I noticed one thing that when i use the commands
Code:
sed 's/[ \t]\+$//' file

and
Code:
sed 's/[[:space:]]*$//' file

the tab spaces at the end of the lines containing 6 fields are removed but not at the end of the lines containing less than 6 fields.
I mean to say when i use the above commands the tab space at the end of the line below is removed
Code:
15022   15022   15022   15022   15022   15022

and the tab space remained same at the end for these lines
Code:
15113   15113   15113   15113
15118   15118   15118
15119   15119   15119
15172   15172
15173   15173
15174
15175

Can you please look into this once?

---------- Post updated at 04:53 AM ---------- Previous update was at 04:50 AM ----------

One more thing i would like to add here,as Don said there are no trailing spaces in my data lines.and there is 1 tab space and 1 blank between each fields which i want to keep it as it is.and only thing is i want to remove the tab spaces at the end of each line even though the lines contains 6 fields or less than 6 fields.
# 5  
Old 08-31-2015
Can't possibly be. Both sed solutions look just for the end-of-line and do not consider the field count. Even if there are more than one <TAB> due to missing fields, they are matched by the + sign in the regex and thus removed.
Please make sure there's no DOS line terminator (shown as \r, ^M or 0x0D)!
# 6  
Old 08-31-2015
Thanks for replying Rudi. and i can not see any DOS line terminators in my file.
# 7  
Old 08-31-2015
So - ...?
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 catch a two word keyword which may contain a new line(may include spaces or tab) in it?

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it. for example there is a file a.txt. $more a.txt create view as (select from ......... .......... ( select .... ( select ...... .. select only no ((( number ( select end (12 Replies)
Discussion started by: neelmani
12 Replies

2. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

3. UNIX for Dummies Questions & Answers

Removing empty lines at the end of a Tab-delimited file

I'm trying to remove all of the empty lines at the end of a Tab delimited file. They have no data just tabs. I've tried may things, here are a couple: sed /^\t.\t/d File1 > File2 sed /^\t{44}/d File1 > File2 What am I missing? (9 Replies)
Discussion started by: SirHenry1
9 Replies

4. Shell Programming and Scripting

append end of line with 8 spaces

child_amt=$amount prev_line="$prev_line $child_amt" i am getting the result like this 21234567890001343 000001004OLFXXX029100020091112 0000060 but i want 8 spaces between the eg: 21234567890001343 000001004OLFXXX029100020091112 0000060 how can i do this in .ksh (1 Reply)
Discussion started by: kshuser
1 Replies

5. Shell Programming and Scripting

Removing spaces in a line

Hi All, I have a line like this " field1;field2;field3 " (single space after and before double quotes). Now i have to remove these single space . Kindly help me. Thanks in advance (2 Replies)
Discussion started by: krishna_gnv
2 Replies

6. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

7. Shell Programming and Scripting

Replace spaces between strings in a line with tab

Hi All I am having problem in substitution of any number of spaces, or a combination of space and tab in between strings in the lines of text file. Is there any way out in Perl? Please help me. e.g., Say the input is in the following format:- XX yyy zzz... (1 Reply)
Discussion started by: my_Perl
1 Replies

8. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

9. Shell Programming and Scripting

to see space, tab, end of the line chracters

what can I use ?? In vi, I can use :set list <-- and see end of line $.. or use cat -A but I am wondering if there is command or program that allows me to see all the hidden characters( space, tab and etc) Please help thanks. (3 Replies)
Discussion started by: convenientstore
3 Replies

10. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies
Login or Register to Ask a Question