linux - 需要有关 shell 脚本的帮助以获得预期输出

标签 linux shell grep

我有一个名为 input.txt 的输入文件,如下所示:

powerOf|creating new file|failure
creatEd|new file creating|failure
powerAp|powerof server|failureof file

我将文本提取到第一个字段中第一个大写字母之前,并将这些片段存储在 output.txt 中:

power
creat

我使用 sed 命令来分离值,它工作正常。

从输出文件 (output.txt),我需要从第一个字段grep,输出应该如下所示:

Power
power:powerOf|creating new file|failure,powerAp|powerof server|failureof file
creat
creat:creatEd|new file creating|failure

我尝试了几种方法,但没有得到预期的输出。

我尝试了以下方法,但我得到了重复的条目:

cat input.txt | cut -d '|' f1 >> input1.txt
cat input1.txt | s/\([a-z]\)\([A-Z]\)/\1 \2/g >> output.txt
while read -r line;do
  echo $ line
  cat input.txt |cut -d ‘|’ f1|grep $line >> output1. txt
done< "output.txt"

我在输入文件中有 20000 行。我不知道为什么我会得到重复的输出。我做错了什么?

最佳答案

Bash 解决方案:

#!/bin/bash
keys=()
declare -A map
while read line; do
    key=$(echo ${line} | cut -d \| -f1 | sed -e 's/[[:upper:]].*$//')
    if [[ -z "${map[$key]}" ]]; then
        keys+=(${key})
        map[$key]="${line}"
    else
        map[$key]+=",${line}"
    fi
done

for key in ${keys[*]}; do
    echo "${key}"
    echo "${key}:${map[$key]}"
done

exit 0

也许 Perl 解决方案也适用于 OP:

#!/usr/bin/perl
use strict;
use warnings;

my @keys;
my %map;
while (<>) {
    chomp;
    my($key) = /^([[:lower:]]+)/;
    if (not exists $map{$key}) {
        push(@keys, $key);
        $map{$key} = [];
    }
    push(@{ $map{$key} }, $_);
}

foreach my $key (@keys) {
    print "$key\n";
    print "$key:", join(",", @{ $map{$key} }), "\n";
}


exit 0;

使用给定的输入进行测试:

$ perl dummy.pl <dummy.txt
power
power:powerOf|creating new file|failure,powerAp|powerof server|failureof file
creat
creat:creatEd|new file creating|failure

UPDATE 在 OP 重述原始问题后。第一个循环的解决方案只包括输入的第二列而不是整行:

    message=$(echo ${line} | cut -d \| -f2)
    if [[ -z "${map[$key]}" ]]; then
        keys+=(${key})
        map[$key]="${message}"
    else
        map[$key]+=",${message}"
    fi

使用给定的输入进行测试:

$ perl dummy.pl <dummy.txt
power
power:creating new file,powerof server
creat
creat:new file creating

关于linux - 需要有关 shell 脚本的帮助以获得预期输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54457521/

相关文章:

linux - Grep-ing 同时保持第一行

Unix命令查找字符串集交集或异常值?

linux - 使用 printf 格式化不均匀的列表

android - 无法在 ubuntu 中运行 adb 命令 adb 服务器已过期

shell - awk获取下一行列的值并将其添加到shellscript中的当前行

bash - Grep apache 服务器 500 错误到一个单独的文件

java - 打开传出端口 (CENTOS)

linux - 使用curl调用SOAP Web服务

linux - 为什么这个 NASM 代码打印我的环境变量?

macos - 从具有 '-x'(精确行匹配)的文件中读取 grep 模式...模式顺序重要吗?