Last active
November 4, 2017 19:30
-
-
Save sagax/6fbf22dca7a6d5b6d00180386818781f to your computer and use it in GitHub Desktop.
to overwrite the line in many files with awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
# GNU Awk 4.1.4, API: 1.1 | |
# GNU bash, версия 4.4.12(1)-release (i586-suse-linux-gnu) | |
# | |
# copy linerewriter.awk to $HOME/bin/ | |
# added chmod +x $HOME/bin/linerewriter | |
# run linerewriter | |
# | |
# linerewriter target_dir_with_files Foo Bar | |
# Foo will be rewritten as Bar | |
function blue(s) { return "\033[1;37;1m" s "\033[0m" } | |
function listdir( path) { | |
cmd = "realpath " path "/*" | |
while (cmd | getline filepath) { | |
ARGV[ARGC++] = filepath | |
} | |
close(cmd) | |
} | |
BEGIN { | |
length_argv = length(ARGV) | |
"pwd" | getline basepath | |
# files it's filepath array | |
if (length_argv != 4) { exit 0 } | |
path = ARGV[1] | |
to = ARGV[2] | |
replace = ARGV[3] | |
for (i=0; i<length(ARGV); i++) { | |
delete ARGV[i] | |
} | |
ARGC=0 | |
listdir(path) | |
} | |
{ | |
gsub(to, replace, $0) | |
files[FILENAME] = files[FILENAME] $0 "\n" | |
} | |
END { | |
for (file_path in files) { | |
print files[file_path] > file_path | |
sub(basepath "/", "", file_path) | |
print "replaced in: " blue(" " file_path " ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://asciinema.org/a/rjbw2J2Rg9o9PD7vU2kS20OtW
this version of the script is not stable