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
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 ShellsThe -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 ~] $
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.txtSyntax 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$ 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 ~] $Green,10 Red, 30 Blue,60 100I 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 – 5
September 14, 2019
UNIX INTERVIEW QUESTIONS