如何在bash输入文件格式特殊的格式? [英] How to format an input file in bash to a special format?

查看:109
本文介绍了如何在bash输入文件格式特殊的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件通过我从不同的服务器的crontab一个bash脚本,看起来像这样的格式生成:

i have a textfile i generate via a bash script from various server crontabs that looks like this format:

0系统1服务器1

5系统2服务器2

2,3 SYS3

0 SYS3服务器3

0 sys3 server3

7 SYS4服务器4

7 sys4 server4

...

和我想让它变得格式化这样:

And i want it to get formated to this:

0系统1服务器1

5系统2服务器2

2 SYS3服务器3

2 sys3 server3

3 SYS3服务器3

3 sys3 server3

0 SYS3服务器3

0 sys3 server3

7 SYS4服务器4

7 sys4 server4

...

我的问题是,我可以随时更改,如果有每个SYS /服务器以上两个数字有可能也超过两个crontab项为每个服务器。

The problem i have is that i it can always change if there are two numbers per sys/server or more and there could be also more than two crontab entries for each server.

我学尝试是这样的:

    rday_old=""
    rsys_old=""
    rser_old=""
    [[ -e output ]] && rm output
    while read -u5 -r -a line; do
            rday=${line[0]}
            rsys=${line[1]}
            rser=${line[2]} 
            if [[ "$rsys_old" == "$rsys" ]]; then
                    echo "$rday_old $rsys_old $rser" >> output
            else
                    echo "$rday $rsys $rser" >> output    
            fi   
            rday_old=$rday
            rsys_old=$rsys
            rser_old=$rser  
    done 5< input_file

问题IST明显,它不会象预期的工作。我不知道什么是最好的方式来解决,这是。这一天需要,如果它是在2,3格式奥得甚至2,4,5至7天中的crontab格式(这将是一个*符号)被分割我学尝试没有考虑到的。此外,它可能发生,我需要存储多个rday_old,rsys_old和rser_old。

The problem ist obviously that it won't work like intended. I don't know what the best way to solve this is. My attemp doesn't take into account that the day needs to be split if it is in the format of 2,3 oder even 2,4,5 up to 7 days in crontab format (which would be an * sign). Additionally it could happen that i need to store more than one rday_old, rsys_old and rser_old.

我希望我的方式,是可以理解的说明我的问题。
感谢提前任何帮助。

I hope i stated my problem in a way that is understandable. Thanks for any help in advance.

编辑:

在@anishsane的答案我固定他的回答

After the answer of @anishsane i fixed his answer to

cat input | awk '{sys=$2; ser=$3; split($1,a,","); for(i in a){print a[i]" "  sys " " ser}}' > output

但现在输出的结果是:

but it outputs now as result:

0系统1服务器1

5系统2服务器2

2 SYS3

3 SYS3服务器3

3 sys3 server3

0 SYS3服务器3

0 sys3 server3

7 SYS4服务器4

7 sys4 server4

...

所以我几乎没有。

推荐答案

GAWK 方式:

$ cat srvlist
0 sys1 server1
5 sys2 server2
2,3 sys3
0 sys3 server3
7 sys4 server4

$ awk '{sys=gensub("sys","","",$2); split($1,a,","); for(i in a){print a[i] " sys" sys " server" sys}}' srvlist
0 sys1 server1
5 sys2 server2
2 sys3 server3
3 sys3 server3
0 sys3 server3
7 sys4 server4

说明:


  1. SYS 变量中提取出第二个字段的号码 - 系统1,SYS3等。(3本将包含1,分别)

  2. 将逗号作为分隔符到数组拆分第一场 A

  3. 作为与多循环所需的打印字符串 A

  4. 字段3在输入文件中被忽略。至少在输入端提供,场2&安培; 3总是有相同的号码。

  1. sys variable extracts the number out of second field - sys1, sys3 etc. (This would contain 1, 3 respectively)
  2. split first field by comma as delimiter into array a
  3. print the string as desired with loop over a
  4. Field 3 in the input file is ignored. At least for the input provided, field2 & 3 always have the same number.

这篇关于如何在bash输入文件格式特殊的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆