Last active
September 9, 2022 00:30
-
-
Save kaushikgopal/17d1c9718110e9acc5cc7946f5fe0761 to your computer and use it in GitHub Desktop.
awk explainer example
This file contains hidden or 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
# General pattern | |
# | |
# awk ' | |
# BEGIN { a1; a2; a3; } | |
# <pattern> { a1; a2; a3; } | |
# END { a4; a6; } | |
# ' <filename> | |
# space - default file separator | |
# $0 - entire line | |
# $1 - word 1 | |
# NR - number of records (line number - starts at 1 | |
# NF - number of fields | |
awk ' | |
BEGIN { n = 0; } | |
{ | |
if ($0 = "---") { | |
if (n == 1) { | |
print "xxx"; | |
} else { | |
print; | |
} | |
n+=1; | |
} else { | |
print; | |
} | |
} | |
' <filename> |
This file contains hidden or 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
for f in *.md | |
awk -i inplace -v filename="$f" ' | |
BEGIN { n = 0; } | |
{ | |
if ($0 = "---") { | |
if (n == 1) { | |
sub(".md", "/", filename) | |
sub("-", "/", filename) | |
sub("-", "/", filename) | |
sub("-", "/", filename) | |
print "redirect_from:\n - /"filename; | |
print "xxx"; | |
} else { | |
print; | |
} | |
n+=1; | |
} else { | |
print; | |
} | |
} | |
' $f | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment