Problems reading a system file with awk - bash

I am trying to execute a shell script in which I am trying to open the file /sys/class/power_supply/BAT0/status through awk. But, the script fails to execute saying that the file cannot be read. Even executing the script with sudo does not work. Below is the script. I am on Kubuntu 13.10. I searched a lot, but couldn't find a solution. Thanks for the help!
#!/bin/sh
awk '{
echo $0
}' | /sys/class/power_supply/BAT0/status

The general form of an awk command is:
awk '<awk script commands>' input-file
As status is the input file it should follow the awk scripting. The pipe symbol does not make sense here.
#!/bin/sh
awk '{
print $0
}' /sys/class/power_supply/BAT0/status

Related

Is it possible to pass a script to awk inside a shell variable?

Is it possible to store an awk script inside a shell variable; for example:
export script="'{printf(\$2); printf("\"\\n\"");}'"
echo $script
'{printf($2); printf("\n");}'
The script functions properly when I call it directly as such:
awk '{printf($2); printf("\n");}' testFile.txt
prints proper output
When I try and pass the script as a shell variable, I run into issues.
awk $script testFile.txt
awk: syntax error at source line 1
context is
>>> ' <<<
missing }
awk: bailing out at source line 1
I get a slightly different error when I wrap the variable in double quotes
awk "$script" testFile.txt
awk: syntax error at source line 1
context is
>>> ' <<<
awk: bailing out at source line 1
I'm still learning exactly how shell expansions work, I would appreciate any suggestions about what I am missing here.
Error in your quoting
export script='{printf($2); printf("\n");}'
awk "${script}" YourFile
I am not sure about the proper answer to this, but a very ugly (and probably unstable depending on the $script contents) workaround would be:
echo $script | awk '{print "awk "$0" testFile.txt"}' | bash
This is just printing the contents of $script in an awk statement that is then executed by bash. I am not particularly proud of this, but maybe it helps!
When you type
awk '{printf($2); printf("\n");}' testFile.txt
awk only sees {printf($2); printf("\n");} -- the shell removes the quotes
(see Quote Removal in the bash manual)
Heed #NeronLeVelu's answer.

Using a variable in awk command to name file

I am splitting up a file with awk command, I want to name the file using a variable however I have not had much luck. Here is the line:
awk '/STX/ {f="$tp"++i;} {print > f}' $tp.mixed
this just creates files with $tp# as name.
I read the post "How to use shell variables in awk script" but was unable to figure out how to apply that to me question.
You can use this awk command to pass variable from command line:
awk -v tp="$tp" '/STX/{close(f); f=tp (++i)} f{print > f} END{close(f)}' "$tp.mixed"
Also important is to close the files you're opening for writing output. By calling close we are avoiding memory leak due to large number of opened files.
anubhava answered my question in the comments:
awk -v tp="$tp" '/STX/ {if (f) close(f); f = tp (++i)} {print > f} END{if (f) close(f)}' $tp.mixed
works
thank you everyone for your help

Need help in bash scripting

I am new to shell scripting and I am encountering some issue with my codes. Currently, I have this line of code which is able to run perfectly on my terminal which will give me the result which I am looking for:
cat BookDB.txt | awk -F ":" '$1 ~ $Title'
However, when I try to implement this into my script, no result is shown. Anyone able to help me with this problem?
Is $Title supposed to be a shell variable? If so, the shell can't substitute it because the awk body is in single quotes. Use awk's -v option to pass shell variables into awk:
awk -F : -v "title=$Title" '$1 ~ title' BookDB.txt
See also https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat

Self-contained awk script: Saving to file, calling file

For a lab, I wrote a shell script that used awk to do some stuff. Rereading the lab's directions, it seems that I was supposed to write a self-contained awk script. I'm working on translating my bash script into awk, and I'm having a problem right now:
I want to save the output of an awk command to a new file, and then I want to use that output as input for another awk command.
In my bash script, I have this:
awk '/Blocked SPAM/' maillog > spamlog
cat spamlog | awk '{print $0}' RS=' '
It takes all the lines from maillog that contain the string "Blocked SPAM" and saves this to a new file titled spamlog. Then it opens spamlog and replaces every space character ' ' with a new line.
For my awk script, maillog is the file that is passed to the script from shell. My attempt at writing analogous code:
/Blocked SPAM/ > spamlog`
-f spamlog {print $0} RS=' '
I don't really know what I'm doing with my awk script since I'm having trouble finding useful resources for self-contained awk scripts.
awk '/Blocked SPAM/{ print > "spamlog"; gsub( " ","\n"); print }' maillog
Personally, I prefer to invoke that directly from a shell script, but you can easily make it an awk script by writing:
#!/usr/bin/awk -f
/Blocked SPAM/{ print > "spamlog"; gsub( " ","\n"); print }
Invoke that script with 'maillog' as an argument.

need example for awk command and awk within awk command in in shell script

I need a simple working example that makes me understand
1, awk command in shell script.
2, awk withing awk command in shell script.
I'm not really sure what the point is, but here's awk calling awk from within a shell script...
#!/bin/sh
cmd='BEGIN {print \"foo\"}'
echo foo |
awk "{ system( \"awk '$cmd'\" )}"

Resources