Как получить значения определенной строки по именам столбцов?

Это выходные данные для команды aws iam get-credential-report --query 'Content' - выходной текст | base64 -d из этого вывода, как получить значения access_key_1_active и access_key_1_last_rotated пользователя username mahi-user ?

задан 25 July 2020 в 12:27

1 ответ

You can use awk in the following way (source):

1st create awk script as this:

$ cat usr-col.awk
BEGIN { FS = "," }
NR==1 {
    for (i=1; i<=NF; i++) {
        ix[$i] = i
NR>1 {
    if ( $1 ~ usr ) print $ix[c1], $ix[c2]


  • FS = "," will set the field separator to ,;
  • NR==1 means if it is the first row of the table;
  • the for loop will create associative array: ix[column name] = ;
  • NR>1 means any row of the table which number is bigger than 1;
  • if ( $1 ~ usr ) will provide output if the first field of a line match to the value of the variable usr (the value could be regex);
  • print $ix[c1] will output the value of the field mummer equal to the column name provided by the value of the variable c1...

2nd use the script in this way:

aws ... | awk -f usr-col.awk usr='mahi-user' c1='access_key_1_active' c2='access_key_1_last_rotated'

I do not have the aws command, so in order to perform a test I've stored the provided output in a file, also I've added few lines with data for other users. Here is one of the tests I made - it searches data for multiple users (mahi-user and spas) within two arbitrary columns (arn and password_enabled):

$ cat aws.output | awk -f usr-col.awk usr='mahi-user|spas' c1='arn' c2='password_enabled'
arn:aws:iam::929815623526:user/mahi-user true
arn:aws:iam::929815623526:user/spas false
ответ дан 30 July 2020 в 22:03

Другие вопросы по тегам:

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