Identifying leading line space - Shell script

0

I have a sample file like below. There are leading whitespaces. Is there a way to detect them and print the exact line number which contains the whitespace using a shell script?

test space at back 
 test space at front
TAB at end  
    TAB at front

Daz

Posted 2016-12-27T10:04:26.303

Reputation: 63

What have you tried to so far? You could probably use grep in some way I think. – Seth – 2016-12-27T10:14:52.917

Answers

1

You can use something like this:

awk '/^[ \t]+/ {printf NR ", "}' test.txt

The above command will print the line numbers which have leading space(s) or tab(s) of the file test.txt

Farahmand

Posted 2016-12-27T10:04:26.303

Reputation: 111

You're welcome! If this answer solved your problem, please accept it by clicking on the check mark. – Farahmand – 2016-12-27T11:57:17.330

1

A version which would use the same regular expression as the one supplied by Farahmand but using grep instead of awk could look like this:

grep -n -E $'^[ \t]+' test.txt

The $ is necessary to escape/interpret the \t.

Seth

Posted 2016-12-27T10:04:26.303

Reputation: 7 657