简体   繁体   English

Shell脚本while循环,用于将文件转换为变量

[英]Shell Script while loop for converting a file into variable

So I have a snippet of the excel forms, and I am trying to use bash script to convert them into a format we want. 因此,我有一个excel表单的摘要,并且尝试使用bash脚本将其转换为所需的格式。
The file looks like this: 该文件如下所示:
Group1 Member1 组1成员1
********** Member2 **********会员2
********** Member123 **********会员123
Group2 Member183 组2成员183
**********Member1023 ********** Member1023

So on and so forth.... 等等等等....
What the file means, is that it's essentially a form. 该文件的意思是,它本质上是一种表单。 Where it tells you that Group 1 has the member of "Member1" "Member2""Member123". 它告诉您组1具有“成员1”,“成员2”,“成员123”的成员。 THe " ** "means it's a blank field. ** ”表示这是一个空白字段。 I am trying to use a while loop to read all the group names (column1) into variable $Groupname and all the members (column2) into variable $Groupmember, and then use awk to print something like this: 我正在尝试使用while循环将所有组名(column1)读入变量$ Groupname,将所有成员(column2)读入变量$ Groupmember,然后使用awk打印如下内容:
Group 1 Member1 组1成员1
Group 1 Member2 第1组成员2
Group 1 Member123 1组成员123
So how can I do that? 那我该怎么办呢?

Ok, assuming I understand your question correctly, you could do something like this: 好的,假设我正确理解了您的问题,则可以执行以下操作:

input.txt input.txt中

Group1 Member 组1成员
********* Member2 *********会员2
********* Member123 *********会员123
Group2 Member183 组2成员183
********* Member1023 *********会员1023

Then in a bash script, you could keep track of the most recent group used: 然后在bash脚本中,您可以跟踪最近使用的组:

#!/bin/bash

CURRENTGROUP=""

while read -r group member
do
        if [[ ${group:0:1} != "*" ]]; then
                CURRENTGROUP=$group
        fi

        echo "$CURRENTGROUP $member"

done < "input.txt"

Which produces the following output: 产生以下输出:

Group1 Member 组1成员
Group1 Member2 组1成员2
Group1 Member123 组1成员123
Group2 Member183 组2成员183
Group2 Member1023 2组成员1023

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM