1

I want to do an if statement that compares the values of a command that reads header information of two image files. First I pass output of the command to the variables

~$ hd1=$(<command> <file1> | grep dim3)
~$ hd2=$(<command> <file2> | grep dim3)
~$ if [ $hd1 = $hd2 ];
    ~$ then
       etc

The output of my command has alot of fields

~$ <command> <file>
~$ sizeof_hdr     348
   data_type      INT16
   dim0           3
   dim1           256
   dim2           256
   dim3           70
   dim4           1
   dim5           1
   dim6           1
   dim7           1
   vox_units      mm
   time_units     s
   datatype       4
   nbyper         2
   bitpix         16
   pixdim0        0.000000
   pixdim1        0.828125
   pixdim2        0.828125
   pixdim3        2.199998
   pixdim4        4.177372
   pixdim5        0.000000
   pixdim6        0.000000
   pixdim7        0.000000
   vox_offset     352
   file_type      NIFTI-1+

So I use grep to get the one that I care about here. The problem is the output of my command includes a "dim3" field and a "pixdim3" field, and using grep prints both, like this:

~$ dim3 70 pixdim3 2.19

I really just need compare the second column for both files. I tried using awk, but it wouldn't work because they are variables and not files. Is there anyway to print just the second column, or better yet only grep the dim3 field?

  • Please add an example of the output of <command> file. – pLumo Dec 7 '18 at 14:03
  • 1
    Have you considered using grep -w (or suitable word boundary anchors) to limit the match to only the whole word dim3? – steeldriver Dec 7 '18 at 14:36
1

try replacing grep by

... | awk '$1 == "dim3" { print $2;}' 

this will filter out result on first field being pim3, and will only print second field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.