Ruby txt解析器 - 在条件下输出以下行

时间:2018-01-15 09:14:55

标签: ruby parsing

我有一个像这样结构的文本文件:

"/api/Customers"

对于包含请求网址{"json":"someValue"}的每封邮件,我想输出以下行File.open("my_file.log", "r").each_line do |line| if line.include? "POST https://some.url.com:8080/api/Customers/\r" p line end end 。如果没有,它应该跳过整个块。

这是我到目前为止所拥有的。它正确地放置了这条线,但我想得到以下一行:

<RelativeLayout
    android:id="@+id/relative_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/relative_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image_product_imageview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/status_product_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_marginTop="20dp"
            android:gravity="center_vertical"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:visibility="invisible" />
    </RelativeLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/relative_three"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_below="@+id/relative_two">

        <Spinner
            android:id="@+id/select_spinner"
            android:layout_width="15dp"
            android:layout_height="0dp"
            android:layout_marginRight="5dp"
            android:dropDownVerticalOffset="40dp"
            android:gravity="center_vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHeight_default="wrap"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/name_product_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:ellipsize="marquee"
            android:gravity="right"
            android:maxLines="1"
            android:text="TEXT"
            app:layout_constraintRight_toLeftOf="@+id/select_spinner"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/count_products_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:gravity="right"
            android:text="TEXT"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/select_spinner"
            app:layout_constraintTop_toBottomOf="@+id/name_product_textview" />
    </android.support.constraint.ConstraintLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

这是一个非常简单的解决方案:

def test
  show_next_line = false
  File.open("/home/gergra/code/test/my_file.log", "r").each_line do |line|
    if show_next_line
      show_next_line = false
      puts line
    end
    if line.include? "POST https://some.url.com:8080/api/Customers/"
      show_next_line = true
    end
  end
end