Skip to content

Instantly share code, notes, and snippets.

@DJviolin
Created August 31, 2024 07:08
Show Gist options
  • Save DJviolin/82dd377d55bf2dbb82940d12116fa830 to your computer and use it in GitHub Desktop.
Save DJviolin/82dd377d55bf2dbb82940d12116fa830 to your computer and use it in GitHub Desktop.
AWK JSON output example
#!/usr/bin/awk -f
# This AWK script outputs a valid json file. You can convert it to ndjson stripping off some extra work from this script
# How to run this script
# $ awk -f videos.awk videos.csv > videos.json
BEGIN {
print "["
first = 1
}
{
line = $0;
video_id = $1; # store columns in variables
# strip off url parts from video id
gsub(/.*\?v=|&.*/, "", video_id)
# if line is blank, then remove it
# remove non-alphanumeric lines
if (video_id ~ /^$/ || video_id !~ /[[:alnum:]]/) {
next;
}
# print JSON output with comma handling
if (!first) {
printf(",\n");
}
# print JSON output, because for some reason you want your DB to really handle all of those quotation marks and curly braces instead of raw data
printf(" { \"video_id\": \"%s\" }", video_id);
first = 0;
}
END {
print "\n]"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment