0% found this document useful (0 votes)
28 views8 pages

Unix Commands With Examples and AWK

The document is a guide on Unix commands and AWK programming, providing examples for various commands such as cat, grep, sort, and awk. It includes practical examples of how to manipulate and process text files, specifically focusing on a sample file 'fruits.txt' and employee data. The guide covers tasks like searching, sorting, and calculating data using Unix commands and AWK scripts.

Uploaded by

ragerspirit037
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Unix Commands With Examples and AWK

The document is a guide on Unix commands and AWK programming, providing examples for various commands such as cat, grep, sort, and awk. It includes practical examples of how to manipulate and process text files, specifically focusing on a sample file 'fruits.txt' and employee data. The guide covers tasks like searching, sorting, and calculating data using Unix commands and AWK scripts.

Uploaded by

ragerspirit037
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unix and AWK Guide (With Examples)

Unix Command Notes with Examples

We'll use this sample file throughout the examples:

fruits.txt

------------------------

fruit_id,fruit_name,fruit_qty,unit_price,total_price

1,Mango,2,10,20

2,Apple,6,15,90

3,Banana,4,8,32

4,Watermelon,7,9,63

5,apple,3,15,45

cat

Used to display the contents of a file.

Example:

cat fruits.txt

=> Displays all lines of the file.

grep

Search for patterns in files.

Example:

grep 'Apple' fruits.txt

=> Shows lines containing 'Apple'.

sort

Sort lines in text files.

Example:
Unix and AWK Guide (With Examples)

sort -t',' -k5,5n fruits.txt

=> Sorts by total_price ascending.

uniq

Report or omit repeated lines.

Example:

sort fruits.txt | uniq

=> Removes duplicate lines (must sort first).

cut

Remove sections from each line of files.

Example:

cut -d',' -f2 fruits.txt

=> Extracts fruit_name column.

awk

A powerful text-processing language.

Example:

awk -F',' '$5 > 50 { print $2, $5 }' fruits.txt

=> Prints fruit_name and total_price if price > 50.

sed

Stream editor for filtering and transforming text.

Example:

sed 's/apple/APPLE/g' fruits.txt

=> Replaces 'apple' with 'APPLE' globally.


Unix and AWK Guide (With Examples)

head

Display first lines of a file.

Example:

head -n 2 fruits.txt

=> Shows the first 2 lines.

tail

Display last lines of a file.

Example:

tail -n 2 fruits.txt

=> Shows the last 2 lines.

wc

Print newline, word, and byte counts.

Example:

wc -l fruits.txt

=> Outputs the number of lines.

find

Search files in a directory hierarchy.

Example:

find . -name '*.txt'

=> Finds all .txt files recursively.

ls
Unix and AWK Guide (With Examples)

List directory contents.

Example:

ls -l

=> Lists files with details.

chmod

Change file permissions.

Example:

chmod 755 file.sh

=> Sets read-write-execute for owner, read-execute for others.

AWK Command Programs (Fixed Explanations)

Q1: Display all employee names

Command:

awk -F@ 'NR > 1 { print $2 }' employees.txt

Explanation:

Skips header (NR > 1) and prints the 2nd field (Name).

Q2: Sort employee names in ascending order

Command:

awk -F@ 'NR > 1 { print $2 }' employees.txt | sort

Explanation:

Prints names and sorts them alphabetically using sort.

Q3: Sales employees, sorted descending


Unix and AWK Guide (With Examples)

Command:

awk -F@ 'NR > 1 && $4=="Sales" { print $2 }' employees.txt | sort -r

Explanation:

Selects only Sales department employees and sorts names in reverse order.

Q4: Salary > 60000, print Name and Salary

Command:

awk -F@ 'NR > 1 && $5 > 60000 { print $2 "@" $5 }' employees.txt

Explanation:

Prints name and salary if salary is greater than 60000.

Q5: Count HR employees

Command:

awk -F@ 'NR > 1 && $4=="HR" { count++ } END { print count }' employees.txt

Explanation:

Counts number of HR employees.

Q6: Total salary of all employees

Command:

awk -F@ 'NR > 1 { sum += $5 } END { print sum }' employees.txt

Explanation:

Adds up salaries and prints total.

Q7: Names and ages of employees older than 40

Command:

awk -F@ 'NR > 1 && $3 > 40 { print $2, $3 }' employees.txt
Unix and AWK Guide (With Examples)

Explanation:

Checks if age > 40 and prints name and age.

Q8: Average salary in Marketing

Command:

awk -F@ 'NR > 1 && $4=="Marketing" { sum+=$5; count++ } END { print sum/count }' employees.txt

Explanation:

Computes average salary for Marketing department.

Q9: Minimum salary - print Name and Salary

Command:

awk -F@ 'NR == 2 || $5 < min { min=$5; name=$2 } END { print name, min }' employees.txt

Explanation:

Tracks minimum salary and corresponding name.

Q10: Salary incremented by 10%

Command:

awk -F@ 'NR > 1 { inc = $5 * 1.10; print $2, inc }' employees.txt

Explanation:

Calculates 10% raise and prints updated salary.

Q11: Sort by salary (ascending)

Command:

awk -F@ 'NR > 1' employees.txt | sort -t@ -k5,5n

Explanation:
Unix and AWK Guide (With Examples)

Sorts whole lines based on 5th field (Salary).

Q12: Display first 3 rows (excluding header)

Command:

awk -F@ 'NR > 1 && NR <= 4' employees.txt

Explanation:

Prints 3 employee lines after skipping header.

Q13: Names starting with S

Command:

awk -F@ 'NR > 1 && $2 ~ /^S/ { print $2 }' employees.txt

Explanation:

The '^' symbol matches beginning of string; selects names starting with 'S'.

Q14: Names ending with a

Command:

awk -F@ 'NR > 1 && $2 ~ /a$/ { print $2 }' employees.txt

Explanation:

The '$' symbol matches end of string; selects names ending with 'a'.

Q15: Names containing 'y'

Command:

awk -F@ 'NR > 1 && $2 ~ /y/ { print $2 }' employees.txt

Explanation:

Matches names containing the letter 'y'.


Unix and AWK Guide (With Examples)

Q16: Names containing vowels

Command:

awk -F@ 'NR > 1 && $2 ~ /[aeiouAEIOU]/ { print $2 }' employees.txt

Explanation:

Matches names that contain at least one vowel (a, e, i, o, u).

You might also like