I'm trying to use AWK command, it's printing for me very well and what exact way I want
My question is, how can I know from script if the awk command I used has printed something without spoiling and change the way it prints ?
commands I used: gawk 'BEGIN{RS=ORS="\\n\\n" {s=tolower($0)} s~/word1|word2/' input_file.log
I tried:
status=gawk 'BEGIN{RS=ORS="\\n\\n" {s=tolower($0)} s~/word1|word2/' input_file.log
if [ -z $status]
then
//status is empty or null, means nothing printed in awk command
echo "nothing"
else
//printed something in awk command
echo $status
the issue is that echo $status
prints in sequence all the lines, without the "new lines" between the lines
how can print the original prints from awk without spoiling it?
example: input file:
line 0 no words in here
line 1 starting
line 1 word1
line 2 no words here as well
line 3 starting
line 3 word2
line 3 end
line 4 nothing
line 5 nothing
command: gawk 'BEGIN{RS=ORS="\\n\\n" {s=tolower($0)} s~/word1|word2/' input_file.log
expected output:
line 1 starting
line 1 word1
line 3 starting
line 3 word2
line 3 end
if I use: stat=$(gawk 'BEGIN{RS=ORS="\\n\\n" {s=tolower($0)} s~/word1|word2/' input_file.log)
echo $stat
I get as output:
line 1 starting line 1 word1 line 3 starting line 3 word2 line 3 end
Thanks in advance!
Not fully sure as you haven't shown any sample Input_file or expected output, so could you please try using echo "$status"
.
EDIT: As you have edited your post now, so you should change your code to following and it should fly then.
status=$(awk 'BEGIN{RS=ORS="\n\n"} {s=tolower($0)} s~/word1|word2/' Input_file)
if [[ -z $status ]]
then
echo "nothing"
else
echo "$status"
fi
You can use exit
code for checking whether awk
has printed something or not
Correct your code
From
gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} s~/word1|word2/' input_file.log
TO
status=$(gawk 'BEGIN{RS=ORS="\n\n"}tolower($0)~/word1|word2/' input_file.log)
and (with quotes)
echo "$status"
This happens because when you quote a parameter (whether that parameter is passed to
echo
,test
or some other command), the value of that parameter is sent as one value to the command. If you don't quote it, the shell does its normal magic of looking for whitespace to determine where each parameter starts and ends.
To correct existing code
#!/usr/bin/env bash
status=$(gawk 'BEGIN{RS=ORS="\n\n"}tolower($0)~/word1|word2/' input_file.log)
if [ -z "$status" ]; then
echo "looks like nothing matched and so nothing printed"
else
echo "awk matched regex and printed something"
fi
Here is code for checking awk has printed something or not using exit code :
gawk 'BEGIN{RS=ORS="\n\n"}f=(tolower($0)~/word1|word2/){e=1}f; END{exit !e}' input_file.log
# check exit code
if [ "$?" -eq 0 ]; then
echo "awk matched regex and printed something"
else
echo "looks like nothing matched and so nothing printed"
fi
Test Results:
$ cat test.sh
#!/usr/bin/env bash
gawk 'BEGIN{RS=ORS="\n\n"}f=(tolower($0)~/word1|word2/){e=1}f; END{exit !e}' "$1"
if [ "$?" -eq 0 ]; then
echo "awk matched regex and printed something"
else
echo "looks like nothing matched and so nothing printed"
fi
sample files for testing
$ echo 'word1' >file1
$ echo 'nothing' >file2
Contents of files
$ cat file1
word1
$ cat file2
nothing
Execution with first file
$ bash test.sh file1
word1
awk matched regex and printed something
Execution with second file
$ bash test.sh file2
looks like nothing matched and so nothing printed
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.