Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Data Parsing with Shell Scripts

Introduction to Data Parsing

Data parsing is a critical task in shell scripting, involving the extraction and manipulation of data from various file formats such as text, CSV, and JSON. This tutorial covers essential techniques and tools for parsing data using shell scripts.

Parsing Text Files

Text files are a common data source, and parsing them is a fundamental skill in shell scripting. Here, we will demonstrate how to read and extract information from text files.

Reading a Text File


#!/bin/bash

# Read a text file line by line
FILE="data.txt"

while IFS= read -r line
do
    echo "$line"
done < "$FILE"
                

This script reads a text file line by line and prints each line.

Extracting Specific Information


#!/bin/bash

# Extract lines containing a specific keyword
FILE="data.txt"
KEYWORD="error"

grep "$KEYWORD" "$FILE"
                

This script extracts and prints lines containing the specified keyword from a text file.

Parsing CSV Files

CSV files are widely used for storing tabular data. Shell scripts can efficiently parse and process CSV files using various tools.

Reading a CSV File


#!/bin/bash

# Read a CSV file and print each row
FILE="data.csv"

while IFS=, read -r col1 col2 col3
do
    echo "Column 1: $col1, Column 2: $col2, Column 3: $col3"
done < "$FILE"
                

This script reads a CSV file and prints each row with its columns separated by commas.

Extracting Specific Columns


#!/bin/bash

# Extract specific columns from a CSV file
FILE="data.csv"
COLUMN=2

cut -d, -f"$COLUMN" "$FILE"
                

This script extracts and prints the second column from a CSV file.

Parsing JSON Files

JSON is a popular data interchange format, and parsing JSON files in shell scripts can be accomplished using tools like jq.

Reading a JSON File


#!/bin/bash

# Read a JSON file and print its contents
FILE="data.json"

jq '.' "$FILE"
                

This script reads a JSON file and prints its contents in a formatted manner using jq.

Extracting Specific Fields


#!/bin/bash

# Extract a specific field from a JSON file
FILE="data.json"
FIELD=".name"

jq "$FIELD" "$FILE"
                

This script extracts and prints the value of the specified field from a JSON file using jq.

Advanced Data Parsing Techniques

Advanced data parsing involves combining different tools and techniques to process complex data formats and perform intricate data manipulations.

Combining Tools for Complex Parsing


#!/bin/bash

# Extract data from a CSV file and convert to JSON
CSV_FILE="data.csv"
JSON_FILE="data.json"

# Using awk to convert CSV to JSON
awk -F, '{
    printf "{ \"col1\": \"%s\", \"col2\": \"%s\", \"col3\": \"%s\" }\n", $1, $2, $3
}' "$CSV_FILE" > "$JSON_FILE"
                

This script converts a CSV file to a JSON file using awk for complex data manipulation.

Parsing Logs with Multiple Formats


#!/bin/bash

# Parse a log file with mixed formats
LOG_FILE="logfile.log"

# Using grep and awk to extract and format log entries
grep "ERROR" "$LOG_FILE" | awk '{ print $1, $2, $5, $6 }'
                

This script parses a log file to extract and format entries containing the keyword "ERROR" using grep and awk.

Conclusion

Data parsing with shell scripts is a powerful skill that enables efficient extraction and manipulation of data from various file formats. By mastering these techniques, you can automate data processing tasks, enhance your scripts' capabilities, and streamline your workflows.