How to pass shell variable to awk command inside shell script - linux

I just want to pass a shell variable that stores name of a file to awk command. When I searched this problem on the net I see many different options but none of them worked for me. I tried the followings:
#!/bin/bash
for i in "$#"
do
case $i in
-p=*|--producedfile=*)
PFILE="${i#*=}"
shift # past argument=value
*)
# unknown option
;;
esac
done
echo "PRODUCEDFILE = ${PFILE}"
awk -v FILE=${PFILE} '{print FILE $0}' #DIDNT WORK
awk '{print FILE $0}' ${PFILE} # DIDNT WORK
awk -v FILE=${PFILE} '{print $0}' FILE #DIDNT WORK

To pass a shell variable to awk, you correctly used -v option.
However, the shift was unnecessary (you're iterating options with for), ;; was missing (you have to terminate each case branch), as well as was the name of the file for awk to process. Fixed, your script looks like:
#!/bin/bash
for i in "$#"; do
case $i in
-p=*|--producedfile=*)
PFILE="${i#*=}"
;;
*)
# unknown option
;;
esac
done
echo "PRODUCEDFILE = ${PFILE}"
awk -v FILE="${PFILE}" '{print FILE, $0}' "${PFILE}"
Note however, awk already makes the name of the currently processed file available in the FILENAME variable. So, you could also write the last line as:
awk '{print FILENAME, $0}' "${PFILE}"

Related

Awk doesn`t work when it run at other path

./oppa/loadParesr/spliter.sh [filename]
spliter.sh
#!/bin/sh
sort -k 1V,1 -k 2n,2 $1 -o $1
awk `{close(f);f=$1}{print > f".bed"}`
i try to run this shell at other path. sort command work well but
awk is doesn't work and i don't know why.
Close the file you're writing to, not just part of it's name, and always quote your shell variables, for example:
awk '{close(f); f=$1".bed"; print > f}' "$1"

Can I use a variable as parameter to AWK's {print}?

I have this bash statement for printing a specific cell from a .csv file.
set `cat $filename | awk -v FS=',' '{print $2}' | head -5 | tail -n 1`
The '{print $2}' part determines the column and the head -5 part determines the row.
Can I substitute a $counter variable in place of $2 (e.g., '{print $counter}')?
The answer is "yes" -- and there are a couple ways to do what you want. The proper way is to declare an awk variable using -v:
awk -F',' -v c=$counter 'NR==6 { print $c; exit }' "$filename"
(You will forgive me for moving some things around to do everything in awk, for passing "$filename" to awk safely, and for getting rid of set and back ticks -- that were doing nothing for the cause.)
Another way to do this is a bit of a "hackish" way -- leveraging shell quoting rules. This method requires some escaping to ensure that the first $ character (that references the intended field in awk) is not interpreted by the shell... The following works in bash (and POSIX sh):
awk -F',' "NR==6 { print \$$counter; exit }" "$filename"
Yes and all pipes could be removed. Variables are passed to awk with -v var=value.
Give a try to this tested version. Provide a value to the ̀€col and row variables:
set $(awk -F "," -v col=2 -v row=5 'NR==row {print $col; exit}' "${filename}")
$(command) is prefered to `command`, this later is deprecated.
NR is the current line number.
"${filename}" is expanded by the shell to its value: the double quotes will help if the filename contains some special chars.

Print name of the file in front of every line of file

I have a lot of text files and I want to make a bash script in linux to print the name of file in each lines of file. For example I have file lenovo.txt and I want that every line in the file to start with lenovo.txt.
I try to make a "for" for this but didn't work.
for i in *.txt
do
awk '{print '$i' $0}' /var/SambaShare/$i > /var/SambaShare/new_$i
done
Thanks!
It doesn't work because you need to pass $i to awk with the -v option. But you can also use the FILENAME built-in variable in awk :
ls *txt
file.txt file2.txt
cat *txt
A
B
C
A2
B2
C2
for i in *txt; do
awk '{print FILENAME,$0}' $i;
done
file.txt A
file.txt B
file.txt C
file2.txt A2
file2.txt B2
file2.txt C2
An to redirect into a new file :
for i in *txt; do
awk '{print FILENAME,$0}' $i > ${i%.txt}_new.txt;
done
As for your corrected version :
for i in *.txt
do
awk -v i=$i '{print i,$0}' $i > new_$i
done
Hope this helps.
Using grep you can make use of the --with-filename (alias -H) option and use an empty pattern that always matches:
for i in *.txt
do
grep -H "" $i > new_$i
done
Awk and Bash don't share the same variables as they are different languages with separate interpreters. You should pass Bash variables to Awk with the -v option.
You should also quote your file name variables to ensure they don't get expanded as separate arguments if they contain whitespace.
for i in *.txt
do
awk -v i="$i" '{print i,$0}' "$i" > "$i"
done

Pass parameter to an awk script file

If I want to pass a parameter to an awk script file, how can I do that ?
#!/usr/bin/awk -f
{print $1}
Here I want to print the first argument passed to the script from the shell, like:
bash-prompt> echo "test" | ./myawkscript.awk hello
bash-prompt> hello
In awk $1 references the first field in a record not the first argument like it does in bash. You need to use ARGV for this, check out here for the offical word.
Script:
#!/bin/awk -f
BEGIN{
print "AWK Script"
print ARGV[1]
}
Demo:
$ ./script.awk "Passed in using ARGV"
AWK Script
Passed in using ARGV
You can use -v as a command-line option to provide a variable to the script:
Say we have a file script.awk like this:
BEGIN {print "I got the var:", my_var}
Then we run it like this:
$ awk -v my_var="hello this is me" -f script.awk
I got the var: hello this is me
your hash bang defines the script is not shell script, it is an awk script. you cannot do it in bash way within your script.
also, what you did : echo blah|awk ... is not passing paramenter, it pipes the output of echo command to another command.
you could try these way below:
echo "hello"|./foo.awk file -
or
var="hello"
awk -v a="$var" -f foo.awk file
with this, you have var a in your foo.awk, you could use it.
if you want to do something like shell script accept $1 $2 vars, you can write a small shellscript to wrap your awk stuff.
EDIT
No I didn't misunderstand you.
let's take the example:
let's say, your x.awk has:
{print $1}
if you do :
echo "foo" | x.awk file
it is same as:
echo "foo"| awk '{print $1}' file
here the input for awk is only file, your echo foo doesn't make sense. if you do:
echo "foo"|awk '{print $1}' file -
or
echo "foo"|awk '{print $1}' - file
awk takes two input (arguments for awk) one is stdin one is the file, in your awk script you could:
echo "foo"|awk 'NR==FNR{print $1;next}{print $1}' - file
this will print first foo from your echo, then the column1 from file of course this example does nothing actual work, just print them all.
you can of course have more than two inputs, and don't check the NR and FNR, you could use the
ARGC The number of elements in the ARGV array.
ARGV An array of command line arguments, excluding options and the program argument, numbered from zero to ARGC-1
for example :
echo "foo"|./x.awk file1 - file2
then your "foo" is the 2nd arg, you can get it in your x.awk by ARGV[2]
echo "foo" |x.awk file1 file2 file2 -
now it is ARGV[4] case.
I mean, your echo "foo"|.. would be stdin for awk, it could by 1st or nth "argument"/input for awk. depends on where you put the -(stdin). You have to handle it in your awk script.

How to get extension of a file in shell script

I am trying to get file extension for a file in shell script. But without any luck.
The command I am using is
file_ext=${filename##*.}
and
file_ext = $filename |awk -F . '{if (NF>1) {print $NF}}'
But both of the commands failed to put value in variable file_ext. But when i try
echo $filename |awk -F . '{if (NF>1) {print $NF}}'
It gives me the desired result. I am new to shell script. Please describe the situation what is happening. And also how should I do it?
Thanks.
to get file extension, just use the shell
$ filename="myfile.ext"
$ echo ${filename##*.}
ext
$ file_ext=${filename##*.} #put to variable
$ echo ${file_ext}
ext
Spaces hurt.
Anyway you should do:
file_ext=$(echo $filename | awk -F . '{if (NF>1) {print $NF}}')
[Edit] Better suggestion by Martin:
file_ext=$(printf '%s' "$filename" | awk -F . '{if (NF>1) {print $NF}}')
That will store in $file_ext the output of the command.
You have to be careful when declaring variables.
variable1="string" # assign a string value
variable3=`command` # assign output from command
variable2=$(command) # assign output from command
Notice that you cannot put a space after the variable, because then it gets interpreted as a normal command.

Resources