Trim trailing spaces from file

I have a file like this.

hari,corporationbank,2234356,syndicate                                      
ravi,indian bank,4567900000000,indianbank,accese                
raju,statebank of hyderabad,565866666666666,pause   

Here each record has different record length and there are blank spaces
after each record. How to remove the blank spaces after each record.

awk '{ sub(/ *$/, "") } 1' inputfile > outputfile

Welcome kshari8888,

It's not the best title, so might I suggest "Trim trailing spaces from file" or such might be better? Can you edit this or do you need a moderator/administrator to?

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

2 Likes

Please, try the following:

perl -ple 's/\s+$//' infile > outfile

Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums

Done! Thanks.

With sed :

sed 's/[<b><t>]*$//' /path/to/input > output.file

Replace "<b>" and "<t>" with a literal blank and tab character (i used this just to make it visible).

I hope this helps.

bakunin

Another way to write that that is visually unambiguous is:

sed 's/[[:blank:]]*$//' /path/to/input > output.file

But on a Solaris/SunOS system, to make this work, you might need to use /usr/xpg4/bin/sed .

Longhand using OSX 10.12.3, default bash terminal only, 'cat' and 'hexdump' for display only...

Last login: Tue Feb 28 21:13:22 on ttys000
AMIGA:amiga~> echo 'hari,corporationbank,2234356,syndicate                                      
> ravi,indian bank,4567900000000,indianbank,accese                
> raju,statebank of hyderabad,565866666666666,pause' > /tmp/txt
AMIGA:amiga~> while read -r LINE; do echo $LINE; done < /tmp/txt > /tmp/text
AMIGA:amiga~> # Use hexdump to show removal of trailing spaces...
AMIGA:amiga~> hexdump -C /tmp/text
00000000  68 61 72 69 2c 63 6f 72  70 6f 72 61 74 69 6f 6e  |hari,corporation|
00000010  62 61 6e 6b 2c 32 32 33  34 33 35 36 2c 73 79 6e  |bank,2234356,syn|
00000020  64 69 63 61 74 65 0a 72  61 76 69 2c 69 6e 64 69  |dicate.ravi,indi|
00000030  61 6e 20 62 61 6e 6b 2c  34 35 36 37 39 30 30 30  |an bank,45679000|
00000040  30 30 30 30 30 2c 69 6e  64 69 61 6e 62 61 6e 6b  |00000,indianbank|
00000050  2c 61 63 63 65 73 65 0a  72 61 6a 75 2c 73 74 61  |,accese.raju,sta|
00000060  74 65 62 61 6e 6b 20 6f  66 20 68 79 64 65 72 61  |tebank of hydera|
00000070  62 61 64 2c 35 36 35 38  36 36 36 36 36 36 36 36  |bad,565866666666|
00000080  36 36 36 2c 70 61 75 73  65 0a                    |666,pause.|
0000008a
AMIGA:amiga~> cat /tmp/text
hari,corporationbank,2234356,syndicate
ravi,indian bank,4567900000000,indianbank,accese
raju,statebank of hyderabad,565866666666666,pause
AMIGA:amiga~> _

You have an unquoted variable in a command argument!
The shell does much more with it than removing the trailing spaces. It also removes leading spaces and replaces inter-whitespace by a single space character. And worse, it evaluates glob patterns like * i.e. substitutes them by matching filenames in the current directory.
The latter you can prevent with set -f

(
# a sub shell limits the scope of the following set command
set -f # disable glob expansion in command arguments
while read line
do
  printf "%s\n" $line
done < infile > outfile
)

General rule: in command arguments never have an unquoted variable, unless you have full control over its contents!
Note that echo and [ ] are commands!