mit AWK und SED lassen sich unter LINUX zahlreiche automatisierungsaufgaben erledigen

ls -la > temp.txt
awk '/root/ {print $1, $9;}' temp.txt


awk ‚  ‚{print $1}‘   file


ls files_list | awk ‚{print „mv „$1“ „$1″.new“}‘ | sh



  1. Renaming within the name:
    ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh
    (although in some cases it will fail, as in file_old_and_old)
  2. remove only files:
    ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
    or with awk alone:
    ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
    Be careful when trying this out in your home directory. We remove files!
  3. remove only directories
    ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
    ls -p | grep /$ | wk '{print "rm -r "$1}'
    or with awk alone:
    ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
    Be careful when trying this out in your home directory. We remove things!
  4. killing processes by name (in this example we kill the process called netscape):
    kill ps auxww | grep netscape | egrep -v grep | awk '{print $2}'
    or with awk alone:
    ps auxww | awk ‚$0~/netscape/&&$0!~/awk/{print $2}‘ |xargs kill
    It has to be adjusted to fit the ps command on whatever unix system you are on. Basically it is: „If the process is called netscape and it is not called ‚grep netscape‘ (or awk) then print the pid“


About the syntax of the awk command interpreter itself:

AWK Programming

* All this is fun, but each of these examples only seems to nibble away at „coins.txt“. Why not have Awk figure out everything interesting at one time?

The immediate objection to this idea is that it would be impractical to enter a lot of Awk statements on the command line, but that’s easy to fix. The commands can be written into a file, and then Awk can be told to execute the commands from that file as follows:
Given an ability to write an Awk program in this way, then what should a „master“ „coins.txt“ analysis program do? Here’s one possible output:
The following Awk program generates this information:
This program has a few interesting features:

  • Comments can be inserted in the program by preceding them with a „#“.
  • Note the statements „num_gold++“ and „num_silver++“. C programmers should understand the „++“ operator; those who are not can be assured that it simply increments the specified variable by one.
  • Multiple statements can be written on the same line by separating them with a semicolon („;“).
  • Note the use of the „printf“ statement, which offers more flexible printing capabilities than the „print“ statement. „Printf“ has the general syntax:printf(„<format_code>“,<parameters>)There is one format code for each of the parameters in the list. Each format code determines how its corresponding parameter will be printed. For example, the format code „%2d“ tells Awk to print a two-digit integer number, and the format code „%7.2f“ tells Awk to print a seven-digit floating-point number, with two digits to the right of the decimal point.Note also that, in this example, each string printed by „printf“ ends with a „\n“, which is a code for a „newline“ (ASCII line-feed code). Unlike the „print“ statement, which automatically advances the output to the next line when it prints a line, „printf“ does not automatically advance the output, and by default the next output statement will append its output to the same line. A newline forces the output to skip to the next line.

* This program can be stored in a file named „summary.awk“, and invoked as follows:
The output is:

There are no comments

Leave a Reply

Sport's by Runnerinn

Trek Bike TrekkInn FR

Tennis Smashinn

SeedingUp | Digital Content Marketing
Count per Day
  • 43Besucher heute: