Trim trailing spaces from file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Trim trailing spaces from file
# 8  
Old 02-28-2017
Longhand using OSX 10.12.3, default bash terminal only, 'cat' and 'hexdump' for display only...
Code:
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~> _

# 9  
Old 02-28-2017
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
Code:
(
# 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!
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