Source From Here
Question
I want to change the formatting of a file. I just wanted to remove all blank lines from text file. How do I achieve this task w/o spending much time?
How-To
Yes, you do not have to waste your time making manual changes to files. Both Linux and UNIX systems come with file manipulation tools that can be used to remove all blank lines very quickly.
Task: Remove blank lines using sed
Type the following command:
Task: Remove blank lines using grep
Both grep and sed use special pattern ^$ that matchs the blank lines. Let us say directory /home/me/data/*.txt has all text file. Use following for loop (shell script) to remove all blank lines from all files stored in /home/me/datadirectory:
Question
I want to change the formatting of a file. I just wanted to remove all blank lines from text file. How do I achieve this task w/o spending much time?
How-To
Yes, you do not have to waste your time making manual changes to files. Both Linux and UNIX systems come with file manipulation tools that can be used to remove all blank lines very quickly.
Task: Remove blank lines using sed
Type the following command:
Task: Remove blank lines using grep
Both grep and sed use special pattern ^$ that matchs the blank lines. Let us say directory /home/me/data/*.txt has all text file. Use following for loop (shell script) to remove all blank lines from all files stored in /home/me/datadirectory:
- #!/bin/sh
- files="/home/me/data/*.txt"
- for i in $files
- do
- sed '/^$/d' $i > $i.out
- mv $i.out $i
- done
沒有留言:
張貼留言