I want to combine the second column of the same row with the first column into one row for text processing in bash.

Asked 1 years ago, Updated 1 years ago, 61 views

What should I do if I want to use bash to combine the second row of the same row in the first row into one row?
Illustrated

a1
a2
a3
b4
c5
c6
d7
d8
e9

Text

a 1 2 3
b4
c56
d78
e9

That's how I want to deal with it.Please.

Additional
The text has already been sorted.The results can be sorted or not.
My bash version is version 3.2.57.

I have added your answers and comments.

bash shellscript

2022-09-30 18:10

4 Answers

If you implement it with bash as you asked, assume that the text is stored in a.txt

unset-vHASH
declare-A HASH
while read kv; do HASH[$k]="${HASH[$k]}$v"; done <a.txt
forkin${!HASH[*]}; do echo$k${HASH[$k]}; done

Is that so?If you use other tools such as Perl, it will be different.


2022-09-30 18:10

If the input is sorted by Field 1 and you use POSIX-compatible sh, this is what it looks like.

input(){
   cat<<EOF
a1
a2
a3
b4
c5
c6
d7
d8
e9
EOF
}

efficient_sh()(
    prev=
    while read-rkv
    do
        if ["$prev"="";then
            printf'%s%s'$k"$v"
            prev = $k
        elif ["$prev" = $k]; then
            printf'%s' "$v"
        else
            printf'\n%s%s'$k"$v"
            prev = $k
        fi
    done
    echo
)

input | efficient_sh
# a 1 2 3
# b4
# c56
# d78
# e9


2022-09-30 18:10

An example of using an external command.We do not use Bash extensions.

awk Example

Awk is a standard command.No installation is required by default.

#!/bin/sh
awk'
    US>BEGIN{
        ORS=""
        pre=""
    }
    {
        if($1!=pre){
            if(pre!=""){
                print "\n"
            }
            print $1""$2
            pre = $1
        } else{
            print""$2
        }
    }
    END {print"\n"}
' in.txt

example datamash

#!/bin/sh
datamash --field-separator='--group=1collapse2<in.txt\
    |tr, ' '

GNU datamash is not a standard command.Installation is often required.
The above example assumes that the data does not contain commas (, ).


2022-09-30 18:10

This is a subspecies of Yuki Inoue's response.
Assume that the sorted data is stored in the same directory as data.txt.

#!/bin/sh
set-Cu
# set-vx

while read-r key val;do
    if test "${current:-}" != "$key"; then
        current="$key"
        printf"\n%s""$key"
    fi
    printf "%s" "$val"
done<data.txt
echo

exit0


2022-09-30 18:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.