2013-12-14 16 views
5

は私がセッドawkのかを使用して、ファイル内の長い行を見つける方法を知っています文字。

しかし、長い行を見つけて継続文字(私の場合は '&')と改行を挿入することによってそれらを分割するにはどうすればよいですか?

背景:

私が自動的に生成されたいくつかのFortranコードを持っています。残念ながら、一部の行は132文字の制限を超えています。私はそれらを見つけて、自動的に壊したいと思う。例えば、この:

this is a might long line and should be broken & 
up by inserting the continuation charater '&' a& 
nd newline. 

答えて

6

一つの方法:

$ sed -r 's/.{47}/&\&\n/g' file 
this is a might long line and should be broken & 
up by inserting the continuation charater '&' a& 
nd newline. 
5

あなたが試すことができます:sed

awk ' 
BEGIN { p=47 } 
{ 
    while(length()> p) { 
     print substr($0,1,p) "&" 
     $0=substr($0,p+1) 
    } 
    print 
}' file 
3

この解決策は何sedを必要としません

this is a might long line and should be broken up by inserting the continuation charater '&' and newline. 

このなるべきまたはawk。これは楽しいです。

tr '\n' '\r' < file | fold -w 47 | tr '\n\r' '&\n' | fold -w 48 

そして、ここでは、あなたが得るものです:

、sudo_Oのコードとして
this is a might long line and should be broken & 
up by inserting the continuation charater '&' a& 
nd newline. 
But this line should stay intact 
Of course, this is not a right way to do it and& 
you should stick with awk or sed solution 
But look! This is so tricky and fun! 
1

似ていますが、awkの中でそれを行う

awk '{gsub(/.{47}/,"&\\&\n")}1' file 
関連する問題