В ~ / .ssh / config есть что-то вроде этого:
Host server1
HostName server1
ForwardX11 yes
User username
В языке высокого -уровня вы бы использовали массив массивов, но в bash их нет. Проблемы, предполагающие многоуровневые структуры данных, как эта, обычно очень утомительны для решения в оболочке.
Но поскольку ваша цель — изучить обработку текста в Unix, а не в Python, давайте решим ее в оболочке.
В этом решении мы читаем файл один раз, чтобы получить заголовки строк, а затем снова столько раз, сколько потребуется, чтобы собрать необходимое количество элементов. Мы сохраняем два массива:outrow
— это массив выходных строк, каждая из которых добавляется по мере продвижения; cursor
— это массив целых чисел, в котором хранится наше место в каждой строке.
Обратите внимание, что этот скрипт будет бесконечно зацикливаться, если для удовлетворения запроса недостаточно элементов. Решение этого остается в качестве упражнения для читателя.
#!/bin/bash
k=$1
input=input.txt
declare -a outrow
declare -a cursor
K=0
n=0
while read line
do
outrow[$n]=${line%% *}
cursor[$n]=1
(( n++ ))
done < $input
while [[ $K -lt $k ]]
do
n=0
while read line
do
declare -a col=( $line )
if [[ ${#col[@]} -gt ${cursor[$n]} ]]
then
outrow[$n]+=" ${col[ ${cursor[$n]} ]}"
(( cursor[$n]++ ))
(( K++ ))
[[ $K -lt $k ]] || break
fi
(( n++ ))
done < $input
done
for row in "${outrow[@]}"
do
echo "$row"
done
Обратите внимание :, изменяя переменную num
, вы можете регулировать количество элементов.
gawk -v num=5 '
BEGIN {
PROCINFO["sorted_in"] = "@ind_str_asc"
}
{
###
# Traverse throught input.txt from first to last line
# and store all elements in the two-dimensional array - table
# along the way, maintain the array of counters for each letter
###
# The array of counters for each unique element from the first column.
# In our case the indexes of array are capital letters (A, B, C, D)
# and values are the amount of each letter occurrences.
cnt_arr[$1]++
# Two dimension array - table
# it looks like chess board - rows named by letters (A, B, C, D)
# and columns named by numbers (1, 2, 3, 4, 5... etc).
# Its cells contains numbers from the second column.
# For example, if letter A occurrences 5 times in the input.txt
# then, the table will have the A row with 5 columns
table[$1][cnt_arr[$1]] = $2
}
# At this point, all lines from input.txt are processed
# and stored in the table
END {
# Do needed number of iterations - specified by the num variable
for(i = 0; i < num; i++) {
# On each iteration run the inner loop,
# which iterating through all rows in the table
for(row_name in table) {
# Check each cell - if it is non-empty
# add its value to the result_arr[row_name], separated by OFS.
# OFS - output field separator, the space by default
if(table[row_name][i]) {
result_arr[row_name] = result_arr[row_name] OFS table[row_name][i]
# and count the number of succesful occurences
cnt++
}
# If count of non-empty cells equals to the num variable
# or equals to the NR (number of records|lines)
# print the result_arr and exit
if(cnt == num || cnt >= NR) {
for(i in result_arr) {
print i result_arr[i]
}
exit
}
}
}
}' input.txt
Информация о строке PROCINFO["sorted_in"] = "@ind_str_asc"
находится здесь .
Вход
A 1
B 2
C 9
D 1
A 5
B 3
C 9
A 6
C 7
A 5
C 1
Выход
A 1 5
B 2
C 9
D 1