Combine sed commands into one command in bash script

bluethundr

I'd like to improve this line:

aws_iam_role=$(aws iam get-role --role-name rl-company-admin --profile=company-bill |
jq -r '.Role.AssumeRolePolicyDocument.Statement[].Principal.AWS' |
sed "s/arn:aws:iam::123456789101:user\///g" |
grep tdunphy |
sed 's/"//g'|
sed 's/,//g')

This is the raw output of the command with no processing:

aws iam get-role --role-name rl-company-admin --profile=company-bill
{
    "Role": {
        "Description": "company Admin Role", 
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17", 
            "Statement": [
                {
                    "Action": "sts:AssumeRole", 
                    "Effect": "Allow", 
                    "Principal": {
                        "AWS": [
                            "arn:aws:iam::123456789101:user/tdunphy", 
                            "arn:aws:iam::123456789101:user/user1", 
                            "arn:aws:iam::123456789101:user/user2", 
                            "arn:aws:iam::123456789101:user/user3", 
                        ]
                    }
                }
            ]
        }, 
        "MaxSessionDuration": 3600, 
        "RoleId": "AROAJGNAT3IXV7DIWSDCK", 
        "CreateDate": "2018-01-18T17:35:27Z", 
        "RoleName": "rl-company-admin", 
        "Path": "/", 
        "Arn": "arn:aws:iam::188087670762:role/rl-company-admin"
    }
}

How can I use one sed statement to process this text instead of using multiple sed statements?

anubhava

It can be done in single jq command like this this avoiding invoking multiple external commands e.g. grep, sed etc:

aws iam get-role --role-name rl-company-admin --profile=company-bill |
jq --arg u 'tdunphy' -r '
 .Role.AssumeRolePolicyDocument.Statement[].Principal.AWS[] |
 select(. | endswith($u)) | sub("arn:aws:iam::123456789101:user/"; "")'

Output:

tdunphy

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Execute sed command in a bash script

分類Dev

Why combine commands on a single line in a Bash script?

分類Dev

Multiple find -exec commands in one bash script doesn't work?

分類Dev

Commands Working In Terminal, But Not In Bash Script

分類Dev

bash SED command explanation with semicolon

分類Dev

Implement multiple substitution sed commands on one line?

分類Dev

Bash: command substitution with multiple unset/export commands

分類Dev

Bash script - unix - command not found

分類Dev

Command works in terminal but not in bash script

分類Dev

Run multiple commands and kill them as one in bash

分類Dev

exit a bash command in a script without exiting the script

分類Dev

Using bash variables in perl command in bash script

分類Dev

Bash script saving output of pipe sed to variable

分類Dev

sed: Conditional line deletion in one command

分類Dev

ksh/bash Formatting Files through sed by cat file|sed command

分類Dev

Running a command in a new terminal instance in a bash script

分類Dev

Bash script to monitor file change and execute command

分類Dev

How to execute a command within a bash script?

分類Dev

Linux Bash Script, Single Command But Multiple Lines?

分類Dev

bash: Why are () causing error in script but not on the command line?

分類Dev

Bash script using mysql command with variables failing

分類Dev

How to turn Diff command into bash script with prompt

分類Dev

bash script to check website content by curl command

分類Dev

Bash script can't find command if quoted

分類Dev

Bash script that runs a command with arguments and redirects

分類Dev

Running a shell script with and without "bash" command

分類Dev

Batch command/script to do commands from .txt file?

分類Dev

How can I use two bash commands in -exec of find command?

分類Dev

How to run PowerShell core commands in bash script directly?

Related 関連記事

  1. 1

    Execute sed command in a bash script

  2. 2

    Why combine commands on a single line in a Bash script?

  3. 3

    Multiple find -exec commands in one bash script doesn't work?

  4. 4

    Commands Working In Terminal, But Not In Bash Script

  5. 5

    bash SED command explanation with semicolon

  6. 6

    Implement multiple substitution sed commands on one line?

  7. 7

    Bash: command substitution with multiple unset/export commands

  8. 8

    Bash script - unix - command not found

  9. 9

    Command works in terminal but not in bash script

  10. 10

    Run multiple commands and kill them as one in bash

  11. 11

    exit a bash command in a script without exiting the script

  12. 12

    Using bash variables in perl command in bash script

  13. 13

    Bash script saving output of pipe sed to variable

  14. 14

    sed: Conditional line deletion in one command

  15. 15

    ksh/bash Formatting Files through sed by cat file|sed command

  16. 16

    Running a command in a new terminal instance in a bash script

  17. 17

    Bash script to monitor file change and execute command

  18. 18

    How to execute a command within a bash script?

  19. 19

    Linux Bash Script, Single Command But Multiple Lines?

  20. 20

    bash: Why are () causing error in script but not on the command line?

  21. 21

    Bash script using mysql command with variables failing

  22. 22

    How to turn Diff command into bash script with prompt

  23. 23

    bash script to check website content by curl command

  24. 24

    Bash script can't find command if quoted

  25. 25

    Bash script that runs a command with arguments and redirects

  26. 26

    Running a shell script with and without "bash" command

  27. 27

    Batch command/script to do commands from .txt file?

  28. 28

    How can I use two bash commands in -exec of find command?

  29. 29

    How to run PowerShell core commands in bash script directly?

ホットタグ

アーカイブ