francois:an_introduction_to_sed
Différences
Ci-dessous, les différences entre deux révisions de la page.
| francois:an_introduction_to_sed [2013/05/14 08:30] – créée francois | francois:an_introduction_to_sed [2013/05/15 10:24] (Version actuelle) – francois | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Introduction to Sed ====== | ====== Introduction to Sed ====== | ||
| - | |||
| How to use sed, a special editor for modifying files automatically. If you want to write a program to make changes in a file, sed is the tool to use. | How to use sed, a special editor for modifying files automatically. If you want to write a program to make changes in a file, sed is the tool to use. | ||
| Ligne 14: | Ligne 13: | ||
| Anyhow, sed is a marvelous utility. Unfortunately, | Anyhow, sed is a marvelous utility. Unfortunately, | ||
| - | Do not fret! It is not your fault you don't understand sed. I will cover sed completely. But I will describe the features in the order that I learned them. I didn't learn everything at once. You don't need to either. | + | **Do not fret!** It is not your fault you don't understand sed. I will cover sed completely. But I will describe the features in the order that I learned them. I didn't learn everything at once. You don't need to either. |
| - | The essential command: s for substitution | + | |
| + | ===== The essential command: s for substitution | ||
| Sed has several commands, but most people only learn the substitute command: s. The substitute command changes all occurrences of the regular expression into a new value. A simple example is changing " | Sed has several commands, but most people only learn the substitute command: s. The substitute command changes all occurrences of the regular expression into a new value. A simple example is changing " | ||
| - | sed s/ | + | |
| Or another way (for Unix beginners), | Or another way (for Unix beginners), | ||
| - | sed s/ | + | |
| and for those who want to test this: | and for those who want to test this: | ||
| - | echo day | sed s/ | + | |
| This will output " | This will output " | ||
| Ligne 33: | Ligne 33: | ||
| I didn't put quotes around the argument because this example didn't need them. If you read my earlier tutorial, you would understand why it doesn' | I didn't put quotes around the argument because this example didn't need them. If you read my earlier tutorial, you would understand why it doesn' | ||
| - | sed ' | + | |
| I must emphasize the the sed editor changes exactly what you tell it to. So if you executed | I must emphasize the the sed editor changes exactly what you tell it to. So if you executed | ||
| - | echo Sunday | sed ' | + | |
| This would output the word " | This would output the word " | ||
| Ligne 43: | Ligne 43: | ||
| There are four parts to this substitute command: | There are four parts to this substitute command: | ||
| - | s Substitute command | + | * s Substitute command |
| - | + | | |
| - | /../../ Delimiter | + | |
| - | + | | |
| - | day Regular Expression Pattern Search Pattern | + | |
| - | + | ||
| - | night Replacement string | + | |
| The search pattern is on the left hand side and the replacement string is on the right hand side. | The search pattern is on the left hand side and the replacement string is on the right hand side. | ||
| We've covered | We've covered | ||
| - | The slash as a delimiter | + | |
| + | ===== The slash as a delimiter | ||
| The character after the s is the delimiter. It is conventionally a slash, because this is what ed, more, and vi use. It can be anything you want, however. If you want to change a pathname that contains a slash - say / | The character after the s is the delimiter. It is conventionally a slash, because this is what ed, more, and vi use. It can be anything you want, however. If you want to change a pathname that contains a slash - say / | ||
| - | sed ' | + | |
| Gulp. Some call this a ' | Gulp. Some call this a ' | ||
| - | sed ' | + | |
| Some people use colons: | Some people use colons: | ||
| - | sed ' | + | |
| Others use the " | Others use the " | ||
| - | sed ' | + | |
| Pick one you like. As long as it's not in the string you are looking for, anything goes. And remember that you need three delimiters. If you get a " | Pick one you like. As long as it's not in the string you are looking for, anything goes. And remember that you need three delimiters. If you get a " | ||
| - | Using & as the matched string | + | |
| + | ===== Using & as the matched string | ||
| Sometimes you want to search for a pattern and add some characters, like parenthesis, | Sometimes you want to search for a pattern and add some characters, like parenthesis, | ||
| - | sed ' | + | |
| This won't work if you don't know exactly what you will find. How can you put the string you found in the replacement string if you don't know what it is? | This won't work if you don't know exactly what you will find. How can you put the string you found in the replacement string if you don't know what it is? | ||
| Ligne 83: | Ligne 82: | ||
| The solution requires the special character "& | The solution requires the special character "& | ||
| - | sed ' | + | |
| You can have any number of "&" | You can have any number of "&" | ||
| - | % echo "123 abc" | sed ' | + | |
| - | + | 123 123 abc | |
| - | 123 123 abc | + | |
| Let me slightly amend this example. Sed will match the first string, and make it as greedy as possible. The first match for ' | Let me slightly amend this example. Sed will match the first string, and make it as greedy as possible. The first match for ' | ||
| - | % echo "123 abc" | sed ' | + | |
| + | 123 123 abc | ||
| - | 123 123 abc | + | The string "abc" is unchanged, because it was not matched by the regular expression. If you wanted to eliminate " |
| - | The string " | + | ===== Using \1 to keep part of the pattern |
| - | Using \1 to keep part of the pattern | + | |
| I have already described the use of " | I have already described the use of " | ||
| Ligne 104: | Ligne 102: | ||
| If you wanted to keep the first word of a line, and delete the rest of the line, mark the important part with the parenthesis: | If you wanted to keep the first word of a line, and delete the rest of the line, mark the important part with the parenthesis: | ||
| - | sed ' | + | |
| I should elaborate on this. Regular exprssions are greedy, and try to match as much as possible. " | I should elaborate on this. Regular exprssions are greedy, and try to match as much as possible. " | ||
| - | echo abcd123 | sed ' | + | |
| This will output " | This will output " | ||
| Ligne 114: | Ligne 112: | ||
| If you want to switch two words around, you can remember two patterns and change the order around: | If you want to switch two words around, you can remember two patterns and change the order around: | ||
| - | sed ' | + | |
| Note the space between the two remembered patterns. This is used to make sure two words are found. | Note the space between the two remembered patterns. This is used to make sure two words are found. | ||
| Ligne 120: | Ligne 118: | ||
| The " | The " | ||
| - | sed ' | + | |
| You can have up to nine values: " | You can have up to nine values: " | ||
| - | Substitute Flags | + | |
| + | ===== Substitute Flags ===== | ||
| You can add additional flags after the last delimiter. These flags can specify what happens when there is more than one occurrence of a pattern on a single line, and what to do if a substitution is found. Let me describe them. | You can add additional flags after the last delimiter. These flags can specify what happens when there is more than one occurrence of a pattern on a single line, and what to do if a substitution is found. Let me describe them. | ||
| - | /g - Global replacement | + | |
| + | ===== /g - Global replacement | ||
| Most Unix utilties work on files, reading a line at a time. Sed, by default, is the same way. If you tell it to change a word, it will only change the first occurrence of the word on a line. You may want to make the change on every word on the line instead of the first. For an example, let's place parentheses around words on a line. Instead of using a pattern like " | Most Unix utilties work on files, reading a line at a time. Sed, by default, is the same way. If you tell it to change a word, it will only change the first occurrence of the word on a line. You may want to make the change on every word on the line instead of the first. For an example, let's place parentheses around words on a line. Instead of using a pattern like " | ||
| - | sed 's/[^ ]*/ | + | |
| If you want it to make changes for every word, add a " | If you want it to make changes for every word, add a " | ||
| - | sed 's/[^ ][^ ]*/ | + | |
| - | Is sed recursive? | + | |
| + | ===== Is sed recursive? | ||
| Sed only operates on patterns found in the in-coming data. That is, the input line is read, and when a pattern is matched, the modified output is generated, and the rest of the input line is scanned. The " | Sed only operates on patterns found in the in-coming data. That is, the input line is read, and when a pattern is matched, the modified output is generated, and the rest of the input line is scanned. The " | ||
| Ligne 142: | Ligne 143: | ||
| This will not cause an infinite loop. If a second " | This will not cause an infinite loop. If a second " | ||
| - | /1, /2, etc. Specifying which occurrence | + | |
| + | ===== /1, /2, etc. Specifying which occurrence | ||
| With no flags, the first pattern is changed. With the " | With no flags, the first pattern is changed. With the " | ||
| - | sed ' | + | |
| Yuck. There is an easier way to do this. You can add a number after the substitution command to indicate you only want to match that particular pattern. Example: | Yuck. There is an easier way to do this. You can add a number after the substitution command to indicate you only want to match that particular pattern. Example: | ||
| - | sed ' | + | |
| You can combine a number with the g (global) flag. For instance, if you want to leave the first world alone alone, but change the second, third, etc. to DELETED, use /2g: | You can combine a number with the g (global) flag. For instance, if you want to leave the first world alone alone, but change the second, third, etc. to DELETED, use /2g: | ||
| - | sed ' | + | |
| Don't get /2 and \2 confused. The /2 is used at the end. \2 is used in inside the replacement field. | Don't get /2 and \2 confused. The /2 is used at the end. \2 is used in inside the replacement field. | ||
| Ligne 160: | Ligne 162: | ||
| Note the space after the " | Note the space after the " | ||
| - | sed 's/[^ ]*//2' <old >new | + | |
| but this also eats CPU. If this works on your computer, and it does on some Unix systems, you could remove the encrypted password from the password file: | but this also eats CPU. If this works on your computer, and it does on some Unix systems, you could remove the encrypted password from the password file: | ||
| - | sed ' | + | |
| But this didn't work for me the time I wrote thise. Using " | But this didn't work for me the time I wrote thise. Using " | ||
| - | sed ' | + | |
| You could also add a character to the first pattern so that it no longer matches the null pattern: | You could also add a character to the first pattern so that it no longer matches the null pattern: | ||
| - | sed ' | + | |
| The number flag is not restricted to a single digit. It can be any number from 1 to 512. If you wanted to add a colon after the 80th character in each line, you could type: | The number flag is not restricted to a single digit. It can be any number from 1 to 512. If you wanted to add a colon after the 80th character in each line, you could type: | ||
| - | sed ' | + | |
| You can also do it the hard way by using 80 dots: | You can also do it the hard way by using 80 dots: | ||
| - | sed ' | + | |
| - | /p - print | + | |
| + | ===== /p - print ===== | ||
| By default, sed prints every line. If it makes a substitution, | By default, sed prints every line. If it makes a substitution, | ||
| - | sed -n ' | + | |
| - | Write to a file with /w filename | + | |
| + | ===== Write to a file with /w filename | ||
| There is one more flag that can follow the third delimiter. With it, you can specify a file that will receive the modified data. An example is the following, which will write all lines that start with an even number to the file even: | There is one more flag that can follow the third delimiter. With it, you can specify a file that will receive the modified data. An example is the following, which will write all lines that start with an even number to the file even: | ||
| - | sed -n ' | + | |
| In this example, the output file isn't needed, as the input was not modified. You must have exactly one space between the w and the filename. You can also have ten files open with one instance of sed. This allows you to split up a stream of data into separate files. Using the previous example combined with multiple substitution commands described later, you could split a file into ten pieces depending on the last digit of the first number. You could also use this method to log error or debugging information to a special file. | In this example, the output file isn't needed, as the input was not modified. You must have exactly one space between the w and the filename. You can also have ten files open with one instance of sed. This allows you to split up a stream of data into separate files. Using the previous example combined with multiple substitution commands described later, you could split a file into ten pieces depending on the last digit of the first number. You could also use this method to log error or debugging information to a special file. | ||
| - | Combining substitution flags | + | |
| + | ===== Combining substitution flags ===== | ||
| You can combine flags when it makes sense. Also " | You can combine flags when it makes sense. Also " | ||
| - | sed -n ' | + | |
| Next I will discuss the options to sed, and different ways to invoke sed. | Next I will discuss the options to sed, and different ways to invoke sed. | ||
| - | Arguments and invocation of sed | + | |
| + | ===== Arguments and invocation of sed ===== | ||
| previously, I have only used one substitute command. If you need to make two changes, and you didn't want to read the manual, you could pipe together multiple sed commands: | previously, I have only used one substitute command. If you need to make two changes, and you didn't want to read the manual, you could pipe together multiple sed commands: | ||
| - | sed ' | + | |
| This used two processes instead of one. A sed guru never uses two processes when one can do. | This used two processes instead of one. A sed guru never uses two processes when one can do. | ||
| - | Multiple commands with -e command | + | |
| + | ===== Multiple commands with -e command | ||
| One method of combining multiple commands is to use a -e before each command: | One method of combining multiple commands is to use a -e before each command: | ||
| - | sed -e ' | + | |
| A " | A " | ||
| Also see Quoting multiple sed lines in the Bourne shell | Also see Quoting multiple sed lines in the Bourne shell | ||
| - | Filenames on the command line | + | |
| + | ===== Filenames on the command line ===== | ||
| You can specify files on the command line if you wish. If there is more than one argument to sed that does not start with an option, it must be a filename. This next example will count the number of lines in three files that don't begin with a "#:" | You can specify files on the command line if you wish. If there is more than one argument to sed that does not start with an option, it must be a filename. This next example will count the number of lines in three files that don't begin with a "#:" | ||
francois/an_introduction_to_sed.1368520254.txt.gz · Dernière modification : (modification externe)
