Объединить несколько столбцов на основе значений первого столбца

Aquí hay un ejemplo completo de cómo cambiar el nombre de una conexión pppoe en ppp0 a eth0 (para compatibilidad con secuencias de comandos):

Edite /etc/network/interfaces y configure la conexión ppp (aquí llamada dsl -proveedor )como:

auto dsl-provider
iface dsl-provider inet ppp
pre-up /bin/ip link set eth0 down
pre-up /bin/ip link set eth0 name eth99
pre-up /bin/ip link set eth99 up # line maintained by pppoeconf
provider dsl-provider

En /etc/ppp/peers/dsl -proveedor Agregue lo siguiente:

plugin rp-pppoe.so eth99
ifname eth0

Ahora, al arrancar, su dispositivo ppp0 se llamará eth0 (y su antiguo eth0 ha sido renombrado a eth99 ).

2
19.06.2019, 06:49
2 ответа

Скрипт ниже

STEP1

awk '{print $1}' file1 file2 file3| awk '{if(!seen[$1]++){print $0}}' >pattern_content

STEP2

for i in `awk '{print $1}' file1 file2 file3| sort | uniq`; do grep "$i" file1>/dev/null; if [[ $? == 0 ]]; then grep $i file1| awk '{print $2}'; else echo "                                "; fi; done > file1_o

for i in `awk '{print $1}' file1 file2 file3| sort | uniq`; do grep "$i" file2>/dev/null; if [[ $? == 0 ]]; then grep $i file2| awk '{print $2}'; else echo "                                "; fi; done > file2_o


for i in `awk '{print $1}' file1 file2 file3| sort | uniq`; do grep "$i" file3>/dev/null; if [[ $? == 0 ]]; then grep $i file3| awk '{print $2}'; else echo "                                "; fi; done > file3_o


step3

 paste pattern_content file1_o file2_o file3_o|sed '1i                 file1          file2               file3'| sed "s/file1/\t&/g"

выход

        file1       file2      file3
2000    0.0202094   0.0343179
2001    0.0225532   0.036318
2002    0.02553
2003    0.0261099   0.0395799
2004    0.0280311   0.0412106   0.0686893
2005    0.028843    0.041264    0.0645474
0
27.01.2020, 22:26

Вы можете просто запустить awkдля всех этих файлов за один раз, сгруппировав записи в первом столбце. Часть map[$1]?(map[$1] FS $2):($2)представляет собой тернарный оператор, означающий добавление к карте массива, индексированной $1, если она была пустой, или добавление к уже существующим значениям, если она не -пуста.

awk '{ map[$1] = ($1 in map)?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file*

Чтобы сделать вывод более читаемым, чем вывод, полученный с помощью awk,передать вывод как

awk '{ map[$1] = ($1 in map)?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file* | column -t > mergedfile.txt
0
27.01.2020, 22:26

Теги

Похожие вопросы