s i s t e m a o p e r a c i o n a l m a g n u x l i n u x | ~/ · documentação · suporte · sobre |
Next
Previous
Contents
11. Real Life Examples
UNIX' core idea is that there are many simple commands that can linked together via piping and redirection to accomplish even really complex tasks. Have a look at the following examples. I'll only explain the most complex ones; for the others, please study the above sections and the man pages. Problem: Solution:
Problem: I have a file containing a list of words. I want to sort it in reverse order and print it. Solution:
Problem: my data file has some repeated lines! How do I get rid of them? Solution:
Problem: I have a file called 'mypaper.txt' or 'mypaper.tex' or some such somewhere, but I don't remember where I put it. How do I find it? Solution:
Explanation: Problem: I have a text file containing the word 'entropy' in this
directory, is there anything like Solution: yes, try
Problem: somewhere I have text files containing the word 'entropy', I'd
like to know which and where they are. Under VMS I'd use Solution:
Explanation: In alternative, write the following script:
#!/bin/sh # rgrep: recursive grep if [ $# != 3 ] then echo "Usage: rgrep --switches 'pattern' 'directory'" exit 1 fi find $3 -name "*" -exec grep $1 $2 {} \; 2> /dev/null Explanation: Problem: I have a data file that has two header lines, then every line has 'n' data, not necessarily equally spaced. I want the 2nd and 5th data value of each line. Shall I write a Fortran program...? Solution: nope. This is quicker:
Explanation: the command Problem: I've downloaded an FTP site's Solution:
Explanation: Problem: I've written a Fortran program, Solution: a very short script. Make your program look for the data
file '
#!/bin/sh # myprog.sh: run the same command on many different files # usage: myprog.sh *.dat for file in $* # for all parameters (e.g. *.dat) do # append the file name to result.dat echo -n "${file}: " >> results.dat # copy current argument to mydata.dat, run myprog # and append the output to results.dat cp ${file} mydata.dat ; myprog >> results.dat done Problem: I want to replace `geology' with `geophysics' in all my text files. Shall I edit them all manually? Solution: nope. Write this shell script:
#!/bin/sh # replace $1 with $2 in $* # usage: replace "old-pattern" "new-pattern" file [file...] OLD=$1 # first parameter of the script NEW=$2 # second parameter shift ; shift # discard the first 2 parameters: the next are the file names for file in $* # for all files given as parameters do # replace every occurrence of OLD with NEW, save on a temporary file sed "s/$OLD/$NEW/g" ${file} > ${file}.new # rename the temporary file as the original file /bin/mv ${file}.new ${file} done Problem: I have some data files, I don't know their length and have to remove their last but one and last but two lines. Er... manually? Solution: no, of course. Write this script:
#!/bin/sh # prune.sh: removes n-1th and n-2th lines from files # usage: prune.sh file [file...] for file in $* # for every parameter do LINES=`wc -l $file | awk '{print $1}'` # number of lines in file LINES=`expr $LINES - 3` # LINES = LINES - 3 head -n $LINES $file > $file.new # output first LINES lines tail -n 1 $file >> $file.new # append last line done I hope these examples whetted your appetite...
Next Previous Contents |