AWK is awesome!
This commit is contained in:
parent
f00781edb4
commit
77dd6b1d66
1 changed files with 35 additions and 19 deletions
50
bin/t2s
50
bin/t2s
|
|
@ -1,26 +1,42 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
import sys
|
awk -v n_spaces="$1" '
|
||||||
|
|
||||||
DEFAULT_N_SPACES = 4
|
BEGIN {
|
||||||
TAB, SPACE = ' ', ' '
|
tab = " "
|
||||||
|
space = " "
|
||||||
|
|
||||||
try:
|
if (n_spaces < 1) {
|
||||||
n_spaces = int(sys.argv[1])
|
n_spaces = 4
|
||||||
except:
|
}
|
||||||
n_spaces = DEFAULT_N_SPACES
|
|
||||||
|
|
||||||
if n_spaces < 1:
|
spaces_per_tab = ""
|
||||||
n_spaces = DEFAULT_N_SPACES
|
for (i = 1; i <= n_spaces; i += 1) {
|
||||||
|
spaces_per_tab = spaces_per_tab space
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for line in sys.stdin:
|
function spaces_for_n_tabs(n) {
|
||||||
line = line.rstrip()
|
spaces = ""
|
||||||
|
while (n--) {
|
||||||
|
spaces = spaces spaces_per_tab
|
||||||
|
}
|
||||||
|
return spaces
|
||||||
|
}
|
||||||
|
|
||||||
|
function tabs_to_spaces(line) {
|
||||||
n_tabs = 0
|
n_tabs = 0
|
||||||
|
for (i = 1; i <= length(line); i += 1) {
|
||||||
for ch in line:
|
if (substr(line, i, 1) != tab) {
|
||||||
if ch != TAB:
|
|
||||||
break
|
break
|
||||||
|
}
|
||||||
n_tabs += 1
|
n_tabs += 1
|
||||||
|
}
|
||||||
|
return spaces_for_n_tabs(n_tabs) substr(line, i)
|
||||||
|
}
|
||||||
|
|
||||||
spaces = SPACE * n_spaces * n_tabs
|
{
|
||||||
print(spaces, line[n_tabs:], sep='')
|
print tabs_to_spaces($0)
|
||||||
|
}
|
||||||
|
|
||||||
|
'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue