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

sed 's/[ \t]\+$//' file

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

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

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

In addition to what RudiC suggested, the following should work on most systems:

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.

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

sed 's/[ \t]\+$//' file

and

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

15022   15022   15022   15022   15022   15022

and the tab space remained same at the end for these lines

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.

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 )!

Thanks for replying Rudi. and i can not see any DOS line terminators in my file.

So - ...?

The way you are describing your file and the results you say you are getting do not make sense to us.

Please show us the exact output you get from the commands:

uname -a
printf '%s\n' "$SHELL"
od -bc file

and

sed 's/[[:space:]]*$//' file | od -bc

Thanks for replying Don.Sorry for late reply. I have executed the commands you suggested and here is the output from that.

uname -a
SunOS cvgsolsasp001 5.10 Generic_142900-06 sun4u sparc SUNW,SPARC-Enterprise
printf '%s\n' "$SHELL"
/bin/csh
od -bc headrec

0000060 061 065 060 062 062 011 061 065 060 062 062 011 061 065 060 062
           1   5   0   2   2  \t   1   5   0   2   2  \t   1   5   0   2
0000100 062 011 061 065 060 062 062 011 061 065 060 062 062 012 061 065
           2  \t   1   5   0   2   2  \t   1   5   0   2   2  \n   1   5
sed 's/[[:space:]]*$//' headrec | od -bc

0000060 061 065 060 062 062 011 061 065 060 062 062 011 061 065 060 062
           1   5   0   2   2  \t   1   5   0   2   2  \t   1   5   0   2
0000100 062 011 061 065 060 062 062 011 061 065 060 062 062 012 061 065
           2  \t   1   5   0   2   2  \t   1   5   0   2   2  \n   1   5

Kindly look into this once

I asked you to show us results of running a set of commands on your sample input file. Instead of that, you showed us the results of running those commands on bytes 48 through 80 of that file showing us a single complete line of data (with no spaces at all and no trailing tabs) and two characters from the start of the next line (with no spaces and no tabs at all). So, I have no more idea now what you're trying to do than I did before.

But, now that we know you're using a Solaris system, try:

/usr/xpg4/bin/sed 's/[[:space:]]*$//' file

If this still doesn't work, you are going to have to give us the details about your file that I requested before AND a MUCH clearer explanation of what output you are trying to produce.

Thanks Don. I tried the command but it did not work. Below is the data in my file.There are thousands of lines like this in the original file.But i have taken a few here.
All these lines are having tab spaces in between the fields and some lines are having tab spaces at the end also.
The lines 1,2 and 11,12 are not having tab spaces at the end. The 3,4 lines are having 1 tab space at the end.The 5,6 lines are having 2 tabs paces at the end
and lines 7,8 are having 4 tab spaces at the end and lines 9,10 are having 5 tab spaces at the end.
I want to keep the tab spaces as it is in between fields in all these lines.But I want to remove the tab spaces at the end from the lines 3,4,5,6,7,8,9,10.
for ex:- If i give

shift$

(command for moving the cursor to the end of the line) in the line 7,then cursor should be highlighting the last digit in the line which is '2'(15172 15172)

      1 15022	15022	15022	15022	15022	15022
      2 15023	15023	15023	15023	15023	15023
      3 15074	15074	15074	15074	15074	
      4 15075	15075	15075	15075	15075	
      5 15080	15080	15080	15080		
      6 15113	15113	15113	15113		
      7 15172	15172				
      8 15173	15173				
      9 15174					
     10 15175					
     11 15404	15404	15404	15404	15404	15404
     12 15405	15405	15405	15405	15405	15405

Now below is the output which i want.The lines 3,4,5,6,7,8,9,10 should not contain the tab spaces at the end.

      1 15022	15022	15022	15022	15022	15022
      2 15023	15023	15023	15023	15023	15023
      3 15074	15074	15074	15074	15074
      4 15075	15075	15075	15075	15075
      5 15080	15080	15080	15080
      6 15113	15113	15113	15113
      7 15172	15172
      8 15173	15173
      9 15174
     10 15175
     11 15404	15404	15404	15404	15404	15404
     12 15405	15405	15405	15405	15405	15405

Hope this time i am clear on my need.Kindly suggest me on this.
Thanks in advance.

Got Perl?

perl -ple 's/\s+$//' am24.file
1 Like

I'm a bit surprised. Applied to you sample file in post#11, the solutions in post#1 and #2 do exactly what you want.

sed -n '9p' file | hd
00000000  20 20 20 20 20 20 39 20  31 35 31 37 34 09 09 09  |      9 15174...|
00000010  09 09 0a                                          |...|

sed -n '9 s/[ \t]\+$//p' file | hd
00000000  20 20 20 20 20 20 39 20  31 35 31 37 34 0a 

All <TAB>s are removed!

1 Like

Thank you Rudi for your reply. The both commands did not work for me when i used them on my file. I will try to see if i can get any other way.
Thanks again for your help.

---------- Post updated at 04:37 AM ---------- Previous update was at 04:11 AM ----------

Thanks Aia for your reply. When i am using 'Perl' , I am getting a warning message "Setting locale failed". I think my system does not have the supporting locale settings to use this "perl".

We have made several suggestions. We have tested them on the sample data you have provided and they do exactly what you have asked them to do.

You say they do not work. But, that doesn't help. The output you showed us in post #9 in this thread shows that the output produced is exactly what you asked for. Please pick a few lines of your input file that are NOT processed correctly and copy them into a file named bad_input . Then run the commands:

cat bad_input
od -bc < bad_input
/usr/xpg4/bin/sed 's/[[:space:]]*$//' bad_input > bad_output
od -bc < bad_output

and show us the exact, complete output (not just some 32 character sample) in CODE tags. It should be obvious by now that if we can't see what is going wrong, we can't help.

1 Like

Thanks for the reply Don. I will work on this and will get back to you.

Hi Don,

I have tried the command you suggested

/usr/xpg4/bin/sed 's/[[:space:]]*$//' bad_input > bad_output

and it worked fine now in my sample file and also in the original file.
Thank you so much for your help and suggestions.:slight_smile:

---------- Post updated at 07:36 AM ---------- Previous update was at 07:35 AM ----------

Also Thanks to Rudi and Aia :slight_smile: