[Linux-shell] AWK

Go to the first, previous, next, last section, table of contents.


Printing Output

One of the most common actions is to print, or output, some or all of the input. You use the print statement for simple output. You use the printf statement for fancier formatting. Both are described in this chapter.

The print Statement

The print statement does output with simple, standardized formatting. You specify only the strings or numbers to be printed, in a list separated by commas. They are output, separated by single spaces, followed by a newline. The statement looks like this:

print item1, item2, ...

The entire list of items may optionally be enclosed in parentheses. The parentheses are necessary if any of the item expressions uses the `>‘ relational operator; otherwise it could be confused with a redirection (see section Redirecting Output of print and printf).

The items to be printed can be constant strings or numbers, fields of the current record (such as $1), variables, or any awk expressions. Numeric values are converted to strings, and then printed.

The print statement is completely general for computing what values to print. However, with two exceptions, you cannot specify how to print them--how many columns, whether to use exponential notation or not, and so on. (For the exceptions, see section Output Separators, and section Controlling Numeric Output with print.) For that, you need the printfstatement (see section Using printf Statements for Fancier Printing).

The simple statement `print‘ with no items is equivalent to `print $0‘: it prints the entire current record. To print a blank line, use `print ""‘, where "" is the empty string.

To print a fixed piece of text, use a string constant such as "Don‘t Panic" as one item. If you forget to use the double-quote characters, your text will be taken as an awk expression, and you will probably get an error. Keep in mind that a space is printed between any two items.

Each print statement makes at least one line of output. But it isn‘t limited to one line. If an item value is a string that contains a newline, the newline is output along with the rest of the string. A single print can make any number of lines this way.

Examples of print Statements

Here is an example of printing a string that contains embedded newlines (the `\n‘ is an escape sequence, used to represent the newline character; see section Escape Sequences):

$ awk ‘BEGIN { print "line one\nline two\nline three" }‘
-| line one
-| line two
-| line three

Here is an example that prints the first two fields of each input record, with a space between them:

$ awk ‘{ print $1, $2 }‘ inventory-shipped
-| Jan 13
-| Feb 15
-| Mar 15
...

A common mistake in using the print statement is to omit the comma between two items. This often has the effect of making the items run together in the output, with no space. The reason for this is that juxtaposing two string expressions in awk means to concatenate them. Here is the same program, without the comma:

$ awk ‘{ print $1 $2 }‘ inventory-shipped
-| Jan13
-| Feb15
-| Mar15
...

To someone unfamiliar with the file `inventory-shipped‘, neither example‘s output makes much sense. A heading line at the beginning would make it clearer. Let‘s add some headings to our table of months ($1) and green crates shipped ($2). We do this using the BEGIN pattern (see section The BEGIN and END Special Patterns) to force the headings to be printed only once:

awk ‘BEGIN {  print "Month Crates"
              print "----- ------" }
           {  print $1, $2 }‘ inventory-shipped

Did you already guess what happens? When run, the program prints the following:

Month Crates
----- ------
Jan 13
Feb 15
Mar 15
...

The headings and the table data don‘t line up! We can fix this by printing some spaces between the two fields:

awk ‘BEGIN { print "Month Crates"
             print "----- ------" }
           { print $1, "     ", $2 }‘ inventory-shipped

You can imagine that this way of lining up columns can get pretty complicated when you have many columns to fix. Counting spaces for two or three columns can be simple, but more than this and you can get lost quite easily. This is why the printf statement was created (see section Using printf Statements for Fancier Printing); one of its specialties is lining up columns of data.

As a side point, you can continue either a print or printf statement simply by putting a newline after any comma (see section awk Statements Versus Lines).

Output Separators

As mentioned previously, a print statement contains a list of items, separated by commas. In the output, the items are normally separated by single spaces. This need not be the case; a single space is only the default. You can specify any string of characters to use as the output field separator by setting the built-in variable OFS. The initial value of this variable is the string " ", that is, a single space.

The output from an entire print statement is called an output record. Each print statement outputs one output record and then outputs a string called the output record separator. The built-in variable ORS specifies this string. The initial value of ORS is the string "\n", i.e. a newline character; thus, normally each print statement makes a separate line.

You can change how output fields and records are separated by assigning new values to the variables OFS and/or ORS. The usual place to do this is in the BEGIN rule (see section TheBEGIN and END Special Patterns), so that it happens before any input is processed. You may also do this with assignments on the command line, before the names of your input files, or using the `-v‘ command line option (see section Command Line Options).

The following example prints the first and second fields of each input record separated by a semicolon, with a blank line added after each line:

$ awk ‘BEGIN { OFS = ";"; ORS = "\n\n" }
>            { print $1, $2 }‘ BBS-list
-| aardvark;555-5553
-|
-| alpo-net;555-3412
-|
-| barfly;555-7685
...

If the value of ORS does not contain a newline, all your output will be run together on a single line, unless you output newlines some other way.

Controlling Numeric Output with print

When you use the print statement to print numeric values, awk internally converts the number to a string of characters, and prints that string. awk uses the sprintf function to do this conversion (see section Built-in Functions for String Manipulation). For now, it suffices to say that the sprintf function accepts a format specification that tells it how to format numbers (or strings), and that there are a number of different ways in which numbers can be formatted. The different format specifications are discussed more fully in section Format-Control Letters.

The built-in variable OFMT contains the default format specification that print uses with sprintf when it wants to convert a number to a string for printing. The default value of OFMT is"%.6g". By supplying different format specifications as the value of OFMT, you can change how print will print your numbers. As a brief example:

$ awk ‘BEGIN {
>   OFMT = "%.0f"  # print numbers as integers (rounds)
>   print 17.23 }‘
-| 17

According to the POSIX standard, awk‘s behavior will be undefined if OFMT contains anything but a floating point conversion specification (d.c.).

Using printf Statements for Fancier Printing

If you want more precise control over the output format than print gives you, use printf. With printf you can specify the width to use for each item, and you can specify various formatting choices for numbers (such as what radix to use, whether to print an exponent, whether to print a sign, and how many digits to print after the decimal point). You do this by supplying a string, called the format string, which controls how and where to print the other arguments.

Introduction to the printf Statement

The printf statement looks like this:

printf format, item1, item2, ...

The entire list of arguments may optionally be enclosed in parentheses. The parentheses are necessary if any of the item expressions use the `>‘ relational operator; otherwise it could be confused with a redirection (see section Redirecting Output of print and printf).

The difference between printf and print is the format argument. This is an expression whose value is taken as a string; it specifies how to output each of the other arguments. It is called the format string.

The format string is very similar to that in the ANSI C library function printf. Most of format is text to be output verbatim. Scattered among this text are format specifiers, one per item. Each format specifier says to output the next item in the argument list at that place in the format.

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if you want a newline, you must include one in the format string. The output separator variables OFS and ORS have no effect on printf statements. For example:

BEGIN {
   ORS = "\nOUCH!\n"; OFS = "!"
   msg = "Don‘t Panic!"; printf "%s\n", msg
}

This program still prints the familiar `Don‘t Panic!‘ message.

Format-Control Letters

A format specifier starts with the character `%‘ and ends with a format-control letter; it tells the printf statement how to output one item. (If you actually want to output a `%‘, write`%%‘.) The format-control letter specifies what kind of value to print. The rest of the format specifier is made up of optional modifiers which are parameters to use, such as the field width.

Here is a list of the format-control letters:

c
This prints a number as an ASCII character. Thus, `printf "%c", 65‘ outputs the letter `A‘. The output for a string value is the first character of the string.
d
i
These are equivalent. They both print a decimal integer. The `%i‘ specification is for compatibility with ANSI C.
e
E
This prints a number in scientific (exponential) notation. For example,

printf "%4.3e\n", 1950

prints `1.950e+03‘, with a total of four significant figures of which three follow the decimal point. The `4.3‘ are modifiers, discussed below. `%E‘ uses `E‘ instead of `e‘ in the output.

f
This prints a number in floating point notation. For example,

printf "%4.3f", 1950

prints `1950.000‘, with a total of four significant figures of which three follow the decimal point. The `4.3‘ are modifiers, discussed below.

g
G
This prints a number in either scientific notation or floating point notation, whichever uses fewer characters. If the result is printed in scientific notation, `%G‘ uses `E‘ instead of`e‘.
o
This prints an unsigned octal integer. (In octal, or base-eight notation, the digits run from `0‘ to `7‘; the decimal number eight is represented as `10‘ in octal.)
s
This prints a string.
x
X
This prints an unsigned hexadecimal integer. (In hexadecimal, or base-16 notation, the digits are `0‘ through `9‘ and `a‘ through `f‘. The hexadecimal digit `f‘ represents the decimal number 15.) `%X‘ uses the letters `A‘ through `F‘ instead of `a‘ through `f‘.
%
This isn‘t really a format-control letter, but it does have a meaning when used after a `%‘: the sequence `%%‘ outputs one `%‘. It does not consume an argument, and it ignores any modifiers.

When using the integer format-control letters for values that are outside the range of a C long integer, gawk will switch to the `%g‘ format specifier. Other versions of awk may print invalid values, or do something else entirely (d.c.).

Modifiers for printf Formats

A format specification can also include modifiers that can control how much of the item‘s value is printed and how much space it gets. The modifiers come between the `%‘ and the format-control letter. In the examples below, we use the bullet symbol "*" to represent spaces in the output. Here are the possible modifiers, in the order in which they may appear:

-
The minus sign, used before the width modifier (see below), says to left-justify the argument within its specified width. Normally the argument is printed right-justified in the specified width. Thus,

printf "%-4s", "foo"

prints `foo*‘.

space
For numeric conversions, prefix positive values with a space, and negative values with a minus sign.
+
The plus sign, used before the width modifier (see below), says to always supply a sign for numeric conversions, even if the data to be formatted is positive. The `+‘ overrides the space modifier.
#
Use an "alternate form" for certain control letters. For `%o‘, supply a leading zero. For `%x‘, and `%X‘, supply a leading `0x‘ or `0X‘ for a non-zero result. For `%e‘, `%E‘, and`%f‘, the result will always contain a decimal point. For `%g‘, and `%G‘, trailing zeros are not removed from the result.
0
A leading `0‘ (zero) acts as a flag, that indicates output should be padded with zeros instead of spaces. This applies even to non-numeric output formats (d.c.). This flag only has an effect when the field width is wider than the value to be printed.
width
This is a number specifying the desired minimum width of a field. Inserting any number between the `%‘ sign and the format control character forces the field to be expanded to this width. The default way to do this is to pad with spaces on the left. For example,

printf "%4s", "foo"

prints `*foo‘. The value of width is a minimum width, not a maximum. If the item value requires more than width characters, it can be as wide as necessary. Thus,

printf "%4s", "foobar"

prints `foobar‘. Preceding the width with a minus sign causes the output to be padded with spaces on the right, instead of on the left.

.prec
This is a number that specifies the precision to use when printing. For the `e‘, `E‘, and `f‘ formats, this specifies the number of digits you want printed to the right of the decimal point. For the `g‘, and `G‘ formats, it specifies the maximum number of significant digits. For the `d‘, `o‘, `i‘, `u‘, `x‘, and `X‘ formats, it specifies the minimum number of digits to print. For a string, it specifies the maximum number of characters from the string that should be printed. Thus,

printf "%.4s", "foobar"

prints `foob‘.

The C library printf‘s dynamic width and prec capability (for example, "%*.*s") is supported. Instead of supplying explicit width and/or prec values in the format string, you pass them in the argument list. For example:

w = 5
p = 3
s = "abcdefg"
printf "%*.*s\n", w, p, s

is exactly equivalent to

s = "abcdefg"
printf "%5.3s\n", s

Both programs output `**abc‘.

Earlier versions of awk did not support this capability. If you must use such a version, you may simulate this feature by using concatenation to build up the format string, like so:

w = 5
p = 3
s = "abcdefg"
printf "%" w "." p "s\n", s

This is not particularly easy to read, but it does work.

C programmers may be used to supplying additional `l‘ and `h‘ flags in printf format strings. These are not valid in awk. Most awk implementations silently ignore these flags. If `--lint‘ is provided on the command line (see section Command Line Options), gawk will warn about their use. If `--posix‘ is supplied, their use is a fatal error.

Examples Using printf

Here is how to use printf to make an aligned table:

awk ‘{ printf "%-10s %s\n", $1, $2 }‘ BBS-list

prints the names of bulletin boards ($1) of the file `BBS-list‘ as a string of 10 characters, left justified. It also prints the phone numbers ($2) afterward on the line. This produces an aligned two-column table of names and phone numbers:

$ awk ‘{ printf "%-10s %s\n", $1, $2 }‘ BBS-list
-| aardvark   555-5553
-| alpo-net   555-3412
-| barfly     555-7685
-| bites      555-1675
-| camelot    555-0542
-| core       555-2912
-| fooey      555-1234
-| foot       555-6699
-| macfoo     555-6480
-| sdace      555-3430
-| sabafoo    555-2127

Did you notice that we did not specify that the phone numbers be printed as numbers? They had to be printed as strings because the numbers are separated by a dash. If we had tried to print the phone numbers as numbers, all we would have gotten would have been the first three digits, `555‘. This would have been pretty confusing.

We did not specify a width for the phone numbers because they are the last things on their lines. We don‘t need to put spaces after them.

We could make our table look even nicer by adding headings to the tops of the columns. To do this, we use the BEGIN pattern (see section The BEGIN and END Special Patterns) to force the header to be printed only once, at the beginning of the awk program:

awk ‘BEGIN { print "Name      Number"
             print "----      ------" }
     { printf "%-10s %s\n", $1, $2 }‘ BBS-list

Did you notice that we mixed print and printf statements in the above example? We could have used just printf statements to get the same results:

awk ‘BEGIN { printf "%-10s %s\n", "Name", "Number"
             printf "%-10s %s\n", "----", "------" }
     { printf "%-10s %s\n", $1, $2 }‘ BBS-list

By printing each column heading with the same format specification used for the elements of the column, we have made sure that the headings are aligned just like the columns.

The fact that the same format specification is used three times can be emphasized by storing it in a variable, like this:

awk ‘BEGIN { format = "%-10s %s\n"
             printf format, "Name", "Number"
             printf format, "----", "------" }
     { printf format, $1, $2 }‘ BBS-list

See if you can use the printf statement to line up the headings and table data for our `inventory-shipped‘ example covered earlier in the section on the print statement (see sectionThe print Statement).

Redirecting Output of print and printf

So far we have been dealing only with output that prints to the standard output, usually your terminal. Both print and printf can also send their output to other places. This is calledredirection.

A redirection appears after the print or printf statement. Redirections in awk are written just like redirections in shell commands, except that they are written inside the awk program.

There are three forms of output redirection: output to a file, output appended to a file, and output through a pipe to another command. They are all shown for the print statement, but they work identically for printf also.

print items > output-file
This type of redirection prints the items into the output file output-file. The file name output-file can be any expression. Its value is changed to a string and then used as a file name (see section Expressions). When this type of redirection is used, the output-file is erased before the first output is written to it. Subsequent writes to the same output-file do not erase output-file, but append to it. If output-file does not exist, then it is created. For example, here is how an awk program can write a list of BBS names to a file `name-list‘ and a list of phone numbers to a file `phone-list‘. Each output file contains one name or number per line.

$ awk ‘{ print $2 > "phone-list"
>        print $1 > "name-list" }‘ BBS-list
$ cat phone-list
-| 555-5553
-| 555-3412
...
$ cat name-list
-| aardvark
-| alpo-net
...
print items >> output-file
This type of redirection prints the items into the pre-existing output file output-file. The difference between this and the single-`>‘ redirection is that the old contents (if any) ofoutput-file are not erased. Instead, the awk output is appended to the file. If output-file does not exist, then it is created.
print items | command
It is also possible to send output to another program through a pipe instead of into a file. This type of redirection opens a pipe to command and writes the values of items through this pipe, to another process created to execute command. The redirection argument command is actually an awk expression. Its value is converted to a string, whose contents give the shell command to be run. For example, this produces two files, one unsorted list of BBS names and one list sorted in reverse alphabetical order:

awk ‘{ print $1 > "names.unsorted"
       command = "sort -r > names.sorted"
       print $1 | command }‘ BBS-list

Here the unsorted list is written with an ordinary redirection while the sorted list is written by piping through the sort utility. This example uses redirection to mail a message to a mailing list `bug-system‘. This might be useful when trouble is encountered in an awk script run periodically for system maintenance.

report = "mail bug-system"
print "Awk script failed:", $0 | report
m = ("at record number " FNR " of " FILENAME)
print m | report
close(report)

The message is built using string concatenation and saved in the variable m. It is then sent down the pipeline to the mail program. We call the close function here because it‘s a good idea to close the pipe as soon as all the intended output has been sent to it. See section Closing Input and Output Files and Pipes, for more information on this. This example also illustrates the use of a variable to represent a file or command: it is not necessary to always use a string constant. Using a variable is generally a good idea, sinceawk requires you to spell the string value identically every time.

Redirecting output using `>‘, `>>‘, or `|‘ asks the system to open a file or pipe only if the particular file or command you‘ve specified has not already been written to by your program, or if it has been closed since it was last written to.

As mentioned earlier (see section Summary of getline Variants), many awk implementations limit the number of pipelines an awk program may have open to just one! In gawk, there is no such limit. You can open as many pipelines as the underlying operating system will permit.

Special File Names in gawk

Running programs conventionally have three input and output streams already available to them for reading and writing. These are known as the standard inputstandard output, andstandard error output. These streams are, by default, connected to your terminal, but they are often redirected with the shell, via the `<‘, `<<‘, `>‘, `>>‘, `>&‘ and `|‘ operators. Standard error is typically used for writing error messages; the reason we have two separate streams, standard output and standard error, is so that they can be redirected separately.

In other implementations of awk, the only way to write an error message to standard error in an awk program is as follows:

print "Serious error detected!" | "cat 1>&2"

This works by opening a pipeline to a shell command which can access the standard error stream which it inherits from the awk process. This is far from elegant, and is also inefficient, since it requires a separate process. So people writing awk programs often neglect to do this. Instead, they send the error messages to the terminal, like this:

print "Serious error detected!" > "/dev/tty"

This usually has the same effect, but not always: although the standard error stream is usually the terminal, it can be redirected, and when that happens, writing to the terminal is not correct. In fact, if awk is run from a background job, it may not have a terminal at all. Then opening `/dev/tty‘ will fail.

gawk provides special file names for accessing the three standard streams. When you redirect input or output in gawk, if the file name matches one of these special names, then gawkdirectly uses the stream it stands for.

`/dev/stdin‘
The standard input (file descriptor 0).
`/dev/stdout‘
The standard output (file descriptor 1).
`/dev/stderr‘
The standard error output (file descriptor 2).
`/dev/fd/N
The file associated with file descriptor N. Such a file must have been opened by the program initiating the awk execution (typically the shell). Unless you take special pains in the shell from which you invoke gawk, only descriptors 0, 1 and 2 are available.

The file names `/dev/stdin‘`/dev/stdout‘, and `/dev/stderr‘ are aliases for `/dev/fd/0‘`/dev/fd/1‘, and `/dev/fd/2‘, respectively, but they are more self-explanatory.

The proper way to write an error message in a gawk program is to use `/dev/stderr‘, like this:

print "Serious error detected!" > "/dev/stderr"

gawk also provides special file names that give access to information about the running gawk process. Each of these "files" provides a single record of information. To read them more than once, you must first close them with the close function (see section Closing Input and Output Files and Pipes). The filenames are:

`/dev/pid‘
Reading this file returns the process ID of the current process, in decimal, terminated with a newline.
`/dev/ppid‘
Reading this file returns the parent process ID of the current process, in decimal, terminated with a newline.
`/dev/pgrpid‘
Reading this file returns the process group ID of the current process, in decimal, terminated with a newline.
`/dev/user‘
Reading this file returns a single record terminated with a newline. The fields are separated with spaces. The fields represent the following information:

$1
The return value of the getuid system call (the real user ID number).
$2
The return value of the geteuid system call (the effective user ID number).
$3
The return value of the getgid system call (the real group ID number).
$4
The return value of the getegid system call (the effective group ID number).

If there are any additional fields, they are the group IDs returned by getgroups system call. (Multiple groups may not be supported on all systems.)

These special file names may be used on the command line as data files, as well as for I/O redirections within an awk program. They may not be used as source files with the `-f‘option.

Recognition of these special file names is disabled if gawk is in compatibility mode (see section Command Line Options).

Caution: Unless your system actually has a `/dev/fd‘ directory (or any of the other above listed special files), the interpretation of these file names is done by gawk itself. For example, using `/dev/fd/4‘ for output will actually write on file descriptor 4, and not on a new file descriptor that was dup‘ed from file descriptor 4. Most of the time this does not matter; however, it is important to not close any of the files related to file descriptors 0, 1, and 2. If you do close one of these files, unpredictable behavior will result.

The special files that provide process-related information may disappear in a future version of gawk. See section Probable Future Extensions.

Closing Input and Output Files and Pipes

If the same file name or the same shell command is used with getline (see section Explicit Input with getline) more than once during the execution of an awk program, the file is opened (or the command is executed) only the first time. At that time, the first record of input is read from that file or command. The next time the same file or command is used ingetline, another record is read from it, and so on.

Similarly, when a file or pipe is opened for output, the file name or command associated with it is remembered by awk and subsequent writes to the same file or command are appended to the previous writes. The file or pipe stays open until awk exits.

This implies that if you want to start reading the same file again from the beginning, or if you want to rerun a shell command (rather than reading more output from the command), you must take special steps. What you must do is use the close function, as follows:

close(filename)

or

close(command)

The argument filename or command can be any expression. Its value must exactly match the string that was used to open the file or start the command (spaces and other "irrelevant" characters included). For example, if you open a pipe with this:

"sort -r names" | getline foo

then you must close it with this:

close("sort -r names")

Once this function call is executed, the next getline from that file or command, or the next print or printf to that file or command, will reopen the file or rerun the command.

Because the expression that you use to close a file or pipeline must exactly match the expression used to open the file or run the command, it is good practice to use a variable to store the file name or command. The previous example would become

sortcom = "sort -r names"
sortcom | getline foo
...
close(sortcom)

This helps avoid hard-to-find typographical errors in your awk programs.

Here are some reasons why you might need to close an output file:

  • To write a file and read it back later on in the same awk program. Close the file when you are finished writing it; then you can start reading it with getline.
  • To write numerous files, successively, in the same awk program. If you don‘t close the files, eventually you may exceed a system limit on the number of open files in one process. So close each one when you are finished writing it.
  • To make a command finish. When you redirect output through a pipe, the command reading the pipe normally continues to try to read input as long as the pipe is open. Often this means the command cannot really do its work until the pipe is closed. For example, if you redirect output to the mail program, the message is not actually sent until the pipe is closed.
  • To run the same program a second time, with the same arguments. This is not the same thing as giving more input to the first run! For example, suppose you pipe output to themail program. If you output several lines redirected to this pipe without closing it, they make a single message of several lines. By contrast, if you close the pipe after each line of output, then each line makes a separate message.

close returns a value of zero if the close succeeded. Otherwise, the value will be non-zero. In this case, gawk sets the variable ERRNO to a string describing the error that occurred.

If you use more files than the system allows you to have open, gawk will attempt to multiplex the available open files among your data files. gawk‘s ability to do this depends upon the facilities of your operating system: it may not always work. It is therefore both good practice and good portability advice to always use close on your files when you are done with them.



Go to the first, previous, next, last section, table of contents.

时间: 2024-08-09 19:43:29

[Linux-shell] AWK的相关文章

linux shell awk 语法

引用:http://blog.csdn.net/weekly123/article/details/1465675 inux shell awk 语法   Awk 是一种非常好的语言,同时有一个非常奇怪的名称.在本系列(共三篇文章)的第一篇文章中,Daniel Robbins 将使您迅速掌握 awk 编程技巧.随着本系列的进展,将讨论更高级的主题,最后将演示一个真正的高级 awk 演示程序.捍卫 awk在本系列文章中,我将使您成为精通 awk 的编码人员.我承认,awk 并没有一个非常好听且又非

Linux Shell常用技巧(四) awk

Linux Shell常用技巧(四) awk 九.  awk实用功能: 和sed一样,awk也是逐行扫描文件的,从第一行到最后一行,寻找匹配特定模板的行,并在这些行上运行"选择"动作.如果一个模板没有指定动作,这些匹配的行就被显示在屏幕上.如果一个动作没有模板,所有被动作指定的行都被处理.       1.  awk的基本格式:    /> awk 'pattern' filename    /> awk '{action}' filename    /> awk '

Linux Shell常用技巧(五) awk编程

Linux Shell常用技巧(五) awk编程 十一.  awk编程:    1.  变量:    在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义.变量的类型可以是数字.字符串.根据使用的不同,未初始化变量的值为0或空白字符串" ",这主要取决于变量应用的上下文.下面为变量的赋值负号列表: 符号 含义 等价形式 = a = 5 a = 5 += a = a + 5 a += 5 -= a = a - 5 a -= 5 *= a = a * 5 a *= 5 /= a =

Linux - Shell - cut: 低配 awk

概述 简述 shell 命令行工具 cut 背景 偶尔需要用 awk 来筛选特定的列 awk 很是强大 但是强大的背后, 却伴随着复杂 其实同样的功能, awk 也没有复杂多少 如果是 简单的任务, cut 工具完全是可以胜任的 切割行内的特定位置 切割行内的特定字段 描述可能不是很准确, 下面会有例子 1. 准备 os centos7 文件 cutdemo01 1:2:3:4:5 1:2:3:4:5 1:2:3:4:5 cutdemo02 1 2 3 4 5 1 2 3 4 5 1 2 3 4

Linux Shell sort 指定排序第几列

ip.txt 里存储着ip信息 统计排序后取前10条 awk '{cnt[$1]++} END{for (ip in cnt) print ip":"cnt[ip]}' ip.txt | sort -k 2 -rn -t":" | head -n 10 awk '{cnt[$1]++} END{for (ip in cnt) print cnt[ip],ip}' ip.txt | sort -rn | head -n 10 sort -k  根据第几列排序  -n

Linux shell脚本-基础学习笔记

Linux脚本能力不是太强,最近再补习下,毕竟linux shell在日常工作中还是很普遍的, 用起来更方便.省时省力. 以下是学习笔记,偏理论,后面有几个例子,供参考. shell脚本组成元素系统命令.文本处理工具(grep\sed等).变量.条件判断.循环结构和函数 -------------------------------------------- 三剑客:grep,sed,awk,还有wc,sort,head等 ------------------------------------

Linux shell脚本基础学习详细介绍(完整版)一

Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Linux 脚本编写基础◆1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执

37条常用Linux Shell命令组合

序号 任务 命令组合 1 删除0字节文件 find . -type f -size 0 -exec rm -rf {} \; find . type f -size 0 -delete 2 查看进程,按内存从大到小排列 ps -e -o “%C : %p : %z : %a”|sort -k5 -nr 3 按cpu利用率从大到小排列 ps -e -o “%C : %p : %z : %a”|sort -nr 4 打印说cache里的URL grep -r -a jpg /data/cache/*

linux shell中的比较符号与特殊符号介绍

shell字符串比较.判断是否为数字  二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别.  整数比较  -eq 等于,如:if [ "$a" -eq "$b" ]  -ne  不等于,如:if [ "$a" -ne "$b" ]  -gt 大于,如:if [ "$a" -gt "$b" ]  -ge 大于等于,如:if [  "$a" -ge "

Linux shell一行流编程实践

Linux下很多命令用起来真相当方便,尤其是进行批处理操作时.(话说感觉这种程序也不复杂,windows咋一直不搞一个好用的shell呢) 这里列出一些常用shell操作的应用,具体命令的用法与解释就不列了,网上有很多很好的教程. 批量重命名 假如当前目录下有若干.wma文件,我希望把它们批量转成.mp3文件 例: 001.wma -> 001.mp3 解决方案: awk ? 1 ls * | awk -F '.' '{print "mv "$0" "$1&q