UNIX INTERVIEW QUESTIONS AND ANSWERS – 5

UNIX-INTERVIEW-QUESTIONS-and-ANSWERS-part-5


41) I have a file like below which contains special characters in the 2nd column and I want to clean the file by having only alphanumeric values in the file. How can I do that?

Michael,10#$
Bob,20^&
Smith,30@!
Will,20()
Joe,$30+_

Below sed used to remove special characters from file.

sed 's/[^a-zA-Z0-9,]//g' emp_new.txt
[wot@unix ~] $ cat emp_new.txt
Michael,10#$
Bob,20^&
Smith,30@!
Will,20()
Joe,$30+_
[wot@unix ~] $ sed -e ‘s/[^a-zA-Z0-9,]//g’ emp_new.txt
Michael,10
Bob,20
Smith,30
Will,20
Joe,30


42) I want to create a directory when that directory does not exist and if directory exists it should not return any error?


The -p option with mkdir can be used to determine if the directory is already present and create it if it does not exist.

$ mkdir -p Shells

The -p flag creates the Shells if it does not already exist.

However, if the directory already exists, the command does nothing.

[wot@unix ~] $ ls -ltr
total 20
-rw-rw-r–. 1 unix.user unix.user 42 Apr 4 15:03 emp.txt
-rw-rw-r–. 1 unix.user unix.user 72 Apr 4 15:04 emp_data.txt
-rw-rw-r–. 1 unix.user unix.user 31 Apr 4 19:31 2.txt
-rw-rw-r–. 1 unix.user unix.user 31 Apr 12 19:12 sample.txt
drwxrwxr-x. 2 unix.user unix.user 20 Apr 12 19:16 Shells
-rw-rw-r–. 1 unix.user unix.user 53 Apr 13 14:10 emp_new.txt
[wot@unix ~] $ mkdir Shells
mkdir: cannot create directory ‘Shells’: File exists
[wot@unix ~] $ mkdir -p Shells
[wot@unix ~] $


43) How to count the number of columns in each row in a text file?


We would be using awk command to count the number of columns in each row in a text file.The awk command has multiple built in variable that let us define the row, column delimiters.

Suppose we have a comma separated file called ‘records_unix.txt’ with 5 rows in it. Also, each row has different number of columns. In such a case, below awk command can be used to count the number of columns in file.

$ awk -F'|' '{print NR,NF; N++}' records_unix.txt

Syntax explanation:

-F’,’
tells the awk command that the field separator for the file is ‘,’ comma.
print NR, NF
tells the awk command to print the row number and field number.
N++
increments the line count to the next line.
records_unix.txt
name of the input file.

[wot@unix ~] $ cat records_unix.txt
Michael,10,meru,unix,interview
Bob,20,questions,unix
Smith,30,interview
Will,20,unixinterview
Joe,30
[wot@unix ~] $ awk -F’,’ ‘{print NR,NF; N++}’ records_unix.txt
1 5
2 4
3 3
4 3
5 2


44) I have 100 files in a directory and I want to delete all files except some group of files in Unix, how can we do that?

$ rm !(*pattern_to_exclude*)

The above rm command tells the shell to delete all the files in the current directory except the files that match the pattern inside the parenthesis. Here the ‘!’ acts as a negation operator.

The below screen-shot illustrates the same. It deletes all the files in the current directory except for the files that contain the string “emp” in the filename.

[wingsoftechnology@unix ~] $ ls -ltr
total 28
-rw-rw-r–. 1 unix.user unix.user 31 Apr 13 17:28 sample.txt
-rw-rw-r–. 1 unix.user unix.user 101 Apr 13 17:28 records_unix.txt
-rw-rw-r–. 1 unix.user unix.user 6896 Apr 13 17:28 new.csv
-rw-rw-r–. 1 unix.user unix.user 42 Apr 13 17:28 emp.txt
-rw-rw-r–. 1 unix.user unix.user 53 Apr 13 17:28 emp_new.txt
-rw-rw-r–. 1 unix.user unix.user 72 Apr 13 17:28 emp_data.txt
[wingsoftechnology@unix ~] $ rm !(emp*)
[wingsoftechnology@unix ~] $ ls -ltr
total 12
-rw-rw-r–. 1 unix.user unix.user 42 Apr 13 17:28 emp.txt
-rw-rw-r–. 1 unix.user unix.user 53 Apr 13 17:28 emp_new.txt
-rw-rw-r–. 1 unix.user unix.user 72 Apr 13 17:28 emp_data.txt
[wingsoftechnology@unix ~] $


45) I have one file with 4 records, 2 columns and 1 trailer record like below:

Green,10
Red, 30
Blue,60
100

I want to calculate sum of the second column values and need to check whether that total sum is equal to trailer record value i.e.100?

Below shell script can used to calculate sum of the values in the columns and compare the sum with the total value in trailer record.

sum=`awk -F',' 'BEGIN {sum=0} {sum=sum + $2} END {print sum}' file.txt`
echo $sum
total=`tail -1 file.txt`
echo $total
if [ $sum -eq $total ]
then
echo "matched"
else
echo "not matched"
fi
[wot@unix ~] $ cat file.txt
Green,10
Red,30
Blue,60
100
[wot@unix ~] $ cat sample.sh
sum=`awk -F’,’ ‘BEGIN {sum=0} {sum=sum + $2} END {print sum}’ file.txt`
echo $sum
total=`tail -1 file.txt`
echo $total
if [ $sum -eq $total ]
then
echo “matched”
else
echo “not matched”
fi
[wot@unix ~] $ sh sample.sh
100
100
matched
[wot@unix ~] $
Read Also:

Unix interview questions and answers part – 1

Unix interview questions and answers part – 2

Unix interview questions and answers part – 3

Unix interview questions and answers part – 4

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: