0% found this document useful (0 votes)
2 views

Unix_and_AWK_Guide

The document provides an overview of Unix basics, including essential commands for file management, viewing, permissions, process management, and data manipulation. It also introduces the AWK command, detailing its syntax and various applications for processing text files, specifically with employee data. Each AWK command is accompanied by explanations and examples, demonstrating how to perform tasks such as sorting, filtering, and calculating statistics on employee information.

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)
2 views

Unix_and_AWK_Guide

The document provides an overview of Unix basics, including essential commands for file management, viewing, permissions, process management, and data manipulation. It also introduces the AWK command, detailing its syntax and various applications for processing text files, specifically with employee data. Each AWK command is accompanied by explanations and examples, demonstrating how to perform tasks such as sorting, filtering, and calculating statistics on employee information.

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/ 5

Unix Basics and AWK Command Guide

1. Unix Basics

Unix is a powerful, multiuser, multitasking operating system used for servers, desktops, and
laptops. Here are some essential Unix commands and concepts:

1. File and Directory Management:


- ls: List directory contents (e.g., ls -l)
- cd: Change directory (e.g., cd /home/user)
- mkdir: Create directory (e.g., mkdir mydir)
- rm: Remove files/directories (e.g., rm file.txt)

2. File Viewing and Editing:


- cat: View file contents (e.g., cat file.txt)
- more, less: Paginated viewing (e.g., less file.txt)
- nano, vi: Text editors

3. File Permissions:
- chmod: Change file permissions (e.g., chmod 755 script.sh)
- chown: Change file ownership

4. Process Management:
- ps: Show current processes
- top: Dynamic view of processes
- kill: Terminate process (e.g., kill 1234)

5. Redirection and Pipes:


- > : Redirect output to file
- >> : Append output to file
- | : Pipe output to another command (e.g., ls | grep txt)

6. Searching and Filters:


- grep: Search text (e.g., grep "word" file.txt)
- find: Locate files (e.g., find . -name "*.txt")
- sort, uniq, cut, head, tail: Data manipulation tools

2. AWK Command Basics

AWK is a powerful text-processing tool. It scans files line by line, splits each line into fields,
and performs actions.
Syntax:
awk 'pattern { action }' filename

- FS: Field Separator (default is space; set to '@' for your data)
- NR: Current Record Number (line number)
- $1, $2, ..., $n: Represents fields in a line

3. AWK Command Explanations (Q1–Q16)

Q1: Display all employee names


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q2: Sort employee names in ascending order


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q3: Sales employees, sorted descending


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q4: Salary > 60000, print Name and Salary


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.
Q5: Count HR employees
Command:
awk -F@ 'NR > 1 && $4=="HR" { count++ } END { print count }' employees.txt

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q6: Total salary of all employees


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q7: Names and ages of employees older than 40


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q8: Average salary in Marketing


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

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:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.
Q10: Salary incremented by 10%
Command:
awk -F@ 'NR > 1 { inc = $5 * 1.10; print $2, inc }' employees.txt

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q11: Sort by salary (ascending)


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q12: Display first 3 rows (excluding header)


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q13: Names starting with S


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q14: Names ending with a


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.
Q15: Names containing 'y'
Command:
awk -F@ 'NR > 1 && $2 ~ /y/ { print $2 }' employees.txt

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

Q16: Names containing vowels


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

Explanation:
This command sets '@' as field separator and processes the file line-by-line according to the
condition specified.

You might also like