How do we format the logs in the SAM CLI log command to just show the message? - aws-sam-cli

The SAM CLI function to tail the logs is AWESOME. However, I find the formatting to be too verbose most of the time. The default formatting looks like this:
2021/12/13/[$LATEST]c87a125640ed4c66907f22c1d83ca63e 2021-12-13T13:11:09.388000 2021-12-13T13:11:09.388Z 1f99a541-76d7-494f-8f07-3f3e8397eedf INFO small change
but I want to have the option to just print out the message without the log stream and timestamp information so it just looks like this:
INFO small change
Is there a way to pass in a formatting string for the log statements?

You can use awk to only print the 5th and 6th column.
Try:
sam logs -n MyLambdaFunction --tail | awk '{print $5 " " $6}';
Demo:
output="2021/12/13/[$LATEST]c87a125640ed4c66907f22c1d83ca63e 2021-12-13T13:11:09.388000 2021-12-13T13:11:09.388Z 1f99a541-76d7-494f-8f07-3f3e8397eedf INFO small change";
echo $output | awk '{print $5 " " $6}';
Output:
INFO small

Related

Cutting a length of specific string with grep

Let's say we have a string "test123" in a text file.
How do we cut out "test12" only or let's say there is other garbage behind "test123" such as test123x19853 and we want to cut out "test123x"?
I tried with grep -a "test123.\{1,4\}" testasd.txt and so on, but just can't get it right.
I also looked for example, but never found what I'm looking for.
expr:
kent$ x="test123x19853"
kent$ echo $(expr "$x" : '\(test.\{1,4\}\)')
test123x
What you need is -o which print out matched things only:
$ echo "test123x19853"|grep -o "test.\{1,4\}"
test123x
$ echo "test123x19853"|grep -oP "test.{1,4}"
test123x
-o, --only-matching show only the part of a line matching PATTERN
If you are ok with awkthen try following(not this will look for continuous occurrences of alphabets and then continuous occurrences of digits, didn't limit it to 4 or 5).
echo "test123x19853" | awk 'match($0,/[a-zA-Z]+[0-9]+/){print substr($0,RSTART,RLENGTH)}'
In case you want to look for only 1 to 4 digits after 1st continuous occurrence of alphabets then try following(my awk is old version so using --re-interval you could remove it in case you have latest version of ittoo).
echo "test123x19853" | awk --re-interval 'match($0,/[a-zA-Z]+[0-9]{1,4}/){print substr($0,RSTART,RLENGTH)}'

How to show the output in Geneos Active console using Toolkit Plugin

I'm new with Geneos and would like to know how to show the output of our existing script which is previously used in Nagios. We're planning to use the toolkit plugin and not sure what will be the commands to use to be able to see result in active console.
Requirement - check the log if there is session timeout and it will alert OK if grep is equal 20 then Warning alert if less or greater than 20.
Output in Geneos:
column_title - TIMEOUT CHECK, STATUS
row_result - THE_FILE, OK: Session Timeout is 20
Here's our sample script:
#!/bin/ksh
OK=0
WARNING=1
CRITICAL=2
THE_FILE=/target/directory/web.txt
TIMEOUT=`grep "<session-timeout>" $THE_FILE | awk -F'>' '{print $2}' | awk -F'>' '{print $1}'
if [$TIMEOUT -eq 20 ]; then
echo "OK: Session Timeout is $TIMEOUT"
exit $OK
else
echo "WARNING: Session Timeout is $TIMEOUT"
exit $WARNING
fi
Thanks!
You can use the same script (adding the header) and adding it to Geneos. But I highly recommend you to use a FKM sampler, and check this log file using Geneos directly.
Hope this helps you.

awk help > Sending output to a command

I am by no means a programmer. I'll put that right out there. However, I'm trying to write a script that writes fstab entries after grabbing uuid data. This is in a OpenWRT environment on my router. My goal:
Grab uuid info using awk, like:
blkid /dev/sdb2 | awk -F'UUID="|"' '{print $2}'
Send the full uuid output to the end of this command:
uci set fstab.#mount[-1].uuid=
Execute that command with the correct uuid.
This command writes that uuid to the correct place in fstab. How can this be accomplished in a bash-script?
Thanks,
KG
You can either execute the command from within awk (using system(...)), or from within the shell (using uci set fstab.#mount[-1].uuid=$(blkid ...)).
I can't test it with the uci command but I believe this should do what you need.
bblkid /dev/sda1 | uci set fstab.#mount[-1].uuid=$( awk -F'UUID="|"' '{print $2}' )
Edit: this would probably be better.
uci set fstab.#mount[-1].uuid=$( bblkid /dev/sda1 | awk -F'UUID="|"' '{print $2}' )
Edit 2: little bit shorter but should get the same result. Promise this is the last one
uci set fstab.#mount[-1].uuid=$( bblkid /dev/sda1 | tr -d UUID=\" )
The above options didn't work quite right.
What if I have this in fstab:
option uuid 'hotdog'
I want to replace the word "hotdog" with the output of the uuid from this command:
On my router that output looks like:
root#OpenWrt:/etc/config# blkid /dev/sdb2 | awk -F'UUID="|"' '{print $2}'
8618b8fe-93a9-488c-a901-0df30898c82e
or
root#OpenWrt:/etc/config# block info | grep sdb2
/dev/sdb2: UUID="8618b8fe-93a9-488c-a901-0df30898c82e" NAME="EXT_JOURNAL" VERSION="1.0" TYPE="ext4"
I want to then replace the word hotdog with the random uuid, and keep the semi-colons. Keep in mind that the uuid will change each time I format that partition.

Find parameters of a method with grep

I need some help with a grep command (in the Bash).
In my source files, I want to list all unique parameters of a function. Background: I want to search through all files, to see, which permissions ([perm("abc")] are used.
Example.txt:
if (x) perm("this"); else perm("that");
perm("what");
I'd like to have my grep output:
this
that
what
If I do my grep with this search expression
perm\(\"(.*?)\"\)
I'll get perm("this), perm("that"), etc. but I'd like to have just the permissions: this and that and what.
How can I do that?
Use a look-behind:
$ grep -Po '(?<=perm\(")[^"]*' file
this
that
what
This looks for all the text occurring after perm(" and until another " is found.
Note -P is used to allow this behaviour (it is a Perl regex) and -o to just print the matched item, instead of the whole line.
Here is a gnu awk version (due to multiple characters in RS)
awk -v RS='perm\\("' -F\" 'NR>1 {print $1}' file
this
that
what

Use awk to parse and modify every CSV field

I need to parse and modify a each field from a CSV header line for a dynamic sqlite create table statement. Below is what works from the command line with the appropriate output:
echo ",header1,header2,header3"| awk 'BEGIN {FS=","}; {for(i=2;i<=NF;i++){printf ",%s text ", $i}; printf "\n"}'
,header1 text ,header2 text ,header3 text
Well, it breaks when it is run from within a bash shell script. I got it to work by writing the output to a file like below:
echo $optionalHeaders | awk 'BEGIN {FS=","}; {for(i=2;i<=NF;i++){printf ",%s text ", $i}; printf "\n"}' > optionalHeaders.txt
This sucks! There are a lot of examples that show how to parse/modify specific Nth fields. This issue requires each field to be modified. Is there a more concise and elegant Awk one liner that can store its contents to a variable rather than writing to a file?
sed is usually the right tool for simple substitutions on a single line. Take your pick:
$ echo ",header1,header2,header3" | sed 's/[^,][^,]*/& text/g'
,header1 text,header2 text,header3 text
$ echo ",header1,header2,header3" | sed -r 's/[^,]+/& text/g'
,header1 text,header2 text,header3 text
The last 1 above requires GNU sed to use EREs instead of BREs. You can do the same in awk using gsub() if you prefer:
$ echo ",header1,header2,header3" | awk '{gsub(/[^,]+/,"& text")}1'
,header1 text,header2 text,header3 text
I found the problem and it was me... I forgot to echo the contents of the variable to the Awk command. Brianadams comment was so simple that forced me to re-look at my code and find the problem! Thanks!
I am ok with resolving this but if anyone wants to propose a more concise and elegant Awk one liner - that would be cool.
You can try the following:
#! /bin/bash
header=",header1,header2,header3"
newhead=$(awk 'BEGIN {FS=OFS=","}; {for(i=2;i<=NF;i++) $i=$i" text"}1' <<<"$header")
echo "$newhead"
with output:
,header1 text,header2 text,header3 text
Instead of modifying fields one by one, another option is with a simple substitution:
echo ",header1,header2,header3" | awk '{gsub(/[^,]+/, "& text", $0); print}'
That is, replace a sequence of non-comma characters with text appended.
Another alternative would be replacing the commas, but due to the irregularities of your header line (first comma must be left alone, no comma at the end), that's a bit less easy:
echo ",header1,header2,header3" | awk '{gsub(/,/, " text,", $0); sub(/^ text,/, "", $0); print $0 " text"}'
Btw, the rough equivalent of the two commands in sed:
echo ",header1,header2,header3" | sed -e 's/[^,]\{1,\}/& text/g'
echo ",header1,header2,header3" | sed -e 's/\(.\),/\1 text,/g' -e 's/$/ text/'

Resources