English 中文(简体)
在空行重复数据, 直到l 发生非空行
原标题:Repeat data in the empty lines untill a non-empty line occurs

我有一个数据文件 看起来像这个:

 xyz123            2.000    -0.3974     0.0  hij123       
                                          6.0  lmn123      
                                          8.7  efg123      
                                         13.9  uvw123      
                                         28.5  rst123       
 abc123            10.000     0.1943     0.0  wxy123       
                                         10.7  xyz123       
                                         19.9  pqr123     
                                         20.6  stu123      
                                         20.6  klm123      
 def123            50.000    -0.2595    19.2  jkl123      
                                         26.1  stu123      
                                         27.1  def123     
                                         27.1  ghi123     
                                         27.6  abc123

===================================================================================

 lmn123            40.000    -0.3695     19.2  jkl123      
                                         26.1  stu123      
                                         27.1  def123     
                                         27.1  ghi123     
                                         27.6  abc123

我需要把它变成:

xyz123,2.000,-0.3974,0.0,hij123       
xyz123,2.000,-0.3974,6.0,lmn123      
xyz123,2.000,-0.3974,8.7,efg123      
xyz123,2.000,-0.3974,13.9,uvw123      
xyz123,2.000,-0.3974,28.5,rst123       
abc123,10.000,0.1943,0.0,wxy123       
abc123,10.000,0.1943,10.7,xyz123       
abc123,10.000,0.1943,19.9,pqr123     
abc123,10.000,0.1943,20.6,stu123      
abc123,10.000,0.1943,20.6,klm123      
def123,50.000,-0.2595,19.2,jkl123      
def123,50.000,-0.2595,26.1,stu123      
def123,50.000,-0.2595,27.1,def123     
def123,50.000,-0.2595,27.1,ghi123     
def123,50.000,-0.2595,27.6,abc123

============================================================================================================================================== ==========================================================================================================================================================

lmn123,40.000,-0.3695,19.2,jkl123      
lmn123,40.000,-0.3695,26.1,stu123      
lmn123,40.000,-0.3695,27.1,def123     
lmn123,40.000,-0.3695,27.1,ghi123     
lmn123,40.000,-0.3695,27.6,abc123

我怎么能用Python或者AWK或者Sed来做这个?

更新: 因此, 如果您注意到输入数据中有一条线, 看起来像是 " uvw123 15.000-0.3635 ", 当我使用 ix 中的 python 代码时, 这条线就会混乱。 是否有方法可以修改您的代码并正确输出行, 如我显示的行?

最佳回答

你可以尝试这样的东西 开始 -

awk  NF>3{a=$1;b=$2;c=$3;$1=$1;print;next}NF<3{d=$1;e=$2;print a,b,c,d,e;next}{$1=$1;}1  OFS= ,  file
问题回答

以下是Python的解决方案:

import re

with open( data.txt ) as f:
  prev = []
  for line in f:
    tok = [t for t in re.split(r s+ , line.rstrip()) if t]
    if len(tok) < len(prev):
      tok = prev[:-len(tok)] + tok
    print  , .join(tok)
    prev = tok

它跟踪每一列(在 prev 中)的最新值,并用它来在当前行中填充缺失的列。

awk  BEGIN {OFS = ","} NF == 5 {a = $1; b = $2; c = $3; $1 = $1; print; next} {$4 = $1; $5 = $2; $1 = a; $2 = b; $3 = c; print}  inputfile

折断到多行 :

awk  BEGIN {
        OFS = ","
    } 
    NF == 5 {
        a = $1; 
        b = $2; 
        c = $3; 
        $1 = $1; 
        print; 
        next
    } 
    {
        $4 = $1; 
        $5 = $2; 
        $1 = a; 
        $2 = b; 
        $3 = c; 
        print
    }  inputfile

Doing 1= 1美元 强制将线与新的 OFS 重新组合。

awk (和 tr ) 溶液,不特别优雅:

awk  BEGIN { OFS = ","}
  { if (NF == 5) {
    split($0, a); print $1, $2, $3, $4, $5
  } else {
    print a[1], a[2], a[3], $1, $2
  } }  | tr -d   	 

假设文件的标签有限 。

您可以对每行进行循环,并将 split (”) 应用到每行,例如 。

for line in lines:
    result = line.split("	")

如果 Len( 结果) 是 5, 那么您会单击一个新的区域。 您可以将数值解开

h1, h2, h3, v1, v2 = result

否则,

v1, v2 = result

您可以使用 " 打印变量。"join ([h1, h2, h3, v1, v1, v2])

至于第二个问题,在文件中看不到隐形字符, 很难辨别。 例如, 您可以在 vi 中使用“ 设置列表 ” 来查看它们 。

以 awk :

awk  BEGIN {OFS=","} /^[^ ]/ {f1=$1; f2=$2; f3=$3; f4=$4; f5=$5} /^[ ]/ {f4=$1; f5=$2}  {print f1,f2,f3,f4,f5}  < input.txt

使用 awk :

awk  BEGIN{OFS="	";} NF==2{print a,b,c,$1,$2}{};NF==5{a=$1; b=$2; c=$3;print $1,$2,$3,$4,$5}{}  logfile 

此选项首先将输出字段分隔符设置为标签( 您可以根据需要修改此选项), 然后查看行中有多少列。 如果有 5, 它会将前三栏设置为变量a、 b和 c, 然后打印出来 。

如果只有两列,它打印a、b和c(即最后一行的前三列),然后是该行的两列。

<强> 更新:

我注意到这条线只有三列! 下面的Awk命令应该按您指定的方式给出输出 :

awk  BEGIN{OFS="	";} $1~/^[a-z]/{a=$1; b=$2; c=$3;print $1,$2,$3,$4,$5}{}$1!~/^[a-z]/{print a,b,c,$1,$2}{}  logfile

此功能与以前类似, 但看第一个字段是否以字母开头, 而不是以列数开始。 如果需要, 此正则可更具体化 。

简单 grep 可以做到这一点

$ cat so.txt 
xyz123 2.000 -0.3974 0.0 hij123
6.0 lmn123
8.7 efg123
13.9 uvw123
28.5 rst123
abc123 10.000 0.1943 0.0 wxy123
10.7 xyz123
19.9 pqr123
20.6 stu123
20.6 klm123
def123 50.000 -0.2595 19.2 jkl123
26.1 stu123
27.1 def123
27.1 ghi123
27.6 abc123
$ cat so.txt | grep "-"
xyz123 2.000 -0.3974 0.0 hij123
def123 50.000 -0.2595 19.2 jkl123




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签