SAC
SAC

Reputation: 243

how can be remove all white spaces fron begin of line using tr in bash

i know the sed syntax for removing whitespaces from begin of line, can it be done using tr command ?

my file is file.txt like .

    th ht djs
        ncdbh jdhbc ncbs 
     hi nc hdn 
 mued ndc 

after removing all whitespaces from begin we get file like

th ht djs
ncdbh jdhbc ncbs 
hi nc hdn 
mued ndc 

Upvotes: 1

Views: 852

Answers (4)

chiastic-security
chiastic-security

Reputation: 20520

Your question text asks about tr, but the title is more accommodating, and just asks about doing it without sed.

It's impossible with tr, because it just considers characters one by one, without regard for context.

For a non-sed answer, you might try

awk '{$1=$1}1' inputfile

Upvotes: 2

midori
midori

Reputation: 4837

The Answer to your quesition is no. tr doesn't support deletion of specific patterns using regex. The purpose of tr is different - man tr

If you want to use sed:

sed -i -e 's/^[ \t]*//' file.txt

Upvotes: 2

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

while read line; do echo "${line}"; done < File

Sample:

AMD$ while read line; do echo "${line}"; done < File
th ht djs
ncdbh jdhbc ncbs
hi nc hdn
mued ndc

Upvotes: 2

Arnab Nandy
Arnab Nandy

Reputation: 6702

Try this

sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' input.lst > output.lst

with tr

tr -d ' \t\n\r\f' <input.lst >output.lst

Upvotes: 2

Related Questions