LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Remove Error From Output Using Sed in Bash (https://www.linuxquestions.org/questions/linux-newbie-8/remove-error-from-output-using-sed-in-bash-4175637905/)

bluethundr 09-06-2018 10:27 AM

Remove Error From Output Using Sed in Bash
 
I'm building a list of aws users with this command:

Code:

aws iam get-user --user-name "$aws_user_name" --profile="$aws_key"
If the user doesn't exist you get this output:

Code:

aws iam get-user --user-name fred --profile=lab
   
    An error occurred (NoSuchEntity) when calling the GetUser operation: The user with name fred cannot be found.

I can suppress that error on the user output with this command, but a new error appears:

Code:

aws iam get-user --user-name fred --profile=lab 2>&1 | sed 's/^An error occurred (NoSuchEntity)\.*//g' |  jq -r '.User.UserName'
    parse error: Invalid numeric literal at line 2, column 6

I try to remove the parse error with the next line, but it doesn't work

Code:

aws iam get-user --user-name fred --profile=lab 2>&1 | sed 's/^An error occurred (NoSuchEntity)\.*//g' |  sed 's/parse\ error\.*//g' | jq -r '.User.UserName'
    parse error: Invalid numeric literal at line 2, column 6

How can I get rid of the parse error from the user output?

I'm writing the same line to a log file with the error intact for later analysis. But I don't want the user to see these errors.

rtmistler 09-06-2018 10:38 AM

I think you want to be more simple than global search-replace.

I wasn't totally sure, so looked up "sed delete line" https://stackoverflow.com/questions/...pecific-string

So you might do something more like:
Code:

sed 'An error occurred/d'
That should delete any lines containing that pattern.

individual 09-06-2018 10:59 AM

Try changing
Code:

jq -r '.User.UserName'
to
Code:

jq -r '.User?.UserName?'
.
By adding the question marks jq won't complain if either of those keys don't exist.

berndbausch 09-06-2018 11:38 PM

Quote:

Originally Posted by bluethundr (Post 5900533)
I'm building a list of aws users with this command:

Code:

aws iam get-user --user-name "$aws_user_name" --profile="$aws_key"
If the user doesn't exist you get this output:

Code:

aws iam get-user --user-name fred --profile=lab
   
    An error occurred (NoSuchEntity) when calling the GetUser operation: The user with name fred cannot be found.

I can suppress that error on the user output with this command, but a new error appears:

Code:

aws iam get-user --user-name fred --profile=lab 2>&1 | sed 's/^An error occurred (NoSuchEntity)\.*//g' |  jq -r '.User.UserName'
    parse error: Invalid numeric literal at line 2, column 6

I try to remove the parse error with the next line, but it doesn't work

Code:

aws iam get-user --user-name fred --profile=lab 2>&1 | sed 's/^An error occurred (NoSuchEntity)\.*//g' |  sed 's/parse\ error\.*//g' | jq -r '.User.UserName'
    parse error: Invalid numeric literal at line 2, column 6

How can I get rid of the parse error from the user output?

I'm writing the same line to a log file with the error intact for later analysis. But I don't want the user to see these errors.

Is it an option to simply redirect all error output to some file for later analysis? E.g.
Code:

aws ... 2>/var/tmp/aws.err
If not, run the pipeline without the jq command and check line 2, column 6.

By the way, the \.* part of your sed string means “any number of dots” and is probably irrelevant.


All times are GMT -5. The time now is 11:05 PM.