
Introduction
Bash one-liners can streamline your data transformations by cutting out time-consuming steps in your workflow. Whether you’re preparing data for machine learning or cleaning up logs for statistical analysis, these concise commands can save effort and reduce errors.
Below are 10 powerful one-liners that can help you quickly accomplish essential data tasks. Each example includes an explanation of when and why you might use it, as well as a typical real-world scenario.
1. Count the Number of Lines in a File
Command
wc -l file.txt
Explanation & Use Case
Sometimes, you want a quick check on the size of your dataset to make decisions about sampling, memory usage, or iteration in scripts. wc -l simply returns the line count, which is especially useful for structured data in CSV or TSV formats.
Real-World Scenario
If you receive a data file of user interactions and need to know how many records exist before loading it into a data pipeline, wc -l immediately gives you the total row count.
2. Sum a Specific Column
Command
awk '{sum += $1} END {print sum}' data.txt
Explanation & Use Case
awk is a text-processing powerhouse. In this example, $1 denotes the first column, and each value is added to sum. This is helpful for quickly confirming statistics like total counts of a numeric field without writing a more elaborate script.
Real-World Scenario
Suppose you have a daily sales file in which the first column contains the revenue amount. You can quickly sum the total revenue for the day with this single command.
3. Extract a Specific Column from a CSV
Command
cut -d',' -f2 data.csv
Explanation & Use Case
cut splits lines according to a specified delimiter (-d',') and extracts the column (-f2) you need. This is invaluable when you need to isolate a certain field — like user IDs — from a large CSV file without loading it into a program like Python or R.
Real-World Scenario
You receive a large CSV with multiple columns (ID, timestamp, event type, etc.). If you only need the second column (timestamp) for further processing, cut gives you a straightforward solution.
4. Sort a File Numerically by the Second Column
Command
sort -k2,2n data.txt
Explanation & Use Case
Sorting is a standard operation in data wrangling. Here, -k2,2n sorts by column two in ascending numeric order. This one-liner is especially handy for quickly identifying top values or verifying that your dataset is ordered by a specific variable.
Real-World Scenario
A metrics report lists user scores in the second column. You need to see the data sorted from the smallest to largest score to analyze outliers. This command does it without additional overhead.
5. Find Unique Rows
Command
sort data.txt | uniq
Explanation & Use Case
uniq requires the input to be sorted if you want it to detect and remove duplicates. This combination is excellent for quickly cleaning or reducing data sets, such as removing repeated log entries or deduplicating user IDs.
Real-World Scenario
When analyzing server logs, you might see the same request repeated. Sorting the logs first and using uniq can give you a condensed list of unique entries for further investigation.
6. Merge Two Files with Common Keys
Command
join -1 1 -2 1 file1.txt file2.txt
Explanation & Use Case
join combines lines from two sorted files that share a common field. With the options shown, the first column of both files is treated as the key. It’s a simple way to perform a database-like join on plain text files.
Real-World Scenario
If you have user purchase data in one file and user demographic data in another, both keyed by user ID in the first column, you can merge them to create a single, richer dataset.
7. Count Matches of a Pattern
Command
grep -c "pattern" data.txt
Explanation & Use Case
grep -c "pattern" data.txt
grep searches for lines that match a given pattern. The -c flag returns the number of matching lines instead of printing them. This is ideal for quickly assessing how many times a value or string appears in your data.
Real-World Scenario
When you need to determine how many times a specific product ID (e.g., “SKU123”) shows up in a transaction file, a simple grep -c "SKU123" transactions.txt will tell you exactly that.
8. Search and Replace with sed
Command
sed 's/foo/bar/g' file.txt
Explanation & Use Case
sed provides an easy way to transform text on the fly. The above command replaces all occurrences of “foo” with “bar” in each line of file.txt. This is a quick fix for changing column headers or cleaning up mislabeled data.
Real-World Scenario
Let’s say you receive a dataset where the string “N/A” is used for missing values, but your analysis pipeline expects “NULL.” Use sed to replace all instances of “N/A” with “NULL” before loading the data.
9. Preview the First or Last Lines
Command
head -n 10 file.txt tail -n 10 file.txt
Explanation & Use Case
head and tail show the first or last lines of a file, respectively. This helps you quickly check the file’s structure, confirm a file was saved correctly, or inspect trailing logs.
Real-World Scenario
Right after receiving a large CSV, you can run head -n 10 data.csv to confirm the column names and general layout. Similarly, you can confirm the final lines with tail to check any appended records.
10. Perform Simple Arithmetic with awk
Command
awk '{print $1, $2+100}' data.txt
Explanation & Use Case
awk can handle arithmetic operations on the fly, which is convenient for quick manipulations without writing a script. Here, it prints the first column as is and the second column increased by 100.
Real-World Scenario
Imagine you have a column of monthly sales counts. You realize you need to adjust these figures by a static offset to correct an accounting error. Instead of recalculating in a spreadsheet, a single awk command updates each row.
Conclusion
Bash one-liners provide quick, reliable methods for transforming data without requiring heavier scripting languages. From simple file summarization to numeric transformations and merging datasets, these commands give you powerful ways to structure, cleanse, and explore data in just one or two lines of code. Integrating them into your workflow can significantly reduce overhead and speed up your data science or statistical analysis pipeline.
Happy bashing!
