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
1
  • What have you tried to so far? You could probably use grep in some way I think.
    – Seth
    Commented Dec 27, 2016 at 10:14

2 Answers 2

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

1
  • You're welcome! If this answer solved your problem, please accept it by clicking on the check mark.
    – Farahmand
    Commented Dec 27, 2016 at 11:57
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .