You are on page 1of 10

Unix Commands

1. Command to print substring of a string.

Example:

$ cat file

hello world

awk'{print substr($0,2,6)}' file

the syntax for substr() issubstr(string,startindex,length)

2. Sample to execute a series of command in awk.

export new_parma=`echo <variable>| awk '{ start=index($0,"<pattern>l");start=start+1;print


substr($0,start) }'`

3. unset_project_env – to unset project environment


4. find . -type f -name <*pattern*> 2>/dev/null – used this to find a particular pattern
from root.
5. find .-type f -exec grep -il *<pattern>* {} \;
6. find command examples: https://www.linux.com/blog/25-examples-linux-find-command-
search-files-command-line
7. To search multiple patterns using grep:
m_dump<dml><file > -select ‘<select statement>‘| egrep "<pattern 1>|<pattern2>”
8. To use cut command to display only columns

Echo $<variable> | cut -c3-8

9. To untar a tar file


Tar –xvf <tar file name>
10. To lists the contents of tar file:
Tar –tvf<tar filename>
11. air project export <project path> -basedir<sandbox path>
12. m_dump compressed file

m_dump<dml_file_name><input_file_name> -decompress

or

zcat compressed file|m_dumpdmlfile -

13. to read .gz file without unzipping


gzcat<file>.gz
14. m_dump<dml>–describe

touch command options:

touch –a --- updates the current timestamp for a file(tried but not happening)

touch –c --- if file available, updates the timestamp but does not create a file, if file doesn’t
already exists

touch –m --- modifies the timestamp of a file to current timestamp

touch –c –t yyyymmddhhmin<filename> --- modifies the timestamp of the file to specified


timestamp.

Touch –r <file1><file2> --- updates timestamp of file2 with timestamp of file1

Touch –t yyyymmddhhmin.ss --- creates a new file with specified timestamp

Inorder to do arithmetic operation in single line, use `\` before parenthesis

Ex: c=`expr \( $b - $a \) \* 100 / $b`

To print only particular fields in m_dump

m_dump<dml><file_name> -select ‘<select statement>’ -no-print-data -print <field1> -print <field


2>

Alternative Way:

is equal to: eq
is not equal to: ne
is greater than: gt
is less than: lt
is greater than or equal to: ge
is less than or equal to: le

-a is same as using &&


-o is same as using ||

unset_project_env
prstat -a (top command)
du -gh, df -gh

sudo -u <user_id>ksh

for creation of AI test flag directories


air sandbox directories -create -basedir<sandbox path> -all-commons -ignore-existing -references
AI_TEST_FLAG -AI_TEST_FLAG <user_id>

unix test conditions:

Operators

test, [, and [[ accept the following operators:

-b file

true if the file is a block special file

-c file

true if the file is a character special file

-d file

true if the file is a directory

-e file

true if the file exists

-f file

true if the file is an ordinary file

-g file

true if the setgid attribute of the file is on under UNIX (or the system attribute on Windows systems)

-h file

true if file is a symbolic link

-k file

true if the save text attribute of the file is on under UNIX (or the archive attribute on Windows
systems)

-L file

true if file is a symbolic link

-n string

true if the length of string is greater than zero

-p file

true if the file is a FIFO (named pipe)

-r file
true if the file is readable

-s file

true if the size of the file is non-zero

-t fd

true if the numeric file descriptor fd is open and associated with a terminal

-u file

true if the setuid attribute of the file is on under UNIX (or the hidden attribute on Windows systems)

-w file

true if the file is writable

-x file

true if the file is executable. If the specified file does not exist, test checks each extension in the
PATHEXT environment variable (in the order listed) to see if a file exists with the specified name plus
that extension. If such a file does exist and it is executable, -x returns true.

-z string

true if the length of the string is zero

number1 -eq number2

true if number1 and number2 are equal

Both number1 and number2 must be integers

number1 -ge number2

true if number1 is greater than or equal to number2

number -gt number

true if number1 is greater than number2

number1 -le number2

true if the first number1 is less than or equal to number2

number1 -lt number2

true if number1 is less than number2

number1 -ne number2

true if number1 is not equal to number2

file1 -nt file2

true if file1 is newer than file2


Note:

All existing files are considered to be newer than a nonexistent file. In turn, no nonexistent file is
newer than any existing file. Two nonexistent files are considered the same age.

file1 -ot file2

true if file1 is older than file2

Note:

A nonexistent file is considered to be older than any existing file. In turn, no existing file is older than
a nonexistent file. Two nonexistent files are considered the same age.

file1 -ef file2

true if file1 has the same device and i-node number as file2

The following operators handle string comparisons for the test and [ commands:

string

true if string is not a null string

string1 = string2

true if string1 and string2 are identical

string1 != string2

true if string1 and string2 are not identical


The [[ command has several operators which handle string comparisons and pattern-matching. In
the following descriptions, pattern is a glob pattern as described in the File Name Generation section
of sh. To treat pattern as a string, quote it.

string == pattern string = pattern

true if string matches pattern.

string != pattern

true if string does not match pattern.

string1 < string2

true if string1 comes before string2 alphabetically.

string1 > string2

true if string1 comes after string2 alphabetically.

The following operators allow you to combine other operators:

expr1 -a expr2 (test and [ commands) expr1 && expr2 ([[ command)

logical AND; true if both expr1 and expr2 are true

expr1 -o expr2 (test and [ commands) expr1 || expr2 ([[ command)

logical OR; true if either expr1 or expr2 is true

! expr

logical negation; true if expr is false

( expr )

binding; true if expr is true

The precedence of the operators, in descending order, is:

• parenthesized expressions

• unary operators
• comparison operators

• logical AND

• logical OR

EXAMPLES

The following command reports on whether the first positional parameter contains a directory or a
file:

if [ -f $1 ]

then

echo $1 is a file

elif [ -d $1 ]

then

echo $1 is a directory

else

echo $1 neither file nor directory

fi

sed command examples:

http://www.folkstalk.com/2012/01/sed-command-in-unix-examples.html

https://www.tecmint.com/linux-sed-command-tips-tricks/

tr command examples:

http://www.thegeekstuff.com/2012/12/linux-tr-command/

grep command"

https://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/

https://www.thegeekstuff.com/2011/01/regular-expressions-in-grep-command/
https://www.linuxnix.com/awk-scripting-10-begin-and-end-block-examples/

how to find sum of particular column in file using awk:

awk 'BEGIN {sum=0} {sum=sum+$5} END {print sum}' input_file

This will print the squares of first numbers from 1 to 5. The output of the command is

awk 'BEGIN { for(i=1;i<=5;i++) print "square of", i, "is",i*i; }'

print the sum of the columns with sum value greater than 1000.

awk 'BEGIN {sum=0}{sum=sum+$1; if(sum >=1000) print sum}'

We can change this default behavior using the OFS variable as

awk 'BEGIN {OFS=":"} {print $4,$5}' input_file

8. awk '{print NF}' input_file


This will display the number of columns in each row.

NR - number of records variable: 


The NR can be used to know the line number or count of lines in a file.

9. awk '{print NR}' input_file


This will display the line numbers from 1.

10. awk 'END {print NR}' input_file


This will display the total number of lines in the file.

String functions in Awk:


Some of the string functions in awk are:

index(string,search)
length(string)
split(string,array,separator)
substr(string,position)
substr(string,position,max)
tolower(string)
toupper(string)

split function example:

awk '{

split($2,arr,",");
if(arr[3] == "UNIX")

print $0

} ' file.txt

awk command for finding number of blank line:

BEGIN { x=0 }
/^$/  { x=x+1 }
END   { print "I found " x " blank lines. :)" }

Awk complex queries examples:

https://www.ibm.com/developerworks/library/l-awk1/index.html

https://www.funtoo.org/Awk_by_Example,_Part_1

AWK interview questions:

http://www.folkstalk.com/2011/11/unix-interview-questions-on-awk-command.html

interview questions:

https://career.guru99.com/shell-scripting-interview-questions/

remove duplicates in a file:

awk '!seen[$0]++' filename

awk '{if(dup[$0]++ == 1) print $0}' <filename>

seen is an associative-array that Awk will pass every line of the file to. If a line isn't
in the array then seen[$0] will evaluate to false. The ! is a logical NOT operator
and will invert the false to true. Awk will print the lines where the expression
evaluates to true. The ++ increments seen so that seen[$0] == 1 after the first time
a line is found and then seen[$0] == 2, and so on.
Awk evaluates everything but 0 and "" (empty string) to true. If a duplicate line is
placed in seenthen !seen[$0] will evaluate to false and the line will not be written to
the output.

awk pattern matching examples:


https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-
in-linux

awk arrays: http://www.delorie.com/gnu/docs/gawk/gawk_126.html


complex interview questions: http://theprofessionalspoint.blogspot.in/2012/12/35-tricky-and-complex-
unix-interview.html
tail command exampless:
http://www.folkstalk.com/2012/10/tail-command-examples-in-unix-linux.html

You might also like