Find Interview Questions for Top Companies
Ques:- Write a command to print all line except first line?
Asked In :-
Right Answer:
```bash
awk 'NR > 1' filename
```
Ques:- Write a command to rename the files in a directory with “_new” as postfix?
Asked In :-
Ques:- In the text file, some lines are delimited by colon and some are delimited by space. Write a command to print the third field of each line.?
Asked In :-
Right Answer:
```bash
awk -F '[: ]' '{print $3}' filename.txt
```
Ques:- Write a command to print the squares of numbers from 1 to 10 using awk command?
Asked In :-
Right Answer:
```bash
awk 'BEGIN { for (i=1; i<=10; i++) print i*i }'
```
Ques:- Write a command to print the second and third line of a file without using nr?
Asked In :-
Right Answer:
```bash
awk 'FNR==2, FNR==3 {print}' filename
```
Ques:- Write a command to find the total number of lines in a file without using nr?
Asked In :-
Right Answer:
```bash
awk 'END {print NR}' filename
```
Ques:- How to get only zero byte files which are present in the directory?
Asked In :-
Right Answer:
You can use the following `awk` command to get only zero-byte files in a directory:

```bash
ls -l | awk '$5 == 0 {print $9}'
```
Ques:- How to display even number of records into one file and odd number of records into another file?
Asked In :-
Right Answer:
You can use the following `awk` command to achieve this:

```bash
awk 'NR % 2 == 0 { print > "even.txt" } NR % 2 == 1 { print > "odd.txt" }' inputfile.txt
```
Ques:- How to print only blank line of file?
Asked In :-
Right Answer:
You can use the following `awk` command to print only the blank lines of a file:

```bash
awk '/^$/ { print }' filename
```
Ques:- How add a first record and last record to the current file in linux?
Asked In :-
Right Answer:
To add a first record and last record to a file in Linux using `awk`, you can use the following command:

```bash
awk 'BEGIN { print "First Record" } { print } END { print "Last Record" }' inputfile > outputfile
```

Replace `inputfile` with the name of your current file and `outputfile` with the desired name for the new file.
Ques:- Write a command remove all empty lines?
Asked In :-
Right Answer:
```bash
awk 'NF' filename
```
Ques:- Write a command to print the line number before each line?
Asked In :-
Right Answer:
```bash
awk '{print NR, $0}' filename
```
Ques:- Write a command to print zero byte size files?
Asked In :-
Right Answer:
```bash
awk 'BEGIN { for (f in ARGV) if (system("test -s " f) != 0) print f }' *
```
Ques:- how to run awk command specified in a file?
Asked In :-
Right Answer:
You can run an `awk` command specified in a file using the following syntax:

```bash
awk -f filename.awk inputfile
```

Replace `filename.awk` with the name of your file containing the `awk` script, and `inputfile` with the name of the file you want to process.
Ques:- What is the use of ERRNO?
Asked In :-
Right Answer:
ERRNO is a variable in AWK that holds the error number of the last failed system call or library function, allowing users to check for errors and handle them appropriately in their scripts.
Ques:- What is the use of FIELDWIDTHS?
Asked In :-
Right Answer:
FIELDWIDTHS is an Awk built-in variable that specifies the widths of fields in a fixed-width input file, allowing Awk to correctly parse and process each field based on its defined width.
Ques:- Write a command delete all line except first line?
Asked In :- Quarks Technosoft,
Right Answer:
```bash
awk 'NR==1 {print; next} {next}' filename
```
Ques:- What is the use of SUBSEP?
Asked In :-
Right Answer:
SUBSEP is a built-in variable in AWK that represents the subscript separator used in associative arrays. It is typically set to the ASCII character for the subscript separator, allowing for the use of multi-dimensional arrays.
Ques:- What is the use of CONVFMT?
Asked In :-
Right Answer:
CONVFMT is an AWK variable that defines the format for converting numbers to strings when they are printed. It specifies how numeric values should be formatted when using the print statement.
Ques:- What is the use of RSTART, RLENGTH and match ?
Asked In :-
Right Answer:
`RSTART` is the index of the start of the match found by the `match()` function, `RLENGTH` is the length of the matched string, and `match()` is a function that searches for a regular expression in a string and sets `RSTART` and `RLENGTH` accordingly.


The AWK category is a powerful resource for Linux administrators, DevOps engineers, and computer science students aiming to understand text processing on the command line.

Topics include:

  • Basic AWK syntax and operations

  • Pattern scanning and processing

  • Working with pipes and redirection

  • Real-time use cases in log parsing

  • AWK with shell scripting and cron jobs

  • Interview questions on AWK vs. SED vs. GREP

AmbitionBox Logo

What makes Takluu valuable for interview preparation?

1 Lakh+
Companies
6 Lakh+
Interview Questions
50K+
Job Profiles
20K+
Users