Echo a grep result - linux

I have a file with
dogs:${breed}
I want to grep and get ${dog} and get the value, I have exported values as
export breed='german'
What i have Done is
eval "$(grep -iw dogs file_name | awk 'BEGIN {FS=":"} {print $2}')"
which actually results in error as it is taking german as command.
i want the output as follows.
german

You can use envsubst to replace shell style variables in literal text:
echo 'dogs:${breed}' > file_name
export breed=german
grep -iw dogs file_name | awk 'BEGIN {FS=":"} {print $2}' | envsubst
This outputs german

Try changing
eval "$(grep -iw dogs file_name | awk 'BEGIN {FS=":"} {print $2}')"
to
eval "echo $(grep -iw dogs file_name | awk 'BEGIN {FS=":"} {print $2}')"

Related

Strip a part of string in linux

Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 is my string and the result I want is vm-1.0.3
What is the best way to do this
Below is what I tried
$ echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 | awk -F _ {'print $2'} | awk -F - {'print $1,$2'}
vm 1.0.3
I also tried
$ echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 | awk -F _ {'print $2'} | awk -F - {'print $1"-",$2'}
vm- 1.0.3
Here I do not need space in between
I tried using cut and I got the expected result
$ echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 | awk -F _ {'print $2'} | cut -c 1-8
vm-1.0.3
What is the best way to do the same?
Making assumptions from the 1 example you provided about what the general form of your input will be so it can handle that robustly, using any sed:
$ echo 'Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2' |
sed 's/^[^-]*-[^-]*-[^_]*_\(.*\)-[^-]*$/\1/'
vm-1.0.3
or any awk:
$ echo 'Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2' |
awk 'sub(/^[^-]+-[^-]+-[^_]+_/,"") && sub(/-[^-]+$/,"")'
vm-1.0.3
You don't need 2 calls to awk, but your syntax with the single quotes outside the curly's, including printing the hyphen:
echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 |
awk -F_ '{print $2}' | awk -F- '{print $1 "-" $2}'
If your string has the same format, let the field separator be either - or _
echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 | awk -F"[-_]" '{print $4 "-" $5}'
Or split the second field on - and print the first 2 parts
echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 | awk -F_ '{
split($2,a,"-")
print a[1] "-" a[2]
}'
Or with gnu-awk a bit more specific match with a capture group:
echo Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2 |
awk 'match($0, /^Apps-[^_]*_(vm-[0-9]+\.[0-9]+\.[0-9]+)/, a) {print a[1]}'
Output
vm-1.0.3
This is the easiest I can think of:
echo "Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2" | cut -c 25-32
Obviously you need to be sure about the location of your characters. In top of that, you seem to be have two separators: '_' and '-', while both characters also are part of the name of your entry.
echo 'Apps-10.00.00R000-B1111_vm-1.0.3-x86_64.qcow2' | sed -E 's/^.*_vm-([0-9]+).([0-9]+).([0-9]+)-.*/vm-\1.\2.\3/'

Using awk to split string with \n followed by ' and ,

If given the string '1234␤',56789, how can I use awk to split by the sequence ␤',? Here ␤ represents a literal newline character.
Right now I have,
echo $LINE | awk -F'\\\\n',' '{ print $1}'
The split doesn't happen with this. Any advice?
Try to print all fields using the value of -F
echo "1234\n',56789," | awk -F "[',]+" -v ORS="" '{$1=$1}1'
line="1234\n',56789,"; echo "$line" | awk -F "[',]+" -v ORS="" '{$1=$1; print $0}'
Output
1234\n 56789
To print a specific field
echo "1234\n',56789," | awk -F "[',]+" -v ORS="" '{$1=$1; print $1}'
line="1234\n',56789,"; echo "$line" | awk -F "[',]+" -v ORS="" '{$1=$1; print $1}'
Output
1234\n

Awk: parse node names out of "40*r13n15:40*r10n61:40*r11n18:40*r09n15"

I have a linux script for selecting the node.
For example:
4
40*r13n15:40*r10n61:40*r11n18:40*r09n15
The correct result should be:
r13n15
r10n61
r11n18
r09n15
My linux script content is like:
hostNum=`bjobs -X -o "nexec_host" $1 | grep -v NEXEC`
hostSer=`bjobs -X -o "exec_host" $1 | grep -v EXEC`
echo $hostNum
echo $hostSer
for i in `seq 1 $hostNum`
do
echo $hostSer | awk -F ':' '{print '$i'}' | awk -F '*' '{print $2}'
done
But unlucky, I got nothing about node information.
I have tried:
echo $hostSer | awk -F ':' '{print "'$i'"}' | awk -F '*' '{print $2}'
and
echo $hostSer | awk -F ':' '{print '"$i"'}' | awk -F '*' '{print $2}'
But there are wrong. Who can give me a help?
One more awk:
$ echo "$variable" | awk 'NR%2==0' RS='[*:\n]'
r13n15
r10n61
r11n18
r09n15
By setting the record separtor(RS) to *:\n , the string is broken into individual tokens, after which you can just print every 2nd line(NR%2==0).
You can use multiple separators in awk. Please try below:
h='40*r13n15:40*r10n61:40*r11n18:40*r09n15'
echo "$h"| awk -F '[:*]' '{ for (i=2;i<=NF;i+=2) print $i }'
**edited to make it generic based on the comment from RavinderSingh13.

How to extract words between two characters in linux?

I have the following stored in a file named tmp.txt
user/config/jars/content-config-factory-3.2.0.0.jar
I need to store this word to a variable -
$variable=content-config-factory
I have written the following
while read line
do
var=$(echo $line | awk 'BEGIN{FS="\/"; OFS=" "} {print $NF}' )
var=$(echo $var | awk 'BEGIN{FS="-"; OFS=" "} {print $(1)}' )
echo $var
done < tmp.txt
This returns the result "content" instead of "content-config-factory".
Can anyone please tell me how to extract a word between two characters from a string efficiently.
An awk solution would be like
awk -F/ '{sub("-[^-]+$", "", $NF); print $NF}
Test
$ echo "user/config/jars/content-config-factory-3.2.0.0.jar" | awk -F/ '{sub("-[^-]+$", "", $NF); print $NF}'
content-config-factory
You can try this way also and get your expected result
variable=$(sed 's:.*/\(.*\)-.*:\1:' FileName)
echo $variable
OutPut :
content-config-factory
You could use grep,
grep -oP '(?<=/)[^/]*(?=-\d+\.)' file
Example:
$ var=$(echo 'user/config/jars/content-config-factory-3.2.0.0.jar' | grep -oP '(?<=/)[^/]*(?=-\d+\.)')
$ echo "$var"
content-config-factory

can not use unix $variable in Fixed search of awk command

I can not use unix $variable in Fiexd search of awk command.
Please see below my commands.
a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar=$b -F'$0~myvar' '{print $2}'
is not returning any output
but if manually enter the $b value there , its working as below
echo $a | awk -v -F'NEW' '{print $2}'
outputs:
TABLES NEW_INSERT
This should make it:
$ a="NEW_TABLES NEW_INSERT"
$ echo $a | awk -F"NEW_" '{print $2}'
TABLES
$ b="NEW_"
$ echo $a | awk -F"$b" '{print $2}'
TABLES
Your quotings are all messed up and you can use your variable to split the line using split function:
a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar="$b" '{split($0,ary,myvar);print ary[2]}'
Outputs:
_TABLES

Resources