I have a large collection of non-binary files in one place. Some of them have shebangs and of those some have (for some inexplicably reasons) whitespaces in front of the shebangs. This includes empty lines and lines with only whitespaces!
Example 1:
#!/usr/bin/env foo bar
Example 2:
#!/usr/bin/env foo bar
Example 3:
#! /bin/sh -e
Example 4:
______ / ____/___ ____ / /_ / __ \/ __ \ / __/ / /_/ / /_/ / /_/ \____/\____/ This is Foo News #324 with the tip of the day: Don't forget to put #!/bin/sh on top of your shell script files!
I would love for a solution for GNU-based (Linux) systems which would remove the leading white spaces of the file for example 1 and 2, while leaving 3 and especially 4 alone (even if it includes something like a shebang inside it).
Example 1 and 2 would become:
#!/usr/bin/env foo bar
What I unsuccessfully tried so far:
As a first step trying to discern between examples 1-3 and 4:
grep -Pzo '^[ \t\n]+#! ?[ \w/.-]+'
Did not work because
grep: unescaped ^ or $ not supported with -Pz
.Using
awk
:awk 'BEGIN {ws_check=1} !/[ \t]+/ {ws_check=0} /#! ?[ \w/.-]+/,0 && ws_check { print }'
Would still a lot of work in order to detect example 4 but also to only print the parts of the left-trimmed line with the shebang but not trimming the rest.
sed '1s/^[ \t]*#//' filename
[ \t]*
matches white spaces and tabs. I shouldn't have included the#
there. So thesed '1s/^[ \t]*//
should work as expected.