3

I had read the post on: bash - replace space with new line, but it does not helped to solve my issue.

   [My Directory]

[root@testlabs Config]# ls 
Archive Backup_Files
config_file.tgz
hostname1-config.uac
hostname2-config.uac
hostname3-config.uac
My-config_17Oct2014.tgz
non_extension-config_file1
non_extension-config_file2
[root@testlabs Config]#

I need to echo a list of MD5 Checksum results from a file. I am able to do so by doing this:

##IDENTIFY MD5 CHECKSUM##

 //To output the md5sum of the original file with a correct format to a temp file [md5<space><space>file_name]
ls $FULL_FILE_NAME | md5sum  $FULL_FILE_NAME > /tmp/md5sum.tmp

//To compare the md5sum of the orignal file with the md5sum of the backup copies in the archive directory
ls $CONFIG_ARCHIVE_DIR | grep -i --text $CONFIG_FILE_HOSTNAME-$FILE_TIMESTAMP.tgz | md5sum -c /tmp/md5sum.tmp >> /tmp/md5sum2.tmp

##COMPARING MD5 CHECKSUM##

 if [ -s /tmp/md5sum2.tmp ];
 then
   echo ""
   echo "Comparison of MD5 for files archived:"
   echo '---------------------------------------'
   /bin/sort -d /tmp/md5sum2.tmp
 fi

and this will be the result when it was executed: (echo of the CONTENTS in /tmp/md5sum2.tmp)

Comparison of MD5 for files archived:
---------------------------------------
 config_file.tgz: OK
 hostname1-config.uac: OK
 hostname2-config.uac: OK
 hostname3-config.uac: OK
 My-config_17Oct2014.tgz: OK
 non_extension-config_file1: OK
 non_extension-config_file1: OK

## WANTED ##

However I would like the result to be display in this way:

Comparison of MD5 for files archived:
---------------------------------------
 - config_file.tgz: OK
 - hostname1-config.uac: OK
 - hostname2-config.uac: OK
 - hostname3-config.uac: OK
 - My-config_17Oct2014.tgz: OK
 - non_extension-config_file1: OK
 - non_extension-config_file2: OK


I tried doing this, (echo the CONTENTS of /tmp/md5sum2.tmp into /tmp/md5sum3.tmp with the '-' infront)

1)

 ##COMPARING MD5 CHECKSUM##

  if [ -s /tmp/md5sum2.tmp ];
  then
  echo ""
  echo "Comparison of MD5 for files archived:"
  echo '---------------------------------------'
  /bin/sort -d /tmp/md5sum2.tmp

  for CONFIG_FILES in `/bin/cat /tmp/md5sum2.tmp`
  do
/bin/sort -d /tmp/md5sum2.tmp | grep $CONFIG_FILES > /tmp/md5sum3.tmp
  done

  for MD5_COMPARE in $(/bin/sort -d /tmp/md5sum3.tmp)
  do
   echo -e " - $MD5_COMPARE\n"
  done
 fi

Result 1)

Comparison of MD5 for files archived:
 ---------------------------------------
 - config_file.tgz:
 - OK
 - hostname1-config.uac:
 - OK
 - hostname2-config.uac:
 - OK
 - hostname3-config.uac:
 - OK
 - My-config_17Oct2014.tgz.tgz:
 - OK
 - non_extension-config_file1:
 - OK
 - non_extension-config_file2:
 - OK

2)

for MD5_COMPARE in $(/bin/sort -d /tmp/md5sum3.tmp)

  do
  echo -n " - $MD5_COMPARE"
  done

Result 2)

  Comparison of MD5 for files archived:
  ---------------------------------------
- config_file.tgz: - OK - hostname1-config.uac: - OK - hostname2-config.uac: - OK - 
  hostname3-config.uac: - OK - My-config_17Oct2014.tgz: - OK - non_extension-config_file1: -
OK - non_extension-config_file2: - OK
6
  • Hi and welcome to the site. When asking this type of question, it is essential that you also show us your input. Please edit your question and include the contents of /tmp/md5sum3.tmp.
    – terdon
    Nov 14, 2014 at 11:41
  • Hi Terdon, thanks! :) I had edited the question.
    – Win.T
    Nov 17, 2014 at 1:12
  • I think we may have just gotten our edits crossed. I was editing at the same time you were, please make sure I did not remove anything important. I was just removing the extra whitespace.
    – terdon
    Nov 17, 2014 at 1:19
  • Also, please edit and show us your input file. We don't know what $FULL_FILE_NAME or $CONFIG_ARCHIVE_DIR are. We also don't need to. We do need to know what the files you are comparing look like.
    – terdon
    Nov 17, 2014 at 1:21
  • Yes, I am adding some comments. I think Costas's solution worked out for me already :)
    – Win.T
    Nov 17, 2014 at 1:32

2 Answers 2

5

To read file line by line standard procedure is

while IFS= read -r MD5_COMPARE
do
  echo "- $MD5_COMPARE"
done < /tmp/md5sum2.tmp | /bin/sort -d

But sed should work too

/bin/sort -d /tmp/md5sum2.tmp | sed 's/^/ -/'
2
  • This is definitely a perfect job for sed.
    – PM 2Ring
    Nov 14, 2014 at 13:47
  • @Costas Thank you Costas /bin/sort -d /tmp/md5sum2.tmp | sed 's/^/ -/' worked for me. :)
    – Win.T
    Nov 17, 2014 at 1:52
1

Just pipe the output from sort to sed and replace one space with -:

if [ -s /tmp/md5sum2.tmp ];
then
        echo ""
        echo "Comparison of MD5 for files archived:"
        echo '---------------------------------------'
        /bin/sort -d /tmp/md5sum2.tmp | sed 's/\(^ *\) \( [^ ]\)/\1-\2/'
fi

Result:

Comparison of MD5 for files archived:
---------------------------------------
    - My-config_17Oct2014.tgz: OK
    - config_file.tgz: OK
    - hostname1-config.uac: OK
    - hostname2-config.uac: OK
    - hostname3-config.uac: OK
    - non_extension-config_file1: OK
    - non_extension-config_file1: OK
4
  • @Win.T my answer assumes that your initial file has spaces at the beginning of the line, however now I'm not sure if this is correct or your question is just wrongly formatted.
    – jimmij
    Nov 14, 2014 at 9:15
  • The solution for /bin/sort -d /tmp/md5sum2.tmp | sed 's/(^ *) ( [^ ])/\1-\2/' does not worked. It returns the same result as using /bin/sort -d /tmp/md5sum2.tmp. The file /tmp/md5sum2.tmp contains the following: config_file.tgz: OK My-config_17Oct2014.tgz: OK hostname1-config.uac: OK hostname2-config.uac: OK hostname3-config.uac: OK non_extension-config_file2: OK non_extension-config_file1: OK @jimij
    – Win.T
    Nov 14, 2014 at 9:54
  • 1
    @Win.T edit you question, add content of original file properly formatted.
    – jimmij
    Nov 14, 2014 at 10:08
  • @jimij , thank you I had edited my question :)
    – Win.T
    Nov 17, 2014 at 1:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .