删除空白的第一行脚本 [英] remove blank first line script

查看:193
本文介绍了删除空白的第一行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本打印出第一行空白的文件:

  for f in`find。 -regex。* \.php`;为t做
; do
$ t -1 $ f | egrep'^ [] * $'> / dev / null&&在$ f的$ t回显空白行;
完成;
完成

我该如何改进以实际删除空行,或者至少复制所有文件的空白第一行到其他地方。

我试着用这个拷贝,这很好,因为它复制保存目录结构,但它是复制每一个php文件,我需要捕获egrep的正面输出,并只复制这些文件。

  rsync -R $ f。 ./DavidSiteBlankFirst/ 


解决方案

p>

  find ./ -type f -regex'。* \.php'-exec sed -i -e'1 {/ ^ [[:blank:]] * $ / d;}''{}'\; 

这会找到以.php结尾的所有常规文件,并执行在第一行工作的sed命令只检查它的空白并删除它,如果是,文件中的其他空白行不受影响。


I have this script which is printing out the files that have the first line blank:

for f in `find . -regex ".*\.php"`; do 
  for t in head; do 
    $t -1 $f  |egrep '^[  ]*$' >/dev/null && echo "blank line at the $t of $f"; 
  done; 
done

How can I improve this to actually remove the blank line too, or at least copy all the files with the blank first line somewhere else.

I tried copying using this, which is good, because it copies preserving the directory structure, but it was copying every php file, and I needed to capture the postive output of the egrep and only copy those files.

rsync -R $f ../DavidSiteBlankFirst/

解决方案

I would use sed personally

find ./ -type f -regex '.*\.php' -exec sed -i -e '1{/^[[:blank:]]*$/d;}' '{}' \;

this finds all the regular files ending in .php and executes the sed command which works on the first line only and checks to see if its blank and deletes it if it is, other blank lines in the file remain unaffected.

这篇关于删除空白的第一行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆