2016年12月1日 星期四

[Linux 常見問題] Linux command line utilities for removing blank lines from text files

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:
# cat input.txt
Test line 1

Test line 2
Test line 3

Test line 4

# sed '/^$/d' input.txt // You can redirect output to another file
Test line 1
Test line 2
Test line 3
Test line 4


// -i: edit files in place
# sed -i '/^$/d' input.txt
# cat input.txt
Test line 1
Test line 2
Test line 3
Test line 4

Task: Remove blank lines using grep
// -v, --invert-match: Invert the sense of matching, to select non-matching lines.
# grep -v '^$' input.txt > output.txt
Test line 1
Test line 2
Test line 3
Test line 4


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:
  1. #!/bin/sh  
  2. files="/home/me/data/*.txt"  
  3. for i in $files  
  4. do  
  5.   sed '/^$/d' $i > $i.out   
  6.   mv  $i.out $i  
  7. done  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...