Sponsored Content
Top Forums UNIX for Dummies Questions & Answers [Solved] Help with using tr - Removing white spaces Post 302560787 by newbie_01 on Saturday 1st of October 2011 07:55:04 AM
Old 10-01-2011
[Solved] Help with using tr - Removing white spaces

Hi,

I have a file that contains whitespaces with spaces and spaces and tabs on each line and am wanting to remove the whitespaces. My version of sed is one that does not recognize \t etc.

The sed and awk one-liners below that I found via Google both does not work.
Quote:
sed:
sed 's/^[ \t]*//;s/[ \t]*$//'

awk:
awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}'

So my next best option is to use tr, am using tr -d '[:space:]' < in > out and while that one works, I am unfortunately losing the line endings.

When I run the tr command

The input below:

Code:
 
server01  :/mnt/arch  :20  :15 :40 :85 :N :
server02  :/mnt/arch2 :32  :15 :40 :85 :N :

Becomes:

Code:
server01:/mnt/volarch:20:15:40:85:N:server02:/mnt/arch2:32:15:40:85:N:

Preferably, I want it to be as below:

Code:
 
server01:/mnt/volarch:20:15:40:85:N:
server02:/mnt/arch2:32:15:40:85:N:

At the moment since I don't know how to get around this, am using a while read loop that reads each line and running tr -d '[:space:]' on each line. I believe this is a bit of an overkill.

Can anyone advise whether I should be able to get the desired result using tr instead of the while loop?

Thanks in advance.

Last edited by vbe; 10-28-2011 at 05:18 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies

2. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

3. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

4. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies

5. Shell Programming and Scripting

ksh: removing all white spaces

'String' file contains the following contents, D11, D31, D92, D29, D24, using ksh, I want to remove all white spaces between characters no matter how long the string is. Would you please give me some help? (1 Reply)
Discussion started by: yoonius
1 Replies

6. Solaris

removing special characters, white spaces from a field in a file

what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code:: Code: awk -F":"... (2 Replies)
Discussion started by: priyanka3006
2 Replies

7. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

8. Shell Programming and Scripting

[Solved] Remove White spaces from teh beginnning from a particular row

Hi All, $sed -n "58p" sample 1222017 --- Its has to display like: 1222017 (5 Replies)
Discussion started by: Anamica
5 Replies

9. Shell Programming and Scripting

Removing blank/white spaces and special characters

Hello All , 1. I am trying to do a task where I need to remove Blank spaces from my file , I am usingawk '{$1=$1}{print}' file>file1Input :- ;05/12/1990 ;31/03/2014 ; Output:- ;05/12/1990 ;31/03/2014 ;This command is not removing all spaces from... (6 Replies)
Discussion started by: himanshu sood
6 Replies

10. Shell Programming and Scripting

Removing White spaces from a huge file

I am trying to remove whitespaces from a file containing sample data as: 457 <EOFD> Mar 1 2007 12:00:00:000AM <EOFD> Mar 31 2007 12:00:00:000AM <EOFD> system <EORD> 458 <EOFD> Mar 1 2007 12:00:00:000AM<EOFD>agf <EOFD> Apr 20 2007 9:10:56:036PM <EOFD> prodiws<EORD> . Basically these... (11 Replies)
Discussion started by: amvip
11 Replies
AUTO.MASTER(5)							File Formats Manual						    AUTO.MASTER(5)

NAME
/etc/auto.master - Master Map for automounter DESCRIPTION
The auto.master map is consulted when the autofs(8) script is invoked to set up the necessary mount points for the automounter. Each line in this file describes a mount point and points to another file describing the file systems to be mounted under this mountpoint. The access to those maps is governed by a key. Access to an automounted file system is customarily done using the path scheme: /mountpoint/key/path/file, where the mountpoint will be listed in the auto.master configuration file. The key is matched in the map file pointed to by the master map (See autofs(5)). The path and the file are referring to the file on the file system mounted. FORMAT
The file has three fields separated by an arbitrary number of blanks or tabs. Lines beginning with # are comments. The first field is the mount point. Second field is the map file to be consulted for this mount-point. This field is of the form maptype:mapname, where maptype is one of the supported map types (file, program, yp, nisplus, hesiod, userdir, ldap), and mapname is the name of the map. The third field is optional and can contain options to+ be applied to all entries in the map. Options are cumulative, which is a difference from the behav- ior of the SunOS automounter. The format of the map file and the options are described in autofs(5). EXAMPLE
/home /etc/auto.home /misc /etc/auto.misc /mnt yp:mnt.map This will generate three mountpoints /home, /misc, and /mnt. All accesses to /home will lead to the consultation of the map in /etc/auto.home, all accesses to /misc will consult the map in /etc/auto.misc, and all accesses to /mnt will consult the NIS map mnt.map. SEE ALSO
automount(8), autofs(5), autofs(8). AUTHOR
This manual page was written by Christoph Lameter <chris@waterf.org>, for the Debian GNU/Linux system. Edited by <hpa@transmeta.com>. 19 Jun 2000 AUTO.MASTER(5)
All times are GMT -4. The time now is 05:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy