awk save command ouput to variable - linux

I need to execute a command per line of some file. For example:
file1.txt 100 4
file2.txt 19 8
So my awk script need to execute something like
command $1 $2 $3
and save the output of command $1 $2 $3, so system() will not work and neither will getline. (I can't pipe the output if I do something like this.)
The restriction to this problem is to use only awk. (i already had a solution with bashscriot + awk...but I only want awk...just to know more about this)

What's wrong with using getline?
$ ./test.awk test.txt
# ls -F | grep test
test.awk*
test.txt
# cat test.txt | nl
1 ls -F | grep test
2 cat test.txt | nl
3 cat test.awk
# cat test.awk
#!/usr/bin/awk -f
{
cmd[NR] = $0
while ($0 | getline line) output[NR] = output[NR] line RS
}
END {
for (i in cmd) print "# " cmd[i] ORS output[i]
}

Awk's system() function passes the string to /bin/sh, so you can use redirect operators, like ">file.out" if you want.
awk '{system("command " $1 " " $2 " " $3 ">" $1 ".out");}'
Edit: ok, by save, you mean into an awk variable. ephemient is on the right track, then. That's what awk's getline does, like backticks or $(cmd) in shell/perl. In fact, google for awk backticks found this:
http://www.omnigroup.com/mailman/archive/macosx-admin/2006-May/054665.html
You say you can't use getline because then you couldn't pipe. But you can work around that with tee and file-descriptor tricks. This works if /bin/sh is bash:
{ "set +o posix; command " $1 " " $2 " " $3 " | tee >(grep foo)" | getline var; print toupper(var); } # bash-only, and broken.
set +o posix is necessary because awk runs bash as sh, which makes it go into posix mode after readings its startup files. Hmm, I'm not having any luck getting that to work, and it requires bash anyway.
Ok, this works:
$ touch foo bar
$ echo "foo bar" |
awk '{ "{ ls " $1 " " $2 " " $3 " | tee /dev/fd/10 | grep foo > /dev/tty; } 10>&1" | getline var; print toupper(var); }'
foo
BAR

Related

Command "echo" has no effect in awk

I have a line of code that I need to run in a linux terminal and it`s not going very well.
What i`m doing is trying to output some variables obtained from my postfix mail queue to a file. For now I just need this piece of code working, but when I try to execute, nothing happens.
Code:
mailq | tail -n +2 | awk 'BEGIN { RS = "" } { echo $1 }' | tr -d '*!' >> myfile
Additional Notes:
If I change echo to print and remove >> myfile it works, but I need to output it to file.
awk doesn't have an echo command; it does have a print command. Making the replacement should be sufficient, without removing the >> myfile.
Tangentially, you can do away with the tail command by telling awk to ignore its first two lines of input and exiting immediately after the third.
mailq | awk ' NR == 3 { print $1; exit }' | tr -d '*!' >> myfile

unix concatenate list of files into on line

In a directory, there is several files such as:
file1
file2
file3
Is there a simple way to concatenate those files to get one line (connected by "OR") in bash as follows:
file1 OR file2 OR file3
Or do I need to write a script for it?
You can use this function to print all filenames (including ones with space, newline or special characters) with " OR " as separator (assuming your filename doesn't contain ASCII code 4):
orfiles() {
local IFS=$'\4'
local out="$*"
echo "${out//$'\4'/ OR }"
}
Then call it as:
orfiles *
How it works:
We set IFS (Internal Field Separator) to ASCII 4 locally inside the function
We store output of "$*" in local variable out. This will place \4 after each filename in variable $out.
Finally using BASH string substitution we globally replace \4 by " OR " while printing the output from $out.
In Unix systems IFS is only a single character delimiter therefore it cannot store multi character string " OR " and we have to do this in 2 steps as shown above.
You can simply do that with
printf '%s OR ' $(ls -1 *) | sed 's/OR $/''/'; echo -e '\n'
Where ls -1 * is the directory.
The moment that should be considered is that a filename could contain whitespace(s).
Use the following ls + awk solution:
ls -1 * | awk '{ r=(r)? r" OR "$0 : $0 }END{ print r }'
Workaround for filenames with newline(s):
echo -e $(ls -1b hello* | awk -v RS= '{gsub(/\n/," OR ",$0); gsub(/\\ /," ",$0); print $0}')
-b - ls option to print C-style escapes for nongraphic characters
ls -1|awk -v q='"' '{printf "%s%s", NR==1?"":" OR ", q $0 q}END{print ""}'
the ls & awk way to do it, with example that the filename containing spaces:
kent$ ls -1
file1
file2
'file with OR and space'
kent$ ls -1|awk -v q='"' '{printf "%s%s", NR==1?"":" OR ", q $0 q}END{print ""}'
"file1" OR "file2" OR "file with OR and space"
$ for f in *; do printf '%s%s' "$s" "$f"; s=" OR "; done; printf '\n'
file1 OR file2 OR file3

error bash extracting second column of a matched pattern

I am trying to search for a pattern and from the results i am extracting just the second column. The command works well in command line but not inside a bash script.
#!/bin/bash
set a = grep 'NM_033356' test.txt | awk '{ print $2 }'
echo $a
It doesnt print any output at all.
Input
NM_033356 2
NM_033356 5
NM_033356 7
Your code:
#!/bin/bash
set a = grep 'NM_033356' test.txt | awk '{ print $2 }'
echo $a
Change it to:
#!/bin/bash
a="$(awk '$1=="NM_033356"{ print $2 }' test.txt)"
echo "$a"
Code changes are based on your sample input.
.......
a="$(awk '/NM_033356/ { print $2 }' test.txt)"
Try this:
a=`grep 'NM_033356' test.txt | awk '{ print $2 }'`

Linux awk string exec variable

i have a variable that when i echo it looks like this:
#echo $var
awk '{print $7 " " $6 " " $8 " "}'
but if I try something like
#ls -lah | exec $var
awk: '{print
awk: ^ invalid char ''' in expression
What am I doing wrong?
Instead of exec you need to call eval:
ls -lah | eval $var
However 2 cautions here:
Parsing ls output should be avoided
Use of eval should be minimised
Just put the awk script in a variable
var='{print $7 " " $6 " " $8 " "}'
ls -lah | awk "$var"

Using awk in shellscript with parameters?

because i haven't found a solution on google or the searchfunction i will ask here.
Here is my code :
Send="last -n 1 $1 | awk '{ print $1 " " $2 }'"
My problem is, that my shell script is using parameters.
When i'm calling my script:
myScript hello world
Then my awk-Command looks like
awk '{ print hello " " world }'
But how could I avoid this? is there a way?
Because this is a part of a project, i couldn't post more code. ;/
first change the outer "'s to $() so: send=$(last -n 1 $1 | awk '{print $1 " " $2}')
use the FS (field separator) variable which defaults to space in awk instead of " " for a space so: send=$(last -n 1 $1 | awk '{print $1 FS $2}')

Resources