Using awk to create switch cases from enum definition
- Felipe Carvalho

- Jun 13, 2020
- 1 min read
Updated: Jun 16, 2020

You know how you have all those enum cases and want each and every one of them in a switch? You can use awk to make this easy.
enum definition example:
enum Constants {
static let showVisitMenu = "com.app.nyspcainc.observer.showVisitMenu"
static let showCallMenu = "com.app.nyspcainc.observer.showCallMenu"
static let showTextMenu = "com.app.nyspcainc.observer.showTextMenu"
static let addNote = "com.app.nyspcainc.observer.addNote"
static let showDashboard = "com.app.nyspcainc.observer.showExpertDashboard"
static let showVisit = "com.app.nyspcainc.observer.showVisit"
static let showQRCode = "com.app.nyspcainc.observer.showQRCode"
static let showSurvey = "com.app.nyspcainc.observer.showSurvey"
static let activityAssigned = "com.app.nyspcainc.observer.activityAssigned"
static let activityCancelled = "com.app.nyspcainc.observer.activityCancelled"
}This is how you can do this: 1. Move all cases to file and save it.
2. Open your terminal window, navigate to where you saved your file.
3. Use this line:
cat file | awk -F 'static let ' '{print $2}' | awk -F ' = ' '{print "case ."$1": break"}'4. Your output should be:
case .showVisitMenu: break
case .showCallMenu: break
case .showTextMenu: break
case .addNote: break
case .showDashboard: break
case .showVisit: break
case .showQRCode: break
case .showSurvey: break
case .activityAssigned: break
case .activityCancelled: breakInteresting right?
Comments