This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
What is wrong with this script?
#!/bin/sh
for file in *.p; do awk -f get_p.awk "$file" > "$file"er; done
awk -f phase-comp.awk file1 file.per # výpočet fází
paste <(awk '{print $1}' output1) <(awk '{print $2}' file1) > out
The error is:
5: skript: Syntax error: "(" unexpected
When I write command separately to terminal. It works well. Thank you
The problem is your shebang:
#!/bin/sh
It means this script is meant to be interpreted by sh, but your script uses process substitution which is a bash-specific feature. So, you should change it to:
#!/bin/bash
to make your script work.
Related
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Got a bit of a problem. I'm new to bash and I'm trying my hardest, but I can't figure out how to assign the desired output to the variable. Running this command in the prompt
wc -l data.txt | awk '{ print $1 }'
yields the result 12, which is desired. However if I put it in the test.sh file, it won't work. I've tried different quotations, but all I've managed to get is the entire line as a string...
Test.sh
#! /bin/bash
# Count lines for data.txt files
data1=wc -l data.txt | awk '{ print $1 }'
echo "Lines in data.txt: $data1"
exit
I think you want:
data1="$(wc -l data.txt | awk '{ print $1 }')"
The $() syntax causes bash to execute that expression and replace it with the results.
Actually, powershell does allow you to do a straight = assignment like you did...
This question already has an answer here:
awk: fatal: cannot open file `' for reading (No such file or directory)
(1 answer)
Closed 2 years ago.
Using awk when I try to parse the last folder I get error "No such file or directory". How do I pass variable to awk
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName=$(awk -F/ '{print $NF}' $CacheLocation)
awk does not work on strings, it expects the input as file or stdin
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$(awk -F/ '{print $NF}' <<<"$CacheLocation" )"
In this special case, it might be easier to use basename
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$( basename "$CacheLocation" )"
This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 2 years ago.
I would like to use awk to replace any occurrence of a given word with another word in a .sh
The script I wrote is;
#!/bin/bash
read -p "Enter the word to change: " wrd1
read -p "Enter the new word: " wrd2
awk '{sub(/"$wrd1 /, $wrd2")}1' file.txt
but it's not working.
Use the -v option.
awk -v w1="$wrd1" -v w2="$wrd2" '{sub(w1, w2)}1' file.txt
A demonstration:
$ awk -v w1="$wrd1" -v w2="$wrd2" '{sub(w1, w2)}1' <<EOF
foo=3
hi=9
bye=2
EOF
bar=3
hi=9
bye=2
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.
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 8 years ago.
I'm trying to create a simple bash script to get the HTTP codes from CURL
So here is my code :
#!/bin/bash
AWKRESULT = $(curl -sL -w "result=%{http_code}" "http://192.168.8.69:8080/myReport/archive" -o "/tmp/reportlog" | awk -F= '{print $2}')
echo $AWKRESULT
the result of
curl -sL -w "result=%{http_code}" "http://192.168.8.69:8080/myReport/archive" -o "/tmp/reportlog" | awk -F= '{print $2}'
is 500.
However it's always has this result :
./test.sh[2]: AWKRESULT: not found.
any idea what am I missing?
Remove the spaces around the =:
AWKRESULT=$(...)