Examples For Sed Linux Command In Text Manipulation and File Handling
|
1. Replace all occurrence
|
1. Replace all occurrence Ram in Inputfile with Shyam and save the result in Outfile
sed 's/Ram/Shyam/' Inputfile >Outfile
2. Replace all occurrence of multiple string
|
2. Replace all occurrence Ram and Sita in Inputfile with Shyam and Geeta respectively, and save the result in Outfile
sed -e 's/Ram/Shyam/; s/Sita/Geeta/' Inputfile >Outfile
3. Reading sed commands from a file
|
3. Reading Commands From a File: I have multiple strings saved in Inputfile and want to replace them with multiple strings saved in a file string and save the in Outfile
sed -f string Inputfile >Outfile
4. Substituting Flags: to control the replacement in file
|
4. Substituting Flags: There are 4 kinds of substitutions:
- g, replace all occurrences.
- A number, the occurrence number for the new text that you want to substitute.
- p, print the original content.
- w file: means write the results to a file.
Extract Part of a FASTA Sequences with Position by python script HERE
5. Limiting sed in a file
|
5. Limiting sed in a file: By default, Sed command processes your entire file. However, you can limit the sed command to process your file in different ways:
A pattern that matches a specific range.
- following command will replace string in second and third line only
- A range of lines. - following command will replace string in second line only
sed '2s/Ram/Shyam/' Inputfile >Outfile
sed '2,3s/Ram/Shyam/' Inputfile >Outfile- Following command will replace string in second line to the end of the file only
sed '2,$s/Ram/Shyam/' Inputfile >Outfile
6. Delete Lines in a file with sed
|
6. Delete Lines in a file with sed : The delete (d) flag deletes the text from the stream, not the original file.
- following command will delete second line
- following command will delete second line
sed '2d' Inputfile >Outfile- following command will delete second and third line
sed '2,3d' Inputfile >Outfile- following command will delete second line to the end of the file.
sed '2,$d' Inputfile >Outfile
Related Posts HOW TO
|
Was This Post Useful? Add This To Del.icio.us Share on Facebook StumbleUpon This Add to Technorati Share on Twitter |
Labels:
HOW TO
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Have Problem ?? Drop a comments here!