technopathe technopathe - 1 year ago 60
Bash Question

concatination of cut putting variable inside it

I have coded the following code :


for (( j=1; j<=3; j++ ))

First_Pos_resource=$(awk -F ';' 'NR=='$j'{print $3}' "${Fileformat}")
Length_resource=$(awk -F ';' 'NR=='$j'{print $4}' "${Fileformat}")
OUtput_line=${OUtput_line}$(cut -c${range} "${Fileresource}")

echo $OUtput_line >> OutputFile.txt

Fileformat : is the structure that the outputFile.txt should respect.
Fileformat.txt defines the:
Name;Position In Output;Position In resourcefile ; lenght of string to cut from the ressource file

Fileformat.txt looks like this :


Fileresource :it contains a line of strings,from which we generate the output file

it looks like this :

sfdssdffgdfglkjdfsld lkjlkkfdssdfssdf

So to sum up : File resource + File Format = OutputFile.txt


first;1;15;2 + sfdssdffgdfglkjdfsld lkjlkkfdssdfssdf = jd
second;3;6;4 + sfdssdffgdfglkjdfsld lkjlkkfdssdfssdf = ffgd
blabla;7;23;5 + sfdssdffgdfglkjdfsld lkjlkkfdssdfssdf = dfssd

so after concatenation I should have this result in the output file : Output.txt looks like this


I have coded the above lines but I have a problem with the white-space generated after concatenation

output displayed but not desired:

jd ffgd dfssd

Answer Source

The error message looks like your variables are not being correctly set. Without access to your precise input files, it's hard to debug this; but copy/pasting your data produces range values like this for me:


which are obviously incorrect. If you wanted the shell to perform arithmetic on the value, you need to use (( arithmetic expressions )) for that.

Anyway, your loop looks like it's completely inside out. Also, refactoring the entire script into a single Awk script will make it both more efficient and simpler.

awk -F ';' 'NR==FNR { res = res $0; next }
  { printf "%s", substr(res, $3, $4) }
  END { print "" }' resource fileformat.txt

I don't get exactly the output you prescribe but I may have some copy/paste error in the data, or misunderstand how exactly you want to calculate the substring offsets.

(I couldn't help but correct the spelling of "resource".)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download