DEV Community

Sirwan Afifi
Sirwan Afifi

Posted on

3

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay