Alternative energy in Bash

Asked

Viewed 89 times

2

I am trying to get a part of a line in bash text files but instead of the text'field1 'I wanted to use a variable. But it is not working. Suggestions

WORKS:

NomeVar=$(cut -d ":" -f 1 <<< "$(awk /campo1/ { print substr($0,48,10) } /home/ficheiro.txt)")

DOESN’T WORK:

fld1=campo1

NomeVar=$(cut -d ":" -f 1 <<< "$(awk /$fld1/ { print substr($0,48,10) } /home/ficheiro.txt)")

2 answers

2


Do so:

cut -d ":" -f 1 <<< "$(awk "/$fld1/ { print substr(\$0,48,10) }" /home/ficheiro.txt)"

Another possibility is to use pipe, like this:

awk "/$fld1/ { print substr(\$0,48,10) }" /home/ficheiro.txt | cut -d ":" -f 1 

More about AWK in this post: http://rberaldo.com.br/tutorial-awk/

0

perl -nlE "/$fld1/ and say m/.{48}([^:]*)/"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.