Sie sind auf Seite 1von 19

Unix Script

-------------------------------------------------------------------------------Loo ing for last minute shopping deals? Find them fast with Yahoo! Search. Plain Text Attachment [ Scan and Save to Computer ] 1.Write a regular expression to do the following.

^$ [^ \o11]*$

^. .$ -v ^$ c. Write a regular expression that matches a line with exactly two characters ^..$ /^.\{2\}$/ /^ *\o11.\{1\} \o11.\{1\} \o11$/ d. Write a regular expression that matches a line with at least three characters /^ \t*.\{\} \t.\{1\} \t.\{1\} \t.\{1,\}$/ e. Write a regular expression that matches a line with at most three characters /^.\{1,4\}$/ f. Write a regular expression that matches a date in the following format: Month dd,yy ; mm/dd/yy /[a-zA-Z] [0-9][0-9], [0-9][0-9] /[0-1][0-9]\/[123][0-9]\/[0-9][0-9]/ g. Write a regular expression that matches a social security number pattern: (ddd-dd-dddd) /[0-9]\{4\}\-[0-9]\{2\}\-[0-9]\{4\}/ h. Write a regular expression that matches a 7 digit telephone number /[0-9]\{,4\}\-[0-9]\{7\} i. Write a regular expression that matches an HTML tag. /^<[a-zA-Z]>.*<[/a-zA-Z]>$/

b. Write a regular expression that matches a non-blan

a. Write a regular expression that matches a blan

line

line

j. Write a regular expression that matches a number between 20 and 29. /2[0-9]/ . That selects a line from file1 that have exactly 3 characters. /^ *\t*.\{1\} *\t*.\{1\} *\t*.\{1\} *\t*.\{1\}/ 2.Write a grep command that selects lines from the file1 that end with the period. grep \.$ test

3.Write a grep command that copies file1 to file2 grep . file1>file2 grep .* file1 > file2 4.Write a grep command that copies nonblan lines from file1 to file2; that is it deletes the blan lines grep [^\n][^\t][^ ] file1>file2 grep ^[^ ].* file1 > file2 5.Write a grep command that selects the lines from file1 that end with more than one blan spaces. grep \{2,\}$ file1 grep . $ file1 6. Write a grep command that selects the lines from file1 that have atleast 2 digits. grep ^[0-9][0-9].*$ file1

7. Write a grep command that selects the lines from file1 that start with A to E or M to P, inclusive. grep ^[A-EM-P] file1 grep ^[A-EM-P].*$ file1 8. Write a command that selects the lines that have only one integer . The line should not have any other characters. grep ^[0-9]$ file1

9. Write a command that selects the lines that have only one octal number (the octal number should starts with 0). The line should not have any other characters. grep ^0[0-7]*$ file1

10.Write a command that, using an input file, creates the output file that

contains only lines with no alphabetic characters. grep v [a-zA-Z] test

11. Write a command that, using an input file, creates an output file. The output file contains only lines that have at least four digits. The digits can be consecutive (next to each other) or separated by charactes. grep [0-9].*[0-9].*[0-9].*[0-9].* file1 12. Write a command that, using an input file, creates an output file. The output file is the same as input file except it contains only the lines without leading or trailing zeros. grep '^[^0].*$' users|grep '^.*[^0]$'>file1 grep ^[^0].*[^0]$ file1 13. Write a command that, using an input file, creates an output file. The output file is the same as input file, but it contains only the lines that are a five-character palindrome. grep o \(.\)\(.\).\2\1 file1 #matching word only display grep \(.\)\(.\).\2\1 file1 #matching line display 14. Use grep command to simulates the following commands. a.Cp file1 file2 grep .* file1 >file2 file1 | grep ^[3-9][0-9]

b.Tail +30 file1 grep n .*

c.Head -20 file1 grep m 20 file1 d.Cat file1 grep .* e.Cat > file1 grep .* file1 > file1

15.Write a sed command to do the following. a. deletes the first character in each line in a file sed s/^.// file1 b.deletes the character before the last character in each line in a file. sed s/\(.\)\(.\)$/\2/ file1 c.deletes the second word in each line in a file. sed 's/^\( *[^ ]\{1,\} \{1,\}\)[^ ]\{1,\}/\1/' file1 d.swaps the first and second character in each line in a file. sed s/^\(.\)\(.\)$/\2\1/ file1

e.deletes any integer in each line in a file. sed s/[0-9]//g file1 f.replaces all single spaces at the beginning of each line with a tab, sed s/^ \{1,\}/\o11/ file1 g.that double -spaces a file. sed 'G' file1 16. Write a sed command to simulate the following commands a.Cp file1 file2 sed n 1,$p b.Cat file1 sed n /.*/p c.Head -20 file1 sed n 1,20p file1>file2 file1 file1

17. Write an aw command that will do the following a. prints the value of the first field of every input line aw -F"|" '{print $1}' b. prints number of fields in each line. aw 'BEGIN{ FS="|" print "This Script will print record number and field number for each record in file\n" } { print "Record No", NR,"has",NF,"Fields" }' file1 c. prints the record number for each record in the file.

d. prints the value of the last field in each line aw 'BEGIN { FS="|" printf "This Script will print record number for each record in file\n" } { printf $NF }' file1 e. prints the second field of a line if its first field is greater

than 9 aw -F"|" 'length($1) < 9 {print $2}' file1 f.prints every line that has at least one field. aw -F"|" 'NF>1 {print $0}' list1 g.prints the sum of all values in the third field. aw -F"|" ' NF > 3 { = +$3; print $0} END{ printf "The TOtal is %d", } ' list1 h.prints the number of blan lines. aw -F"|" ' NF < 1 { = +1;} END{ printf "NUmber of blan line are %d", }' list1 i.prints the lines in which the value of the third field is greater than 5.00 and the value of the forth field is not zero. aw -F"|" ' $5 >5.00 && $4 != 0 { print $0}' list1 8. Write an aw command that simulates the following command: a.Cat file1 aw -F"|" ' BEGIN{ printf FILENAME} {print $0}' $1 b.Head -20 file1 aw -F"|" ' NR<=20{ print $0}' $1 c.Tail -40 file1 aw -F"|" ' NR>=40{ print $0}' $1 19.Write a shell script that, given a string as the only command-line argument, uses a (1) grep command, (2) sed command to determine whether a first and last character of the string are the same. #Script to compare 1st and last character if [ $# -ne 1 ] then echo "Please enter just a single argument in double quotes" exit fi

echo "The argument you entered is: $1" echo $1|grep -q '^\(.\).*\1$' if [ $? -eq 0 ] then echo "1st and last character are the same" else echo "no match found" fi res=`echo $1|sed -n '/\(.\).*\1$/p'` if [ -z $res ] then echo "No match found" else echo "1st and last character are same" fi 20.Write a shell script that, given a string as the only command-line argument, exchanges the first three characters of the string with the last three characters. #Script to exchange 1st 3 characters with last 3 characters. if [ $# -ne 1 ] then echo "Please enter just a single argument in double quotes" exit fi echo "The argument you entered is: $1" result=`echo $1 | sed 's/^\(.\)\(.\)\(.\)\(.*\)\(.\)\(.\)\(.\)$/\5\6\7\4\1\2\3/g'` echo "The resultant string is: $result" 21.Write a shell script that, given the name of the file as an argument, reads the file and creates a new file containing only lines consisting of one word. #Sript to filter those lines from supplied that contain only one word echo "Enter the name of the file. If file in current directory do not specify path" echo " If file is not in current directory plese specify path" read fname echo "The filename you entered is: $fname" if [ -e "$fname" -a -f "$fname" ] then echo "File exists"

else echo "File does not exist" exit fi sed -n '/^[ \o11]*[^ \o11]\{1,\}[ \o11]*$/p' $fname>f1 cat f1 22.Write a shell script, that changes the name of files passed as arguments to lowercase. #Sript to convert supplied filenames into lowercase. if [ $# -eq 0 ] then echo "Please enter 1 or more arguments" exit fi echo "The filenames you entered are: $*" for i in "$@" do if [ -e "$i" -a -f "$i" ] then : else echo "File $i does not exist" fi done for i in "$@" do echo echo "Original file is:$i" case=`echo $i|tr :[A-Z]: :[a-z]:` echo "New file name is:$case" done 23.Write a shell script, that given a filename as the argument, deletes all even lines in the file.(2,4,6 etc) #Script to delete all even lines in a file if [ $# -ne 1 ] then echo "Pass only 1 single argument, if of more than one characters encle in double quotes" exit fi echo "The file name you entered is: $1" if [ -e "$1" -a -f "$1" ] then echo "File exists" else echo "File does not exist" fi

cat $1|grep -n '.*'|cut -f1 -d:>nos lineno=$(cat nos) if [ -e even ] then rm even fi for i in $lineno do result=$((i % 2)) if [ $result -ne 0 ] then grep -n '.*' $1 | grep "^$i:" >>even else : fi done echo "After deleting Even lines of the supplied file contents are:" cat even 24.Write a shell script, that given a filename as the argument, deletes all odd lines in the file (1,3,5 etc) #Script to delete all even lines in a file if [ $# -ne 1 ] then echo "Pass only 1 single argument, if of more than one characters ene in double quotes" exit fi echo "The file name you entered is: $1" if [ -e "$1" -a -f "$1" ] then echo "File exists" else echo "File does not exist" fi cat $1|grep -n '.*'|cut -f1 -d:>nos lineno=$(cat nos) if [ -e even ] then rm even fi for i in $lineno do

result=$((i % 2)) if [ $result -eq 0 ] then grep -n '.*' $1 | grep "^$i:" >>even else : fi done echo "After deleting odd lines of the supplied file contents are:" cat even 25.Write a shell script that changes the first three letters of the name of all the files passed as arguments to lowercase. #Sript to convert supplied filenames into lowercase. if [ $# -eq 0 ] then echo "Please enter 1 or more arguments" exit fi echo "The filenames you entered are: $*" for i in "$@" do if [ -e "$i" -a -f "$i" ] then : else echo "File $i does not exist" fi done for i in "$@" do echo echo "Original file is:$i" case=`echo $i|grep -o "^..."|tr :[A-Z]: :[a-z]:` case1=`echo $i|sed 's/\(^...\)\(.*\)/\2/'` echo "New file name is:$case$case1" done 26.Write a shell script that, given a filename as the argument, combines odd and even lines together. In other words, lines 1 and 2 become line1, lines 3 and 4 becomes line 2, and so on. if [ $# -ne 1 ] then echo Ussage : q26 filename echo exit 1 fi

if [ ! -f $1 ] then echo File is not Ordinary file. exit 1 fi aw '(NR % 2) != 0 { print $0 ; }' $1 > temp1 aw '(NR % 2) == 0 { print $0 ; }' $1 > temp paste temp1 temp > temp3 && mv temp3 $1 rm temp temp1 echo 27.Write a shell script that, given two file names as arguments, prints the name of the file that is newer. ls -t1 > filename for i in $(cat filename) do if [ "$i" = $1 -o "$i" = $2 ] then result=$i brea fi done echo $result is newer file. 28.What is the exit status of a grep command when it finds a pattern? What is the exit status of a grep when it doesn t find the pattern? Write a one-line scrpt that verifies your answer. #Script to chec status of grep if [ $# -ne 1 ] then echo "Please enter just a single argument in double quotes" exit fi echo "The argument you entered is: $1" echo $1|grep -q '^\(.\).*\1$' if [ $? -eq 0 ] then echo "1st and last character are the same" else echo "no match found" fi echo $1|sed -n '/\(.\).*\1$/p'>/dev/null if [ $? -eq 0 ] then echo "1st and last character are same"

else echo "No match found" fi 29. Try the above program for sed and aw command.

if [ $# -ne 1 ] then echo "Please enter just a single argument in double quotes" exit fi echo "The argument you entered is: $1" echo $1|grep -q '^\(.\).*\1$' if [ $? -eq 0 ] then echo "1st and last character are the same" else echo "no match found" fi echo $1|sed -n '/\(.\).*\1$/p'>/dev/null if [ $? -eq 0 ] then echo "1st and last character are same" else echo "No match found" fi 30.Write a script, that will display the files which are lin ed to a particular file (The file of the same directory). ls l > 1.txt F $2=2 {print $NF} 1.txt

rm 1.txt 31.Develop a script that will ill all the processes belongs to a particular user ps -u ishan | cut -c 1-5 > temp aw '{ ill -9 $1 print " illed process", $1}' temp 32.Develop a script that will prompt you when a particular user logs in and logs out. flag =0 while =true do who|cut -d" " -f 1|grep "$1" exitstatus=$?

aw

#Script to chec

status of sed

if[exitstatus=1 -a $flag=1] then echo "$1 logged out" flag=0; fi if[exitstatus=0 -a flag=0] then flag=1; echo"$1 is login" fi done & 33. Develop a script that will add users. if [ $(id -u) eq 0 ] then read p Enter username: username read p Enter password: password grep ^$username /etc/passwd > /dev/null if [ $? eq 0 ] then echo $username already exists! exit 1 else useradd m p $password $username [ $? eq 0 ] && echo User has been added. || echo Failed to add a user. fi else echo Only root may add a user to the system. exit 2 fi 34.Develop a script that will delete users. read p Enter username to delete: uname

grep ^$uname /etc/passwd > /dev/null [ $uname == root ] && exit 1 || : if [ $? else echo fi 35.Develop a script in aw , that will counts for the occurrences of each character in a file. aw 'BEGIN { =0;} { for(i=1;i<=length;i++) { if(substr($0,i,1)=="s") ++; } } END {print }'Temp User $uname not found. eq 0 ] then deluser $uname echo User $uname has been deleted.

36.We have a text file containing the following fields: (1) stud.txt Uid:rollno:name:l/h(localite/hostalite) (2) log.txt Uid:loginhrs:logouthrs (a)Develop a collection of scripts that will generate the above fields. i=1 while [ $i gt 0 a $i lt 3 ] do echo 1. Enter data to Stud.txt echo 2. Enter data to Log.txt read p Enter your choice: i case $i in 1) read p Enter UID: uid read p Enter RollNo.: rn read p Enter Name: name read p Enter (L)ocalite/(H)ostalite: st echo uid:rn:name:st >> Stud.txt echo Insert Successful ;; 2) read p Enter UID: uid read p Enter LoginHr.: lin read p Enter LogoutHr: lout echo uid:lin:lout >> Log.txt echo Insert Successful ;; esac done (b)Develop a script that contains the following fields: hrs:no_of_users. for i in "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" do users=0 t=`tty` exec<Log.txt while read line do lin=`echo $line | cut -d':' -f2` lout=`echo $line | cut -d':' -f3` diff=`expr $lout - $lin` if [ $diff -eq $i ] then users=`expr $users + 1` fi done exec<$t echo "For $i hours, $users users were online" done (c)Develop a script that will find out the average time that a hostalite logged in and the average time that a localite will be logged in d)Develop a script that generates a file which contains the uid and the total time the user has logged in e)Calculate the total time spend in the lab by hostalite. o=1

while [ "$o" -eq 1 ] do echo "1> Avg. of Hostalite|Localite 2> Total time of user logged in 3> Total time spend by hostalite Enter Choice:" read ch if [ "$ch" -ge 1 -a "$ch" -le 3 ] then case $ch in 1) cnt=0 gtot=0 for stud in `grep ":h$" stud.txt` do uid=`echo $stud|cut -d ":" -f 1` tot=0 for log in `grep "^$uid" log.txt` do in=`echo $log|cut -d ":" -f 2` out=`echo $log|cut -d ":" -f 3` dif=`expr $out - $in` tot=`expr $tot + $dif` done cnt=`expr $cnt + 1` gtot=`expr $gtot + $tot` done echo "Average time for Hostalite:" `expr $gtot / $cnt` cnt=0 gtot=0 for stud in `grep ":l$" stud.txt` do uid=`echo $stud|cut -d ":" -f 1` tot=0 for log in `grep "^$uid" log.txt` do in=`echo $log|cut -d ":" -f 2` out=`echo $log|cut -d ":" -f 3` dif=`expr $out - $in` tot=`expr $tot + $dif` done cnt=`expr $cnt + 1` gtot=`expr $gtot + $tot` done echo "Average time for Localite:" `expr $gtot / $cnt` ;; 2) cnt=0 gtot=0 for stud in `cat stud.txt` do uid=`echo $stud|cut -d ":" -f 1` name=`echo $stud|cut -d ":" -f 3` tot=0

for log in `grep "^$uid" log.txt` do in=`echo $log|cut -d ":" -f 2` out=`echo $log|cut -d ": " -f 3` dif=`expr $out - $in` tot=`expr $tot + $dif` done echo "User ID : $uid Name : $name Total Time : $tot" done ;; 3) tot=0 for stud in `grep ":h$" stud.txt` do uid=`echo $stud|cut -d ":" -f 1` for log in `grep "^$uid" log.txt` do in=`echo $log|cut -d ":" -f 2` out=`echo $log|cut -d ": " -f 3` dif=`expr $out - $in` tot=`expr $tot + $dif` done done echo "Total time for Hostalite: $tot" ;; esac fi echo "continue....1 | Exit....0" read o done 37. (1) bills.txt Billno;date;cust_code;item_code;qty:rate (2) item.txt Itemcode;itemname;rate (3) cust.txt Custcode;name;area (a)develop a script that will ta e appropriate data from user, validate it and generates file bills.txt read p Enter Bill No.: bill_no read p Enter Date: bill_dt flag=0 valid_cust_code=`cat Cust.txt | cut d ; f1` while [ $flag ne 1 ] do read p Enter Customer Code: cust_code for i in $valid_cust_code

do if [ $cust_code flag=1 eq $i ] then fi done if [ $flag $ ne 1 ] then echo Invalid Customer Code. Try Again! fi done flag=0 valid_item_code=`cat Item.txt | cut d ; f1` while [ $flag ne 1 ] do read p Enter Item Code: item_code for i in $valid_item_code do if [ $item_code eq $i ] then flag=1 fi done if [ $flag ne 1 ] then echo Invalid Item Code. Try Again! fi done flag=0 while [ $flag ne 1 ] do read p Quantity: qty if [ $qty le 0 ] then echo Invalid Quantity Specified. Try Again! else flag=1 fi done rate=`cat Item.txt | grep ^$item_code | cut -d ; f3` echo $bill_no;$bill_dt;$cust_code;$item_code; $qty;$rate >> Bills.txt (b)generate the file contains billno and amount exec<Bills.txt while read line do bill_no=`echo $line | cut d ; f1` qty=`echo $line | cu d ; f5` rate=`echo $line | cu d ; f6` amt=`expr $qty \* $rate` echo $bill_no;$amt >> final_amt done echo File final_amt generated. Its contents: cat final_amt (c)find are which has maximum sales. cat > t1.txt 2> erro for area in `cat Cust.txt | cut d ; f3 | sort | uniq c | cut-c9-` do tot=0 for cust_code in `cat Cust.txt | grep $area$ | cut d ; f1` do

for var1 in `cat Bills.txt | grep ^.*;.*;$cust_code;.*;.*;.*$ ` do qty=`echo $var1 | cut d ; rate=`echo $var1 | cut d ; amt=`expr $qty \* $rate` tot=`expr $tot + $amt` done done echo $area;$tot f5` f6`

>> t1.txt done echo Area having maximum sales: `sort n r 2,3 t1.txt | head -1 | cut

d ;

f1`

38. Write a program to display all the subdirectories and their count. cnt=0 for i in * do if [ -d $1 ] then echo $1 cnt=`expr $cnt + 1` fi done echo "No. of Subdirectories = $cnt" 39. Write a program to demonstrate the user of the trap command. trap echo This is trap executing! ; exit 1 SIGINT echo We are running the script. Press Ctrl-C to cause trap to execute! read #How this wor s: We are trapping SIGINT (Ctrl-C). # if you press enter & not Ctrl-C, it exits with status 0. # if you press Ctrl-C , it prints a line & exits with status 1 40. Write a program to display all executable files in the entered directory. cnt=0 for i in * do if [ -x $1 ] then echo $1 cnt=`expr $cnt + 1` fi done echo "No. of executable files = $cnt" 41. Write a program rename files with extension .text to .txt. j=0 for I in ls *.text

do mv $I j=`expr $j + 1` done $j .txt

42. Write a aw command to print longest line in a file.

43. Write a program to mail top ten processes to the user.

ps|head -10|cat>f2 mail $(cat f1)<f2 44. Write a program to delete files to zero length. cnt=0 for i in * do if [ ! -s $i ] then rm $i cnt=`expr $cnt+1` fi done echo "No. of files that has been removed =$tot" 45. Write a grep command to display lines having only digits. grep ^[0-9]\{1,\}$ file1

46. Write a grep command to display lines having exactly 80 characters. grep ^.\{80,\}$ file1 47. Write a regular expression to validate variable of c language. sed n /^[\oii]*[ int char float ].*;$/p file1

48. Write regular expression to chec whether number is between (-32768) to (32767).

49. Write a shell script that will display files whose file name consists of only alphabets. ls | grep v [0-9] | grep [a-zA-Z]

DeleteReplyForwardSpamMove... Previous | Next | Bac to Messages Save Message Text | Full Headers Chec MailCompose Search Mail: Search MailSearch the Web Move Options

who|aw

aw max < length($0) { max=length($0); str=$0; } END { print length= max \n str;} file1

'{print $1}'| cat>f1

[New Folder] Mayur rana My Document Viru joshi gujjuchaps nccabatch2005 Forward Options As Inline Text As Attachment Reply Options Reply To Sender Reply To Everyone Address Boo Shortcuts Add Contact Add Category View Contacts View Lists Quic builder Import Contacts Addresses Options Addresses Help Calendar Shortcuts Add Event Add Tas Add Birthday Day Wee Month Year Event List Reminders Tas s Sharing Calendar Options Calendar Help Notepad Shortcuts Add Note Add Folder View Notes Notepad Options Notepad Help Advanced Search Advanced Search Copyright 2007 Yahoo Web Services India Pvt Ltd. All rights reserved. Copyright/ IP Policy - Terms of Service - Help NOTICE: We collect personal information on this site. To learn more about how we use your information, see our Privacy Policy

Das könnte Ihnen auch gefallen