Some useful UNIX file utilities:
Example: executing
wc pub/swap.cin my home directory produces:
30 92 537 pub/swap.c
Example: executing
wc pub/*.cin my home directory produces a count of lines, words and characters for each file ending in .c in my pub subdirectory.
Format:
grep pattern file
Example: executing
grep val1 swap.cin my pub subdirectory finds all lines containing val1 in file swap.c, producing:
int val1 = 3, val2 = 10; printf("The value of val1 is %d\n", val1); swap(&val1, &val2); printf("The value of val1 is %d\n", val1);
Example: executing
grep temp *.cin my pub subdirectory finds all lines containing temp in all files ending in .c, producing:
array4.c: float temp; array4.c: temp = cgs[i]; array4.c: cgs[j] = temp; array4.c: float temp; array4.c: temp = *i; array4.c: *j = temp; cellFahr.c: printf("Enter the celsius temperature: "); prog6.c: bookrec temp; prog6.c: temp = *i; prog6.c: *j = temp; swap.c: int temp; swap.c: temp = *i; swap.c: *j = temp;Note that each line of output is labeled with the file it came from.
this is a text file by me.then
sort atextfileproduces:
a text by me. file this is
Some useful commands:
UNIX commands can also be executed from inside C programs by calling the system() function whose prototype is in stdlib.h.
Example:
system("clear");clears the screen.
Example:
system("cal 9 1996 > month.txt");writes a calender for September 1996 into file month.txt. The program can then access the file.