Trim trailing spaces from file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Trim trailing spaces from file
# 1  
Old 02-27-2017
Trim trailing spaces from file

I have a file like this.
Code:
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.

Last edited by rbatte1; 02-27-2017 at 11:56 AM.. Reason: Added CODE tags
# 2  
Old 02-27-2017
Code:
awk '{ sub(/ *$/, "") } 1' inputfile > outputfile

# 3  
Old 02-27-2017
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
These 2 Users Gave Thanks to rbatte1 For This Post:
# 4  
Old 02-27-2017
Quote:
Originally Posted by kshari8888
I have a file like this.
Code:
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.
Please, try the following:
Code:
perl -ple 's/\s+$//' infile > outfile

# 5  
Old 02-27-2017
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

Quote:
Originally Posted by rbatte1
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?
Done! Thanks.
# 6  
Old 02-28-2017
Quote:
Originally Posted by kshari8888
each record has different record length and there are blank spaces after each record. How to remove the blank spaces after each record.
With sed:

Code:
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
# 7  
Old 02-28-2017
Quote:
Originally Posted by bakunin
With sed:

Code:
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove leading and trailing spaces from a file

Hi, I am trying to remove leading and trailing spaces from a file using awk but somehow I have not been able to do it. Here is the data that I want to trim. 07/12/2017 15:55:00 |entinfdev |AD ping Time ms | .474| 1.41| .581|green |flat... (9 Replies)
Discussion started by: svajhala
9 Replies

2. Shell Programming and Scripting

Trim spaces

All, i am comparing the output of one command to a numberic if ] but my problem is the output of follwoing is but but has some leading columns. I don't have any problme in LINUX and HP-UX. But only in AIX i am getting the leading spaces. I have developed my script on LINUX but when... (4 Replies)
Discussion started by: rcc50886
4 Replies

3. Shell Programming and Scripting

How to add trailing spaces to have file with lines of the same length?

I have textfile (source.txt) with different length of lines in it. Can anybody help to compose a script under bash which would add suitable number of trailing spaces to the end of each line so that after the processing the each line would have the same (let's say 100 char) length? Output can be... (6 Replies)
Discussion started by: sameucho
6 Replies

4. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

5. Shell Programming and Scripting

trim spaces in a file

Hi, I'm new to shell programming. Need some help in the following requirement: I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor .35~ 765.76~ anh reid~ kishna kerry Now each row in the file has value for 4 columns with "~" as... (7 Replies)
Discussion started by: badrimohanty
7 Replies

6. UNIX for Advanced & Expert Users

TRIM spaces in shell

am get a value like ' 15' in a variable what is the easiest method i can follow to strip 15 out (3 Replies)
Discussion started by: anumkoshy
3 Replies

7. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

8. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

9. UNIX for Dummies Questions & Answers

Adding Trailing Spaces to a file

I have a text file which is not fixed width. I want to put trailing spaces to each line and make it a 100 byte fixed width file. Can someone please help me as soon as possible? Thanks, Denis (1 Reply)
Discussion started by: 222001459
1 Replies

10. UNIX for Dummies Questions & Answers

removing trailing spaces of a particular column in a file

Hi, I am currently confused. Suppose I have a file something like the one below. 4299|raj Telecommunications|12||||| 4302|anjali International Ltd.|86|ritchie||dong|(000)2890 9993 |(222)4881 3689 4305|フィデュシアリ・ト-スト・インター...ショ...ル投資顧問株式会社 |112||||01-9211-1931 |08-3677-1985 Now... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question