Substring in Bash

2

I'm trying to get a portion of a line in a text files in bash 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)")

IT DOES NOT WORK:

fld1=campo1

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

2 answers

2

Do this:

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

Another possibility is to use a pipe, like this:

%pre%

More about AWK in this post: link

    
13.01.2015 / 13:51
0
perl -nlE "/$fld1/ and say m/.{48}([^:]*)/"
    
31.03.2015 / 17:45