mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-19 22:51:41 +08:00
20160823-1 选题
This commit is contained in:
parent
1a02b41b7e
commit
5b8d2d6419
@ -1,106 +0,0 @@
|
||||
How to Allow Awk to Use Shell Variables – Part 11
|
||||
===========================================
|
||||
|
||||
When we write shell scripts, we normally include other smaller programs or commands such as Awk operations in our scripts. In the case of Awk, we have to find ways of passing some values from the shell to Awk operations.
|
||||
|
||||
This can be done by using shell variables within Awk commands, and in this part of the series, we shall learn how to allow Awk to use shell variables that may contain values we want to pass to Awk commands.
|
||||
|
||||
There possibly two ways you can enable Awk to use shell variables:
|
||||
|
||||
### 1. Using Shell Quoting
|
||||
|
||||
Let us take a look at an example to illustrate how you can actually use shell quoting to substitute the value of a shell variable in an Awk command. In this example, we want to search for a username in the file /etc/passwd, filter and print the user’s account information.
|
||||
|
||||
Therefore, we can write a `test.sh` script with the following content:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
#read user input
|
||||
read -p "Please enter username:" username
|
||||
|
||||
#search for username in /etc/passwd file and print details on the screen
|
||||
cat /etc/passwd | awk "/$username/ "' { print $0 }'
|
||||
```
|
||||
|
||||
Thereafter, save the file and exit.
|
||||
|
||||
Interpretation of the Awk command in the test.sh script above:
|
||||
|
||||
```
|
||||
cat /etc/passwd | awk "/$username/ "' { print $0 }'
|
||||
```
|
||||
|
||||
`"/$username/ "` – shell quoting used to substitute value of shell variable username in Awk command. The value of username is the pattern to be searched in the file /etc/passwd.
|
||||
|
||||
Note that the double quote is outside the Awk script, `‘{ print $0 }’`.
|
||||
|
||||
Then make the script executable and run it as follows:
|
||||
|
||||
```
|
||||
$ chmod +x test.sh
|
||||
$ ./text.sh
|
||||
```
|
||||
|
||||
After running the script, you will be prompted to enter a username, type a valid username and hit Enter. You will view the user’s account details from the /etc/passwd file as below:
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/08/Shell-Script-to-Find-Username-in-Passwd-File.png)
|
||||
|
||||
### 2. Using Awk’s Variable Assignment
|
||||
|
||||
This method is much simpler and better in comparison to method one above. Considering the example above, we can run a simple command to accomplish the job. Under this method, we use the -v option to assign a shell variable to a Awk variable.
|
||||
|
||||
Firstly, create a shell variable, username and assign it the name that we want to search in the /etc/passswd file:
|
||||
|
||||
```
|
||||
username="aaronkilik"
|
||||
```
|
||||
|
||||
Then type the command below and hit Enter:
|
||||
|
||||
```
|
||||
# cat /etc/passwd | awk -v name="$username" ' $0 ~ name {print $0}'
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/08/Find-Username-in-Password-File-Using-Awk.png)
|
||||
|
||||
Explanation of the above command:
|
||||
|
||||
- `-v` – Awk option to declare a variable
|
||||
- `username` – is the shell variable
|
||||
- `name` – is the Awk variable
|
||||
|
||||
Let us take a careful look at `$0 ~ name` inside the Awk script, `' $0 ~ name {print $0}'`. Remember, when we covered Awk comparison operators in Part 4 of this series, one of the comparison operators was value ~ pattern, which means: true if value matches the pattern.
|
||||
|
||||
The `output($0)` of cat command piped to Awk matches the pattern (aaronkilik) which is the name we are searching for in /etc/passwd, as a result, the comparison operation is true. The line containing the user’s account information is then printed on the screen.
|
||||
|
||||
### Conclusion
|
||||
|
||||
We have covered an important section of Awk features, that can help us use shell variables within Awk commands. Many times, you will write small Awk programs or commands within shell scripts and therefore, you need to have a clear understanding of how to use shell variables within Awk commands.
|
||||
|
||||
In the next part of the Awk series, we shall dive into yet another critical section of Awk features, that is flow control statements. So stay tunned and let’s keep learning and sharing.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/use-shell-script-variable-in-awk/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,259 @@
|
||||
How to Use Flow Control Statements in Awk – Part 12
|
||||
===========================================
|
||||
|
||||
When you review all the Awk examples we have covered so far, right from the start of the Awk series, you will notice that all the commands in the various examples are executed sequentially, that is one after the other. But in certain situations, we may want to run some text filtering operations based on some conditions, that is where the approach of flow control statements sets in.
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/08/Use-Flow-Control-Statements-in-Awk.png)
|
||||
|
||||
There are various flow control statements in Awk programming and these include:
|
||||
|
||||
- if-else statement
|
||||
- for statement
|
||||
- while statement
|
||||
- do-while statement
|
||||
- break statement
|
||||
- continue statement
|
||||
- next statement
|
||||
- nextfile statement
|
||||
- exit statement
|
||||
|
||||
However, for the scope of this series, we shall expound on: if-else, for, while and do while statements. Remember that we already walked through how to use next statement in Part 6 of this Awk series.
|
||||
|
||||
### 1. The if-else Statement
|
||||
|
||||
The expected syntax of the if statement is similar to that of the shell if statement:
|
||||
|
||||
```
|
||||
if (condition1) {
|
||||
actions1
|
||||
}
|
||||
else {
|
||||
actions2
|
||||
}
|
||||
```
|
||||
|
||||
In the above syntax, condition1 and condition2 are Awk expressions, and actions1 and actions2 are Awk commands executed when the respective conditions are satisfied.
|
||||
|
||||
When condition1 is satisfied, meaning it’s true, then actions1 is executed and the if statement exits, otherwise actions2 is executed.
|
||||
|
||||
The if statement can also be expanded to a if-else_if-else statement as below:
|
||||
|
||||
```
|
||||
if (condition1){
|
||||
actions1
|
||||
}
|
||||
else if (conditions2){
|
||||
actions2
|
||||
}
|
||||
else{
|
||||
actions3
|
||||
}
|
||||
```
|
||||
|
||||
For the form above, if condition1 is true, then actions1 is executed and the if statement exits, otherwise condition2 is evaluated and if it is true, then actions2 is executed and the if statement exits. However, when condition2 is false then, actions3 is executed and the if statement exits.
|
||||
|
||||
Here is a case in point of using if statements, we have a list of users and their ages stored in the file, users.txt.
|
||||
|
||||
We want to print a statement indicating a user’s name and whether the user’s age is less or more than 25 years old.
|
||||
|
||||
```
|
||||
aaronkilik@tecMint ~ $ cat users.txt
|
||||
Sarah L 35 F
|
||||
Aaron Kili 40 M
|
||||
John Doo 20 M
|
||||
Kili Seth 49 M
|
||||
```
|
||||
|
||||
We can write a short shell script to carry out our job above, here is the content of the script:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
awk ' {
|
||||
if ( $3 <= 25 ){
|
||||
print "User",$1,$2,"is less than 25 years old." ;
|
||||
}
|
||||
else {
|
||||
print "User",$1,$2,"is more than 25 years old" ;
|
||||
}
|
||||
}' ~/users.txt
|
||||
```
|
||||
|
||||
Then save the file and exit, make the script executable and run it as follows:
|
||||
|
||||
```
|
||||
$ chmod +x test.sh
|
||||
$ ./test.sh
|
||||
```
|
||||
|
||||
Sample Output
|
||||
|
||||
```
|
||||
User Sarah L is more than 25 years old
|
||||
User Aaron Kili is more than 25 years old
|
||||
User John Doo is less than 25 years old.
|
||||
User Kili Seth is more than 25 years old
|
||||
```
|
||||
|
||||
### 2. The for Statement
|
||||
|
||||
In case you want to execute some Awk commands in a loop, then the for statement offers you a suitable way to do that, with the syntax below:
|
||||
|
||||
Here, the approach is simply defined by the use of a counter to control the loop execution, first you need to initialize the counter, then run it against a test condition, if it is true, execute the actions and finally increment the counter. The loop terminates when the counter does not satisfy the condition.
|
||||
|
||||
```
|
||||
for ( counter-initialization; test-condition; counter-increment ){
|
||||
actions
|
||||
}
|
||||
```
|
||||
|
||||
The following Awk command shows how the for statement works, where we want to print the numbers 0-10:
|
||||
|
||||
```
|
||||
$ awk 'BEGIN{ for(counter=0;counter<=10;counter++){ print counter} }'
|
||||
```
|
||||
|
||||
Sample Output
|
||||
|
||||
```
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
```
|
||||
|
||||
### 3. The while Statement
|
||||
|
||||
The conventional syntax of the while statement is as follows:
|
||||
|
||||
```
|
||||
while ( condition ) {
|
||||
actions
|
||||
}
|
||||
```
|
||||
|
||||
The condition is an Awk expression and actions are lines of Awk commands executed when the condition is true.
|
||||
|
||||
Below is a script to illustrate the use of while statement to print the numbers 0-10:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
awk ' BEGIN{ counter=0 ;
|
||||
|
||||
while(counter<=10){
|
||||
print counter;
|
||||
counter+=1 ;
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Save the file and make the script executable, then run it:
|
||||
|
||||
```
|
||||
$ chmod +x test.sh
|
||||
$ ./test.sh
|
||||
```
|
||||
|
||||
Sample Output
|
||||
|
||||
```
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
```
|
||||
|
||||
### 4. The do while Statement
|
||||
|
||||
It is a modification of the while statement above, with the following underlying syntax:
|
||||
|
||||
```
|
||||
do {
|
||||
actions
|
||||
}
|
||||
while (condition)
|
||||
```
|
||||
|
||||
The slight difference is that, under do while, the Awk commands are executed before the condition is evaluated. Using the very example under while statement above, we can illustrate the use of do while by altering the Awk command in the test.sh script as follows:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
awk ' BEGIN{ counter=0 ;
|
||||
do{
|
||||
print counter;
|
||||
counter+=1 ;
|
||||
}
|
||||
while (counter<=10)
|
||||
}
|
||||
'
|
||||
```
|
||||
|
||||
After modifying the script, save the file and exit. Then make the script executable and execute it as follows:
|
||||
|
||||
```
|
||||
$ chmod +x test.sh
|
||||
$ ./test.sh
|
||||
```
|
||||
|
||||
Sample Output
|
||||
|
||||
```
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
This is not a comprehensive guide regarding Awk flow control statements, as I had mentioned earlier on, there are several other flow control statements in Awk.
|
||||
|
||||
Nonetheless, this part of the Awk series should give you a clear fundamental idea of how execution of Awk commands can be controlled based on certain conditions.
|
||||
|
||||
You can as well expound more on the rest of the flow control statements to gain more understanding on the subject matter. Finally, in the next section of the Awk series, we shall move into writing Awk scripts.
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/use-flow-control-statements-with-awk-command/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user