Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk print is not working inside bash shell script

Tags:

bash

shell

unix

awk

When I use AWK print command outside shell it is working perfectly. Below is content of the file (sample.txt) which is comma separated.

IROG,1245,OUTO,OTUG,USUK

After, executing below command outside shell I get IROG as output.

cat sample.txt | awk -F, '{print $1}' > data.txt

Below is inside the shell script

my $HOME        ='home/tmp/stephen';    
my $DATA        ="$HOME/data.txt";    
my $SAMPLE     ="$HOME/sample.txt";    
`cat $SAMPLE | awk -F, '{print $1}' > $DATA`;

But here i get the same content as in original file instead of 1st column.

output is IROG,1245,OUTO,OTUG,USUK

but I expect only IROG. Can someone advise where I am wrong here?

like image 788
stephenjacob Avatar asked Dec 11 '22 01:12

stephenjacob


1 Answers

The $1 inside your backticks expression is being expanded by perl before being executed by the shell. Presumably it has no value, so your awk command is simply {print }, which prints the whole record. You should escape the $ to prevent this from happening:

`awk -F, '{print \$1}' "$SAMPLE" > "$DATA"`;

Note that I have quoted your variables and also removed your useless use of cat.

If you mean to use a shell script, as opposed to a perl one (which is what you've currently got), you can do this:

home=home/tmp/stephen
data="$home/data.txt"
sample="$home/sample.txt"
awk -F, '{print $1}' "$sample" > "$data"

In the shell, there must be no spaces in variable assignments. Also, it is considered bad practice to use UPPERCASE variable names, as you risk overwriting the ones used internally by the shell. Furthermore, it is considered good practice to use double quotes around variable expansions to prevent problems related to word splitting and glob expansion.

There are a few ways that you could trim the leading whitespace from your first field. One would be to use sub to remove it:

awk -F, '{sub(/^ */, ""); print $1}'

This removes any space characters from the start of the line. Again, remember to escape the $ if doing this within backticks in perl.

like image 168
Tom Fenech Avatar answered Jan 03 '23 10:01

Tom Fenech