Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Last active September 9, 2022 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushikgopal/17d1c9718110e9acc5cc7946f5fe0761 to your computer and use it in GitHub Desktop.
Save kaushikgopal/17d1c9718110e9acc5cc7946f5fe0761 to your computer and use it in GitHub Desktop.
awk explainer example
# 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>
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