LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2018, 10:27 AM   #1
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 144

Rep: Reputation: 15
Post 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.
 
Old 09-06-2018, 10:38 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 09-06-2018, 10:59 AM   #3
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 09-06-2018, 11:38 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by bluethundr View Post
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.
 
  


Reply

Tags
bash, bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [bash] can't remove a line from a variable using sed Ari.G Programming 3 02-13-2012 10:01 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
[SOLVED] sed in a bash loop with ascending output gary_in_springhill Programming 20 01-14-2011 10:35 AM
[bash / sed] remove all data between < > Ljohan Programming 4 03-15-2006 05:20 AM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration