Created
August 31, 2024 07:08
-
-
Save DJviolin/82dd377d55bf2dbb82940d12116fa830 to your computer and use it in GitHub Desktop.
AWK JSON output example
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 | |
# 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