根据文件中的定界符捕获值

时间:2018-10-30 08:09:50

标签: awk sed delimited-text

我有一个包含以下数据的文件。

File.txt:

[{name:dany,class:4,Subjects:5,maths:yes,science:yes,EVS:no},{name:Ane,class:5,Subjects:6,maths:yes,science:yes,EVS:Yes},{name:mike,class:5,Subjects:1,maths:yes,science:no,EVS:no}]

我希望输出文件为:

Output.txt:

Dany,5,Yes
Ane,6,Yes
Mike,1,No

我的工作如下: B=`cat file.txt | awk -F '},' '{print $1}'`

echo $B | awk -F , '{print $1 " " $3" " $6'}

将输出显示为 name:dany subjects:5 evs:no

我已经编码为第一行,我希望整个文件都这样,并且需要输出我在问题中提到的

注意:忽略单词的大小写。 我已经更新了输入。请使用,作为字段的分隔符,}作为记录的分隔符

4 个答案:

答案 0 :(得分:0)

这可能对您有用(GNU sed):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:theme="@style/NoBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
tools:context="com.sdc_nollvision_relative.myapp.MainActivity">

<ImageView
    android:id="@+id/logo"
    android:layout_width="80dp"
    android:layout_height="70dp"
    android:scaleType="centerCrop"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo" />

<TextView
    android:id="@+id/main_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="3dp"
    android:text="DEMENSVÅRD"
    android:textAlignment="center"
    android:textAppearance="@style/TextAppearance.AppCompat.Title"
    android:textColor="@android:color/black"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/logo" />

<TextView
    android:id="@+id/subHeader"
    android:typeface="monospace"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="UTAN TVÅNG"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/main_header" />

<TextView
    android:id="@+id/chapter1Header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:elevation="3dp"
    android:text="Placeholder"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:textStyle="bold"
    android:typeface="serif"
    app:layout_constraintBottom_toBottomOf="@+id/chapter1"
    app:layout_constraintEnd_toEndOf="@+id/chapter1"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/iconHolder1"
    app:layout_constraintTop_toTopOf="@+id/chapter1" />

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iconHolder1"
    android:layout_width="80dp"
    android:layout_height="0dp"
    android:elevation="3dp"
    android:padding="8dp"
    android:src="@drawable/icon1"
    app:civ_border_color="@color/colorPrimary"
    app:civ_border_width="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/chapter1"
    app:layout_constraintStart_toStartOf="@+id/chapter1"
    app:layout_constraintTop_toTopOf="@+id/chapter1" />

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iconHolder2"
    android:layout_width="80dp"
    android:layout_height="0dp"
    android:elevation="3dp"
    android:padding="8dp"
    android:src="@drawable/icon2"
    app:civ_border_color="@color/chapter1"
    app:civ_border_width="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/chapter2"
    app:layout_constraintStart_toStartOf="@+id/chapter2"
    app:layout_constraintTop_toTopOf="@+id/chapter2" />

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iconHolder3"
    android:layout_width="80dp"
    android:layout_height="0dp"
    android:elevation="3dp"
    android:padding="8dp"
    android:src="@drawable/icon3"
    app:layout_constraintBottom_toBottomOf="@+id/chapter3"
    app:layout_constraintStart_toStartOf="@+id/chapter3"
    app:layout_constraintTop_toTopOf="@+id/chapter3" />

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iconHolder4"
    android:layout_width="80dp"
    android:layout_height="0dp"
    android:elevation="3dp"
    android:padding="8dp"
    android:src="@drawable/icon4"
    app:layout_constraintBottom_toBottomOf="@+id/chapter4"
    app:layout_constraintStart_toStartOf="@+id/chapter4"
    app:layout_constraintTop_toTopOf="@+id/chapter4" />


<Button
    android:id="@+id/chapter1"
    android:layout_width="0dp"
    android:layout_height="75dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/backgroundview"
    android:elevation="0dp"
    android:text=""
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/subHeader" />

<TextView
    android:id="@+id/chapter2Header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:elevation="3dp"
    android:text="Placeholder"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/chapter2"
    app:layout_constraintEnd_toEndOf="@+id/chapter2"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/iconHolder2"
    app:layout_constraintTop_toTopOf="@+id/chapter2" />


<Button
    android:id="@+id/chapter2"
    android:layout_width="0dp"
    android:layout_height="75dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/backgroundview"
    android:elevation="0dp"
    android:text=""
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/chapter1" />

<TextView
    android:id="@+id/chapter3Header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:elevation="3dp"
    android:text="Placeholder"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/chapter3"
    app:layout_constraintEnd_toEndOf="@+id/chapter3"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/iconHolder3"
    app:layout_constraintTop_toTopOf="@+id/chapter3" />


<Button
    android:id="@+id/chapter3"
    android:layout_width="0dp"
    android:layout_height="75dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/backgroundview"
    android:elevation="0dp"
    android:text=""
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/chapter2" />

<TextView
    android:id="@+id/chapter4Header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:elevation="3dp"
    android:text="Placeholder"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/chapter4"
    app:layout_constraintEnd_toEndOf="@+id/chapter4"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/iconHolder4"
    app:layout_constraintTop_toTopOf="@+id/chapter4" />


<Button
    android:id="@+id/chapter4"
    android:layout_width="0dp"
    android:layout_height="75dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/backgroundview"
    android:elevation="0dp"
    android:text=""
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/chapter3" />

<ImageButton
    android:id="@+id/about_button"
    android:layout_width="15dp"
    android:layout_height="35dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="24dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="#0000"
    android:scaleType="fitXY"
    android:src="@drawable/about"
    app:layout_constraintEnd_toStartOf="@+id/language_button"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toEndOf="@+id/version_button"
    app:layout_constraintTop_toBottomOf="@+id/chapter4" />

<Button
    android:id="@+id/version_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginTop="4dp"
    android:background="@color/colorWhite"

    android:text="version"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/chapter4" />

<Button
    android:id="@+id/language_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginTop="4dp"
    android:background="@color/colorWhite"
    android:text="language"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/chapter4" />

将行拆分为记录,然后对字段名称进行模式匹配并打印必填字段。

答案 1 :(得分:0)

您可以设置RS,FS,ORS和OFS来确认输入和输出数据。假设您的输入数据是一致的,则可以使用类似这样的方法。

awk 'BEGIN{RS="},{";FS="[:,]";ORS="\n";OFS=",";} 1 {print $2,$4,$10}' data.txt

但是输入中的字段分隔符变化很大。有时会有逗号,有时没有,有时在逗号后有空格,有时在逗号前有空格。因此,看来您的数据源已损坏。

答案 2 :(得分:0)

$ awk -v RS='}' -F'[]{: ,]+' -v OFS=',' '$3!=""{print $3, $5, $11}' file
dany,4,yes
Ane,5,yes
mike,5,no

答案 3 :(得分:0)

从服务返回的内容类似于JSON。

您可以通过几个sed步骤使其变为JSON。完成后,您可以使用jq之类的命令行JSON解析器对其进行解析。

标签(冒号前的文字)需要加引号:

sed -E 's/(\s*[a-zA-Z]+)\s*:/"\1":/g'

非数字值(冒号后的文本)也需要加引号:

sed -E 's/:\s*([a-zA-Z]+)\s*/:"\1"/g'

将其与输入数据一起放置,您将获得以下管道:

echo '[{name:dany,class:4,Subjects:5,maths:yes,science:yes,EVS:no},{name:Ane,class:5,Subjects:6,maths:yes,science:yes,EVS:Yes},{name:mike,class:5,Subjects:1,maths:yes,science:no,EVS:no}]' |
    sed -E 's/(\s*[a-zA-Z]+)\s*:/"\1":/g' |
    sed -E 's/:\s*([a-zA-Z]+)\s*/:"\1"/g'

完成此操作后,您将获得像jq这样的工具可以解析的JSON:

[{"name":"dany","class":4,"Subjects":5,"maths":"yes","science":"yes","EVS":"no"},{"name":"Ane","class":5,"Subjects":6,"maths":"yes","science":"yes","EVS":"Yes"},{"name":"mike","class":5,"Subjects":1,"maths":"yes","science":"no","EVS":"no"}]

jq可以使用以下命令生成以逗号分隔的报告:

jq '.[] | "\(.name),\(.Subjects),\(.evs)"' --raw-output
  • .[]遍历数组,一个接一个地对象([...]是一个JSON数组,{...}是一个JSON对象)
  • "\(.name)"从当前对象中提取字段“名称”并输出
  • |组合了jq命令的多个阶段(例如:.[] | .name输出每个对象的名称字段)
  • | "\(.name),\(.Subjects),\(.evs)输出一个字符串,其名称,主题和evs字段用逗号分隔。

这将产生以下输出:

dany,5,null
Ane,6,null
mike,1,null

将其组合在一起的整个管道:

echo '[{name:dany,class:4,Subjects:5,maths:yes,science:yes,EVS:no},{name:Ane,class:5,Subjects:6,maths:yes,science:yes,EVS:Yes},{name:mike,class:5,Subjects:1,maths:yes,science:no,EVS:no}]' |
    sed -E 's/([a-zA-Z]+):/"\1":/g' |
    sed -E 's/:([a-zA-Z]+)/:"\1"/g' |
    jq '.[] | "\(.name),\(.Subjects),\(.evs)"' --raw-output