unix command

1. to display total number of  word  and line  of files which file name started with m in cygwin


find /path/to/directory -type f -name "m*" | xargs wc -w -l



2. to remove all leading space from a file f1

sed -i 's/^ *//' f1


to display line number before each line

nl -ba f1



write shell script to print number in reverse order  it should support following requirement  -script should input from command line and if you don't input any data then display error message to execute the script correctly


if [ $# -eq 0 ]; then

    echo "Error: Please provide numbers as command line arguments."

    echo "Usage: $0 <number1> <number2> ..."

    exit 1

fi


echo "Numbers in reverse order:"

for ((i=$#; i>0; i--)); do

    echo "${!i}"

done




AWK 

How to see column 2 or 3?

awk '{print $2}' file_name


How to see multiple coulumns?

awk '{print $2,$3}' file_name


How to see last column?

awk '{print $NF}' file_name


How to see line no.?

awk '{print NR}' file_name


How to see line no. with - ?

awk '{print NR "- " $2}' file_name


How to get a column from CSV?

awk -F, '{print $7}' country.txt


How to change the salary of Pol?

awk '{if($2=="Pol"){$3="90000"} print $0}' file_name


How to see data of users whose salary is more than 40000?

awk '{if($3>40000) print $0}' file_name


How to see a line whose length of character is more than 15?

awk 'length($0)>15' file_name


How to see data of Indian users?

awk '/India/ {print}' file_name


How to see a specific line example 3rd line?

awk 'NR==3 {print}' file_name


How to see range of lines, 3 to 5th line?

awk 'NR==3,NR==5 {print}'


How to see which lines are empty?

awk 'NF==0 {print NR}' file_name


How to check no. of lines in the file?

awk 'END {print NR}' file_name


How to use for loop in AWK command?

awk 'BEGIN {for(i=0;i<=10;i++) print i;}'


How to use while loop in AWK command?

awk 'BEGIN {while(i<10){ i++; print "Num is " i;}}'



How to see only 2nd char?

cut -c2 file_name


How to see 1 to 4 char?

cut -c1-4 file_name


How to see only 2 and 4th char?

cut -c2,3 file_name


How to see data from CSV?

cut -d: -f 3 file_name   


How to change the delimeter of output?

cut -d, -f 1- country.txt --output-delimiter="|"


How to use cut command with other commands like cat?

Using pipe |



SED


How to show only a given line or range of lines?

sed -n '1p' file_name

sed -n '1,5p' file_name

sed -n '$p' file_name


How to see all the users from India Country?

sed -n '/India/p' file_name



How to use multiple expression in sed command?

Example: If you wanna only see 2 and 5th line

sed -n -e '2p' -e '5p' file_name


How to see all the users from India and Germany?

sed -n -e '/India/p' -e '/Germany/p' file_name



How to see next 4 lines from 2nd line?

sed -n ‘2,+4p’ file_name


How to see every 2nd line from first line?

sed -n ‘1~2p’ file_name


How to read expression from external file?

sed -f ex_file file_name


How to replace a word in a file and show?

sed 's/<string_to_change>/<new_string>/g' file_name


How to replace a word in a file and show except a given line or only in given line?

sed '5 s/<string_to_change>/<new_string>/g' file_name

sed '5! s/<string_to_change>/<new_string>/g' file_name


How to replace a word and and edit in your file?

sed -i's/<string_to_change>/<new_string>/g' file_name


How to change salary or country of a user (Paul)?

sed '/Paul/ s/25000/35000/g' file_name

sed '/Paul/ s/India/US/g' file_name



How to delete a line?

sed '1d' file_name                                    (to delete first line)

sed '1,2d' file_name

sed '$d' file_name


How to delete user from India country?

sed ‘/India/d’ file_name


How to delete empty line?

sed '/^$/d' file_name


How to replace tab with space?

sed 's/\t/ /g' file_name


How to copy output of sed command in separate file?

sed -n ‘/India/ w new_file_name’ file_name


How to add new line after a given line no.?

sed '5 a new_text' file_name


How to add new line after a given string, so it will add text after Paul?

sed '/Paul/ a new_text' file_name


How to edit existing line instead of adding new line?

sed '5 c new_text' file_name (it will add text at line 5)


How to add new line before a given string, so it will add text before Paul?

sed '/Paul/ i new_text' file_name


How to see the hidden characters?

sed -n 'l' file_name


How to wrap your file content with given no. of characters?

sed -n 'l 50' file_name


How to read content from a file and use in our command?

sed '3 r externalfile' file_name


How to stop execution of sed command as soon as first occurance found?

sed ‘/India/ q’ file_name

sed ‘5 q’ file_name  (stop execution at line 5)


How to provide exit status for your sed command?

sed ‘/India/ q 100’ file_name



How to execute external command line date in your expression?

sed '2 e date' file_name


How to see the line number in file?

sed '=' file_name





SED Regular Expressions


^   start of line

$   end of line

.   single character

[]  match character set

[^] exclusive Set 

*   zero or more occurance


examples:


sed -n '/^2/p' file_name

sed -n '/ia$/p' file_name


How to find a 5 letter name which start with S and end with a?

sed -n '/^S...a$/p' names


How to find names start with V?

sed -n '/^V/p' names


How to find names end with a?

sed -n '/a$/p' names


How to use wild card?

ls -ltr *.txt


How to see names start with only A and C?

sed -n '/[AC]/p' names

sed -n '/[AC]/p' names



How to see names start with only A to D?

sed -n '/^[A-D]/p' names




POSIX Classes


Example: sed -n ‘/[[:alpha:]]/p’ posix


[:alnum:]

[:alpha:]

[:digit:]

[:blank:]

[:lower:]

[:upper:]

[:punct:]

[:space:]



GREP

ignore upper and lower case
grep -i "KW" file

search everthing except given patter/kw
grep -v "kw" file


to print how many times (count) given keyword present in file

grep -c "kw" file


search for exact match of given keyword in file

grep -w "kw" file


to print the line no of matches of given kw in file

grep -n   "kw" file



search a given kw in multiple files

grep "kw" file1 file2..

suppress file names while search a given keyword in multiple files
grep -h "kw" file1 file2

search multiple kw in file
grep -e ''kw1" -e "kw2" file

multiple kw in multiple file
grep -e ''kw1" -e "kw2" file1 file2

print only file names which matches given kw
grep -l ''kw" file1 file2


to print the matching line which start with given kw
grep "^kw" file

end with
grep "kw$" file

suppose we have 100 files in a directory (dirA) and we need to search a keyword in all the files
grep -R "kw" dirA/

egrep for multiple kw search
egrep "key1|key2|key3" file

just search but dont want to print on terminal 
grep -q "kw" file

error 
grep -s "kw" file







Comments