DEV Community

Sirwan Afifi
Sirwan Afifi

Posted on

Text processing with awk

From Wikipedia:

AWK is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it's a filter, and is a standard feature of most Unix-like operating systems.

We can do all sort of things with awk, for example suppose that we have a CSV file which contains list of cities, Now we want to take this file and turn it into a JSON file, all we need to do is go to terminal and type this command:

awk 'BEGIN { 
  print "{ results: [" }
END {
  print "]}\n"
}
{ gsub(/"/, "", $2) gsub(/"/, "", $3) gsub(/"/, "", $4)
  print "{ id: " $1 ", name: \"" $2 "\", countryCode: \"" $3 "\", district: \"" $4 "\" }"
}' cities.csv
Enter fullscreen mode Exit fullscreen mode

In fact awk is a programming language which means that it if, loop, variable assignments,...:

awk -v date="$(date)" -v title="Report Generated By Sirwan Afifi" -F';' 'BEGIN { 
  SPACE = 50;
  printf("%s \n", title)
  print "{ results: [" }
END {
  print "]}\n"
  for(space = 0; space < SPACE; space++) printf "-";
  printf("\nFound %d results\n", city)
  printf("Report generated at %s\n", date)
  space = 0; do { if (space == 0 || space == 49) 
                      printf "*" 
                  else 
                      printf "-"; space++ 
            } while (space < SPACE);
  print "\n"
}
{ city = city + 1
  gsub(/"/, "", $2) gsub(/"/, "", $3) gsub(/"/, "", $4)
  print "{ id: " $1 ", name: \"" $2 "\", countryCode: \"" $3 "\", district: \"" $4 "\" }"
}' cities.csv
Enter fullscreen mode Exit fullscreen mode

Top comments (0)