Statement:
These codes are only used to learn and understand the SED command.
It represents the only or optimal solution to the problem.
References
Expected: <sed&awk=>
I. Replacement
1. magic transformation (Y
Command
Use)
Code:
|
Sed 'y/ori_letter_list/target_letter_list/'filename |
Code:
|
Cat filename 1234567890 2345678901 3456789012 4567890123 |
Test
Set
File 1
Change to
2 In the file
Change to B
...
0 in the file
Change to J
Code:
|
Sed 'y/1234567890/abcdefghij/'filename Abcdefghij Bcdefghija Cdefghijab Defghijabc |
Note that the conversion relationship is converted according to the positions of the two lists. Y is a local command and the suffix flag/g is not allowed.
List1: 1234567890
List2: abcdefghij
Lower
Returns a transform opposite to the transform.
Code:
|
Sed 'y/0987654321/abcdefghij/'filename Jihgfedcba Ihgfedcbaj Hgfedcbaji Gfedcbajih |
2. Replace the first match in each line
Code:
|
Sed's/regexpr/anyword/'filename Sed 'S/regexpr/anyword/1 'filename |
Example:
Reference:
|
Cat filename 1234567890 2345678901 3456789012 4567890123 Sed's/5/5/'filename 1234 5 67890 2345 678901 34 v 6789012 45 67890123 |
3. replace N in each line (if there is
) Match
Code:
|
Sed "s/regexpr/anyword/$ {n}" filename Cat filename 111111111111111111 222222222222222222 333333333333333333 444444444444444444 Example Example Sed "s/4/4/8" filename 111111111111111111 222222222222222222 333333333333333333 4444444 4 4444444444 |
4. Replace all matches in each row
Code:
|
Cat filename 1234567890 2345678901 3456789012 4567890123 |
Code:
|
Example: Sed's/3/3/G' filename 12, 3, 4567890 2 3 45678901 3, 456789012, 456789012, and 3 |
Ii. row number processing
1.
Add a row number to the file
Code:
|
Sed = filename | sed 'n'; S // n /:/' Cat filename 111111111111111111 222222222222222222 333333333333333333 444444444444444444 |
Example
Code:
|
Sed = filename | sed 'n'; S // n/:/'filename' 1111111111111111 2: 2222222222222222 33333333333333333 4:444444444444444444 |
2. Only Add rows to the text line in the file
No.
Code:
|
Sed/./= A | sed '/./N; S // n /:/' |
Example
Code:
|
Cat filename 111111111111111111222222222222222222 333333333333333333 444444444444444444 Sed/./= A | sed '/./N; S // n/:/' filename 1111111111111111 3:222222222222222222 43333333333333333 6:4444444444444444 |
Iii. String flip
Code:
|
Sed '// n /! G; S // (./)/(. */n/)/&/2/1/; // D; S /.//' |
Example
Code:
|
Echo 1234567890 | SED '// N /! G; S // (./)/(. */n/)/&/2/1/; // D; S /.//' 0987654321 |
Iv. Selective output
1. Print the odd number of lines in the document (output across rows)
Code:
|
Sed 'n'; D' Sed 'x; $! N; x' Sed-n'p; N'1 3 5 7 |
2. Print an even number of rows (output through the same line)
Code:
|
Sed-n'n'; P' Sed '1d; n; D ;' 2 4 6 8 |
3. Delete duplicate rows in large quantities
Note that the pattern space file is too large)
Code:
|
Sed '$! N;/^/(. */)/n/1 $ /! P; D' # Use $! N beware of memory overflow |
Example
Code:
|
Cat File 111111111111111111 222222222222222222 222222222222222222 333333333333333333 444444444444444444 444444444444444444 444444444444444444 444444444444444444 444444444444444444Sed '$! N; /^/(. */)/N/1 $ /! P; D' filename 111111111111111111 222222222222222222 333333333333333333 444444444444444444 |
4. Merge upper and lower rows and separate them with spaces
Isolation
Code:
|
Sed '$! N; S // N //' |
Example
Code:
|
Cat File 1234567890 0987654321 After the command is executed 1234567890 0987654321 |
5. Merge the rows ending with a slash (/) and separate them with spaces (concatenate and disconnect rows)
Code:
|
Sed-E: A-E '// $/N; S // N //; TA' |
Example
Code:
|
Cat filename 1 111111111111111111/ 2 222222222222222222 3 333333333333333333/ 4 444444444444444444Sed -E: A-E '// $/N; S // N //; TA' filename 1 111111111111111111 2 222222222222222222 3 333333333333333333 4 444444444444444444 |
6. Merge rows by keywords
If a row starts with =, It is merged into the previous row and replaced with = as a space.
Code:
|
Sed-E: A-E '$! N; S // n = //; TA '-e' p; D' |
Example
Code:
|
Cat File 111111111111111111 222222222222222222 = 333333333333333333 444444444444444444Sed-E: A-E '$! N; S // n = //; TA '-e' p; D' Filename 111111111111111111 222222222222222222 333333333333333333 444444444444444444 |
7. output the next row of matching rows
Code:
|
Sed-n'/regexpr/{n; P;} 'filename |
Example
Code:
|
Cat filename 1 111111111111111111 2 222222222222222222 3 333333333333333333 4 444444444444444444Sed -N'/^ 3/{n; P;} 'filename 4 444444444444444444 |
8. Display and output the row number matching the row
Matches upstream, downstream
Sed-n-e '/regexpr/{=; X; 1! P; G; $! N; P; D ;}'
-E h
Example
Code:
|
Cat filename 1 111111111111111111 2 222222222222222222 3 333333333333333333 4 444444444444444444Sed -N-e '/^ 3/{=; X; 1! P; G; $! N; P; D;} '-E h filename 3 # Match the row number 2 222222222222222222 # previous line 3 333333333333333333 # matching rows 4 444444444444444444 # Next line |
9. delete a flag area in the document
Keyword match line
Delete the line containing myword in the block from being to end in the document.
Code:
|
Sed '/^ begin/,/^ end/{/myword/d;}' filename |
Reference:
|
Cat filename Myword Begin Myword Number! Myword Number! Myword Number! Myword Number! End Myword Number! |
Test
Reference:
|
Myword Begin Number! Number! Number! Number! End Myword Number! |
5. String Parsing
1. parse two substrings (the first two characters and the last nine characters) from the string)
Code:
|
Echo "welovechinaunix" | sed-e 'H; S //(.. /). * // 1/; X; S /. */(. /{9/}/) $ // 1/; X; G; S // N //' We chinaunix |
2. Split the date string
Code:
|
Echo 20030922 | SED's // (.../)/(../)/1/2 /3/'| read year month day Echo $ year $ month $ day 2003 09 22 |