AWK how to put the output of a command to a variable - linux

I am trying to get the number of the line where the word "nel" comes as the variable "line" from the prueba.txt with help of the progtesis.awk command I am writing.
I am running this in the terminal:
awk -f progtesis.awk prueba.txt
And progtesis sees as follow:
line=$(awk -f '/nel/{print NR}' FILENAME}
echo "$line"
Any suggestions?

No need of an external awk script :
line=$(awk '/nel/ {print NR; exit}' "${filename}")
echo "${line}"
will display number of first line matching /nel/.
Otherwise if progtesis.awk contains
/nel/ {print NR; exit}
The bash commands can be
line=$(awk progtesis.awk "${filename}")
echo "${line}"

Related

linux|awk|shell script block deletion

My input file has blocks like below. Please help me deleting the block and its contents using awk or sed
[abc]
para1=123
para2=456
para3=111
[pqr]
para1=333
para2=765
para3=1345
[xyz]
para1=888
para2=236
para3=964
now how do i delete a block and its parameters completely .Please help me achieve this with awk command.Thanks in advance
You can use RS for split blocks, (NOTE: NR>1 because awk generate a empty block in beginning)
awk -vRS='[' -v remove="pqr" '
NR>1 && $0 !~ "^"remove"]" {printf "%s", "["$0; }
' file
you get,
[abc]
para1=123
para2=456
para3=111
[xyz]
para1=888
para2=236
para3=964
Depends on how you want to filter. If you want to delete the block with the header '[pqr]'
awk '!/^\[pqr\]/' RS= ORS='\n\n' input
or
awk '$1 !~ "[pqr]"' RS= ORS='\n\n' input
If you want to omit the 2nd record (the same as above)
awk 'NR!=2' RS= ORS='\n\n' input
If you want to omit the record in which para2=765,
awk '$3 !~ "765"' RS= ORS='\n\n' input
Perl solution to remove block [abc]
perl -lne 'BEGIN{$/=""} print "$_\n" unless /^\[abc\]/' file
-n loop around every line of the input file, put the line in the $_ variable, do not automatically print the line
-l removes newlines before processing, and adds them back in afterwards
-e execute the perl code
$/ is the input record separator. Setting it to "" in a BEGIN{} block puts Perl into paragraph mode.
$_ is the current line.
/^/ is a regular expression which begins with the search term
output:
[pqr]
para1=333
para2=765
para3=1345
[xyz]
para1=888
para2=236
para3=964
This variation enables argument parsing with -s and passes [abc] to variable $b
perl -slne 'BEGIN{$/=""} print "$_\n" unless /^$b/' -- -b='\[abc\]'
I propose a slightly different solution using only shell.
#!/bin/sh
# specify the block to withhold
WITHHOLD=2
COUNT=1
INAWHITESP=0
while read i
do if [ -z "$i" -a "$INAWHITESP" -eq 0 ]
then COUNT=$(( COUNT + 1 ))
INAWHITESP=1
fi
if [ -n "$i" -a "$INAWHITESP" -eq 1 ]
then INAWHITESP=0
fi
if [ "$COUNT" -ne "$WITHHOLD" ]
then printf "%s\n" "$i"
fi
done < inputfile > outputfile
To remove block abc
awk 'BEGIN{RS=""} !/\[abc\]/'

echo command displays to the screen even though loop directs to output file

I am trying to get the md5 of every individual line item and dump each md5 into a line on the next file (.md5). The below script echos everything to the screen. How do I redirect the echo output to the .md5 file.
more email/test |
while
read line;
md5=`md5sum $line | awk '{ print $1 }'`
do echo -n $md5;
done < .md5
Using bash
Try:
while IFS= read -r line;
do
md5sum $line | awk '{ print $1 }'
done <email/test >.md5
Using awk
The bash loop is unnecessary:
awk '{ "md5sum " $0 | getline; print $1}' email/test >.md5

How to Use Awk in a Bash Script [having problems]?

So I've been working on an assignment pertaining to awk. Everything is pretty much done, except when I throw commands into a bash script (run eleven of them all automagically) I don't get any output. Am I missing some sort of syntax that awk needs to work in a script? Thanks!
I'll just give you the first four to give you an idea:
#!/bin/sh
##VARIABLES
W=/usr/share/dict/words
C=cars_file
P=/etc/passwd
##PROBLEM EXAMPLE
#display problem number
#display command
#run command
#echo whitespace
##PROBLEMS
echo ">PROBLEM 01"
echo "awk '/zzan/ {print}' $W | head" #contains 'zzan'
awk '/zzan/ {print}' $W | head #pipe to head
echo
echo ">PROBLEM 02"
echo "awk '/^[aeiou].*[ou]rch$/ {print}' $W | head" #start w/lowercase vowel
echo awk '/^[aeiou].*[ou]rch$/ {print}' $W | head #end in orch/urch
echo #pipe to head
echo ">PROBLEM 03"
echo "awk '/chevy/ {print}' $C" #display all records
echo awk '/chevy/ {print}' $C #of all chevys
echo
echo ">PROBLEM 04"
echo "awk '$1 ~ /o/ {print}' $C" #display all records
echo awk '$1 ~ /o/ {print}' $C #contains 'o' in column 1
echo
`
The first awk entry should work fine (assuming your awk script does what you want. The other three have an erroneous leading echo and as such are just echoing the command instead of running it.

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 }'`

bash command error

i=0
while read line
do
echo "i is --- $i"
#echo $line "\n"
if (( $i > 0 ))
then
$Eda_package=$(echo $line | awk '{print $1}')
$well_bias=$(echo $line | awk '{print $2}')
$biasmap=$(echo $line | awk '{print $3}')
$unified=$(echo $line | awk '{print $4}')
echo "eda pack --$Eda_package wellbias is --$well_bias biasmap is --$biasmap unified- -- $unified"
fi
i=$((i+1))
done < config.list
In the above bash program I get an error:
./script.sh: line 9: =EDA_7p0: command not found
How do I fix this?
Lines of the form:
$xyzzy=plugh
will have xyzzy substituted before they're executed so that they look like:
=plugh
assuming they're not yet set. If they are set, you'll probably get different behaviour but still almost certainly not what you want.
You should change your lines from (for one example):
$Eda_package=$(echo $line | awk '{print $1}')
to:
Eda_package=$(echo $line | awk '{print $1}')
The $ is not part of the variable name, it's an indication that the following word is a variable that should be substituted.
Let var1=1 and var2=2 now if you simply write $var2=$var1 then it will give you error that 2=1 command not found
When you initialize any variable you have to do it without $ with variable name on left side

Resources