Reprinted please indicate the source:Http://hi.baidu.com/leejun_2005/blog/item/702d4888928b15a60e2444b4.html Ref: http://bbs.chinaunix.net/thread-108596-1-1.html 1. Getline usage: Getline is a function used in the awk for input redirection. It can read the input from the standard input/a media transcoding queue/file, not just from the currently processed file, he gets the next line of the input and sets the value to the native variables such as NF, NR, and FNR. If a record is found, Getline returns 1, and EOF returns 0, -1 is returned if an error occurs. Man Bash: Search about Getline Getline <file sets $0 to the next record from File. Getline x sets variable X instead.
... In all cases, Getline returns 1 for Successful input, 0 for end of file, and-1 for an error. The Getline Statement of awk is used to read a record. If you have a data record similar to two physical records, Getline is particularly useful. It completes the separation of common fields (set the field variable $0 fnr nf nr ). If the operation succeeds, 1 is returned. If the operation fails, 0 is returned (to the end of the file ). To simply read a file, you can write the following code: Example: Use of Getline {While (Getline = 1) { # Process the inputted Fields } } You can also use Getline to store input data in a field, instead of processing common fields in the form of Getline variable. When this method is used, NF is set to 0, and FNR and NR are added. You can also use Getline <"FILENAME" to input data from a given file, rather than from the content listed in the command line. In this case, Getline completes the general field separation (set the field variables $0 and NF ). If the object does not exist,-1 is returned, success is returned, 1 is returned, and 0 is returned, indicating failure. You can read data from a given file to a variable, or use stdin (standard input device) or a variable containing the file name to replace filename. It is worth noting that FNR and NR are not modified when this method is used. Another way to use the Getline statement is to accept input from Unix commands, for example, the following example: For example, input is accepted from Unix commands. {While ("who-U" | Getline) { # Process each line from the WHO command } } You can also use the following format: "Command" | Getline variable For example: /Home/Lee # awk 'in in {"date" | Getline var; split (VAR, a); print a [4]}' 16:46:26 /Home/Lee # awk 'in in {Getline name <"/dev/tty"; print "yourname:" name }' Lee Yourname: Lee Getline values in different environments: Table-Getline-variants summarizes the eightvariants of Getline, listing which built-in Variables are set by each one.
Variant |
Effect |
Getline |
Sets $0, NF, FNR, and NR |
GetlineVaR |
SetsVaR, FNR, and NR |
Getline <File |
Sets $0 and NF |
GetlineVaR<File
|
SetsVaR |
Command| Getline
|
Sets $0 and NF |
Command| Getline VaR |
SetsVaR |
Command| & Getline
|
Sets $0 and NF. This is a gawk Extension
|
Command| & Getline VaR |
SetsVaR. This is a gawk Extension
|
========================================================== ========================================================== = 2. Example of Getline usage: # Getline in awk obtains the execution result of shell commands (not the return value) June @~ 21:34:59> Echo | awk '{"Echo \" 1 + 2 \ "| BC" | Getline V; print v }'
3 June @~ 21:35:02> Note: Make sure to close ("cmd") after calling the shell command using pipelines in awk ") Case Analysis see: http://hi.baidu.com/leejun_2005/blog/item/88f7b9838e794785f703a60b.html By using pipelines in the awk, you can send the shell command output to the awk. $ Awk 'in in {"date" | Getline date; print date ;}' Sun Mar 22:21:52 2009 # The Return Value of the Getline status can be assigned a value. Awk '{if ($6 !~ /6/) {TMP = $0} if ($6 ~ /6/) {c = Getline s; If (C = 0) S = ""; print TMP, $0, s} 'file
Awk Getline receives user input in two forms: Getline string <"/dev/tty" Getline string <"-" 1) The user is prompted to enter the Getline: awk 'in in {print "input something"; Getline var <"-"; print var} '# Where "-" is the standard input, many tools support "-", such as tar/cat. 2) obtain the location parameter of the awk Script: awk 'in in {print argv [1], argv [2]} 'a B How does Getline save all shell output: Awk 'in in {SRS = RS; RS = ""; "ls./" | Getline TMP; RS = SRS; print TMP }' Getline directly reads the file. Note that begin is a preprocessing part, not an action part. At this time, the file is not ready to be processed, and the pointer does not point to row 1st of the file. During execution, the file pointer will not be moved. In short, some awks in begin do not have pointers. At this time, only Getline pointers are available, and awk only processes pointers in the {} action section. Awk 'in in {While (Getline d <"AA") print d }' SEQ 10 | awk '{Getline d <"AA"; print d}' # If Getline directly reads a file, it reads data row by row, because only Getline pointers are available at this time, there is no awk pointer to process the file. Getline prints even rows: SEQ 10 | awk '{Getline; print }' # Note that this is different from seq 10 | awk 'in in {While (Getline) print! June @~ 23:07:23> SEQ 10 | awk 'I ++ % 2' 2 4 6 8 10 June @~ 23:07:27> Awk Getline interchange: SEQ 10 | awk '{If (Getline TMP) print TMP; print}' # generally, it is used to determine whether the returned value of Getline is greater than 0. 2 1 4 3 6 5 8 7 10 9 In awk, you can also run the shell command through system: system will return the status code Awk 'in in {system ("ls-L ")}' PS: system can also use the awk variable: System ("Echo", $1) |