using variable external like searching pattern in awk - nawk

I have next example of script:
The proposal is to find ID = 67109AB inside "file.txt", using an external variable, in this case, it's called: var.
But, when I run script, it doesn't take value of variable like search pattern.
So, someone can help me to know if there is missing something?
Thanks for your help.
fu="/67109AB/"
awk -v var="$fu" '
var {
print $0
}' file.txt

I think you're looking for something like this:
fu="67109AB"; awk -v var="$fu" '$0 ~ var' file.txt

Related

How to redirect output to file if no console detected in Nim

I want my Nim program to write to the console if there is one, and redirect echo to write to a file if there isn't. Is there an equivalent to the Environment.UserInteractive property in .NET which I could use to detect if no console is available and redirect stdout in that case?
It's a combination of using isatty() as suggested by genotrance and the code that you found :)
# stdout_to_file.nim
import terminal, strformat, times
if isatty(stdout): # ./stdout_to_file
echo "This is output to the terminal."
else: # ./stdout_to_file | cat
const
logFileName = "log.txt"
let
# https://github.com/jasonrbriggs/nimwhistle/blob/183c19556d6f11013959d17dfafd43486e1109e5/tests/cgitests.nim#L15
logFile = open(logFileName, fmWrite)
stdout = logFile
echo fmt"This is output to the {logFileName} file."
echo fmt"- Run using nim {NimVersion} on {now()}."
Save above file as stdout_to_file.nim.
On running:
nim c stdout_to_file.nim && ./stdout_to_file | cat
I get this in the created log.txt:
This is output to the log.txt file.
- Run using nim 0.19.9 on 2019-01-23T22:42:27-05:00.
You should be able to use isatty().
Here's an example in Nimble.
Edit:
#tjohnson this is in response to your comment. I don't have enough points to respond to your comment directly or something? Thanks Stack Overflow...
It's hard to say without seeing more of the code.
What version of Nim are you using?
I suspect stdout has been shadowed by a read only symbol.
Are you calling this code inside of a proc and passing stdout as an argument?
like this:
proc foo(stdout: File)
If so, you will need to change it to a var parameter to make the argument writable:
proc test(stdout: var File)
Or use stdout as a global variable instead.

PigLatin and print a message

I'm using a grunt shell of PIGLATIN and I am trying to print a simple message, like in shell ECHO "result is :" and then result given by Pig script .
However I have done all the searches and no luck so far.
Echo returns error , same as print.
I can't use UDFs...
You can DUMP the alias or STORE the alias in to file to see the alias values.
Refer :
http://chimera.labs.oreilly.com/books/1234000001811/ch05.html#pl_dump
http://chimera.labs.oreilly.com/books/1234000001811/ch05.html#pl_store

Search string occurrence and display directory wise count

We have a error log directory structure wherein we store all errors log files for a particular day in datewise directories -
errorbackup/20150629/errorlogFile3453123.log.xml
errorbackup/20150629/errorlogFile5676934.log.xml
errorbackup/20150629/errorlogFile9812387.log.xml
errorbackup/20150628/errorlogFile1097172.log.xml
errorbackup/20150628/errorlogFile1908071_log.xml
errorbackup/20150627/errorlogFile5675733.log.xml
errorbackup/20150627/errorlogFile9452344.log.xml
errorbackup/20150626/errorlogFile6363446.log.xml
I want to search for a particular string in the error log file and get the output such that I will get directory wise search result of a count of that string's occurrence. For example grep "blahblahSQLError" should output something like-
20150629:0
20150628:0
20150627:1
20150626:1
This is needed because we fixed some errors in one of the release and I want to make sure that there are no occurrences of that error since the day it was deployed to Prod. Also note that there are thousands of error log files created every day. Each error log file is created with a random number in its name to ensure uniqueness.
If you are sure the filenames of the log files will not contain any "odd" characters or newlines then something like the following should work.
for dir in errorbackup/*; do
printf '%s:%s\n' "${dir#*/}" "$(grep -l blahblahSQLError "$dir/"*.xml | wc -l)"
done
If they can have unexpected names then you would need to use multiple calls to grep and count the matching files manually I believe. Something like this.
for dir in errorbackup/*; do
_dcount=0;
for log in "$dir"/*.xml; do
grep -l blahblahSQLError "$log" && _dcount=$((_dcount + 1));
done
done
Something like this should do it:
for dir in errorbackup/*
do
awk -v dir="${dir##*/}" -v OFS=':' '/blahblahSQLError/{c++} END{print dir, c+0}' "$dir"/*
done
There's probably a cuter way to do it with find and xargs to avoid the loop and you could certainly do it all within one awk command but life's too short....

Strip parameter values from URL

I'd like to strip parameter values from a URL, but leave the parameter names in place: I.e.,
Change
http://abc.def.edu/pager/page.cfm?pai=97878&pager=123
into
http://abc.def.edu/pager/page.cfm?pai=&pager=
I've tried:
sed "s/=.*\&/=\&/g"
With no success. Am I getting close? I've seen lots of posts about stripping parameters entirely, but nothing about just stripping the values. Please redirect me and accept my apologies if this has already been addressed.
Thanks,
Al
$ sed -r 's/=[^\&]+/=/g' <<< 'http://abc.def.edu/pager/page.cfm?pai=97878&pager=123'
OUTPUT:
http://abc.def.edu/pager/page.cfm?pai=&pager=
A bit more heavy-weight than sed:
$ perl -pe 's/(?<==).+?(?=&|$)//g' <<< "$url"
http://abc.def.edu/pager/page.cfm?pai=&pager=

jq substring gives "jq: error: Cannot index string with object"

Problem
I'm trying to filter a json JQ result to only show a substring of the original string. For example if a JQ filter grabed the value
4ffceab674ea8bb5ec421c612536696839bbaccecf64e851dfc270d795ee55d1
I want it to only return the first 10 characters 4ffceab674.
What I've tried
On the Official JQ website you can find an example that should give me what I need:
Command: jq '.[2:4]'
Input: "abcdefghi"
Output: "cd"
I've tried to test this out with a simple example in the unix terminal:
# this works fine, => "abcdefghi"
echo '"abcdefghi"' | jq '.'
# this doesn't work => jq: error: Cannot index string with object
echo '"abcdefghi"' | jq '.[2:4]'
So, it turns out most of these filters are not yet in the released version. For reference see issue #289
What you could do is download the latest development version and compile from source. See download page > From source on Linux
After that, if indexing still doesn't work for strings, you should, at least, be able to do explode, index, implode combination, which seems to have been your plan.
Looking at the jq-1.3 manual I suspect there isn't a solution using that version since it offers no primitives for extacting parts of a string.

Resources