0% found this document useful (0 votes)
13 views7 pages

Unix and AWK Guide Revised

The document provides a comprehensive guide on Unix basics and the AWK command with examples. It covers essential Unix commands for file and process management, along with detailed AWK command explanations for data manipulation in employee records. Each command is accompanied by examples to illustrate its usage effectively.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Unix and AWK Guide Revised

The document provides a comprehensive guide on Unix basics and the AWK command with examples. It covers essential Unix commands for file and process management, along with detailed AWK command explanations for data manipulation in employee records. Each command is accompanied by examples to illustrate its usage effectively.

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Unix Basics and AWK Command Guide (With Examples)

1. Unix Basics with Examples

Unix is a powerful command-line operating system used for file management, process
control, text processing, and system management.
Below are essential commands with examples:

ls
Lists files and directories.
Example: ls -l

cd
Changes directory.
Example: cd /home/user

mkdir
Creates a new directory.
Example: mkdir mydir

rm
Removes files or directories.
Example: rm file.txt

touch
Creates an empty file.
Example: touch newfile.txt

cat
Displays file contents.
Example: cat file.txt

nano/vi
Edits files using a text editor.
Example: nano notes.txt

chmod
Changes permissions.
Example: chmod 755 script.sh

chown
Changes file owner.
Example: chown user:group file.txt
ps
Displays active processes.
Example: ps aux

top
Displays dynamic real-time processes.
Example: top

kill
Kills a process.
Example: kill 1234

grep
Searches for a pattern.
Example: grep 'error' logfile.txt

find
Finds files.
Example: find . -name '*.txt'

sort
Sorts lines.
Example: sort names.txt

uniq
Filters duplicate lines.
Example: sort file.txt | uniq

cut
Cuts fields.
Example: cut -d',' -f1 file.csv

head
Displays first lines.
Example: head -n 5 file.txt

tail
Displays last lines.
Example: tail -n 5 file.txt

echo
Prints text to terminal.
Example: echo 'Hello World'
>:
Redirects output to a file.
Example: echo Hello > file.txt

|
Pipes output to another command.
Example: ls | grep '.txt'

2. AWK Command Explanations with Examples

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).

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

Q3: Sales employees, sorted descending


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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.
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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

Q5: Count HR employees


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

Explanation:
Counts number of HR employees.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

Q7: Names and ages of employees older than 40


Command:
awk -F@ 'NR > 1 && $3 > 40 { print $2, $3 }' employees.txt

Explanation:
Checks if age > 40 and prints name and age.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.
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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

Q11: Sort by salary (ascending)


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

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

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

Q12: Display first 3 rows (excluding header)


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

Explanation:
Prints 3 employee lines after skipping header.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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'.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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'.

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

Q15: Names containing 'y'


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

Explanation:
Matches names containing the letter 'y'.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

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).

Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 → $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
etc.

You might also like