LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-20-2010, 02:26 PM   #1
peter1234
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 2
Bash, when I read sort write md5 file, the writen file have a space missing


Hi All,

Iam uesing the following bash scrip to filter all the files that have “-62” in their filename, (filter part of the script is ok). when I run script I get a file named “matched_1sp.md5” [(1) of script] which only have 1 space bitween md5sum (32 characters) and the relative path and file name. This is not a valid md5 file because there should be 2 spaces bitween md5sum (32 characters) and the relative path and file name.

I manage to “fix” the problem in a round about way [(2) of script], I want to know is there a better way
fix this problem.

my script:
Code:
#! /bin/bash
#
# Filter files by file name/partial file name. (0.60v)
#
# Read md5sum file "applist.md5" line by line to string "LINE_READ"
# and see if "LOOKFOR" (partial string match) and write all the matched 
# lines to an output file.

LOOKFOR="-62"  # what to look for is within ""

while read LINE_READ
 do 
     if [[ "$LINE_READ" =~ "$LOOKFOR" ]]
    then # matched
        
# (1)  I am writting content of string "LINE_READ" to a file, note 
#       there is only one space bitween md5 chsum (32 characters) and start
#       of relative path abd filename. There should be 2 spaces for it to 
#       be a proper MD5 sum file.
        echo $LINE_READ >> matched_1sp.md5 # write md5 + relative path + filename

# (2)      Since above (1) do not work I have to "cut" string LINE_READ to 2 strings,
#       CHKSUM=(first 32 characters of string LINE_READ).
#        PARTH_AND_FILE=(from 34 character/ possition till the end of the string).
        CHKSUM="$(echo $LINE_READ | cut --bytes=1-32)"    
        PARTH_AND_FILE="$(echo $LINE_READ | cut --bytes=34-)"
#       I combine the 2 strings after echo separated by 2 spaces, note the ""
        echo "$CHKSUM  $PARTH_AND_FILE" >> matched_2sp.md5 # write md5 + relative path + filename 
#                    ^       ^^               ^
#        else # did not match 
            
    fi
 done < applist.md5

My sample md5sum file: (applist.md5)
Code:
c32dc6f04ff02b371129dd4aa923c16b  ./packages/ap/streamripper-1.64.6-i486-62.1.txz
78997e57b09985132b91cf506bc8f2da  ./packages/ap/a2ps-4.14-i486-3.txz
b41fffae3062fb67118349aea3b0ef3b  ./packages/ap/pm-utils-1.2.5-i486-1.txz
840816e056a58fe7c51de7d20b673ce3  ./packages/ap/systemtools-6.2-i486-62.8.tlz
69a7f5efdacde4adac95e92e20bd8e50  ./packages/ap/mc-20090714_git-i486-1.txz
24b27751b834165918876cce90799c0d  ./packages/ap/man-1.6f-i486-2.txz
b3b4233f0f08020a0b6195426762e0d4  ./packages/ap/gutenprint-5.2.4-i486-62.1.txz
2ef0f7502bb23f09255950adcdcf52e2  ./packages/ap/cdrkit-1.1.9-i486-62.2.txz
c329ae7f3b9ffa7d085a7922dc5a44d3  ./packages/ap/dmapi-2.2.10-i486-1.txz
44dfa989d9db78d13c5361306e3a8614  ./packages/ap/hdparm-9.27-i486-64.1.tlz
9bb2bdd8eb58b9e2455587ee78a3a049  ./packages/ap/depfinder-0.9.2-i486-62.1.tlz
70ac2d99fb8cecf8c7d41a370760a26d  ./packages/ap/netpkg-4.6-i486-62.1.tgz
8ff831361dc6351b3e727fe3ce6e63a1  ./packages/ap/lsscsi-0.22-i486-1.txz
d5025a3b04cc5d7e2431418e72596e82  ./packages/ap/vim-7.2.245-i486-1.txz
fc9c5cd0729f8bbfc5c29bb2726a93d3  ./packages/ap/foomatic-filters-4.0_20090310-i486-60.1.tgz
25d9ab458c4b2ce5698f9e1447261641  ./packages/ap/ntfs-3g-2009.4.4-i486-62.1.tgz
d403019b5a8c6f85c981ab1cb7aeeeeb  ./packages/ap/xfsdump-3.0.1-i486-1.txz
dbb7b3374e75e7e51d27a0eff3db540a  ./packages/ap/netpkg-4.5.6-i486-60.2.tgz
ae2a3c12a55f0d919adbd70827f41924  ./packages/ap/sudo-1.7.2p1-i486-62.1.txz
bash info
$ bash --version
GNU bash, version 3.1.17(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.



Thank you for your time.

Last edited by peter1234; 09-21-2010 at 09:09 AM. Reason: [SOLVED]
 
Old 09-20-2010, 02:58 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Would this help:
Code:
#!/bin/bash
#
# Filter files by file name/partial file name. (0.60v)
#
LOOKFOR="-62"  # what to look for is within ""

while read applMD5 applPACK #read both entries as one specific variable
do
  if [[ "$applPACK" =~ "$LOOKFOR" ]]
  then 
    echo "$applMD5  $applPACK" # Double spaces between the two, quotes are a must
  else
    echo "some commands"
  fi
done < applist.md5
 
1 members found this post helpful.
Old 09-20-2010, 03:19 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
The echo command squeezes unquoted multiple spaces down to a single space.
Code:
string="x       x"
echo $string
x x

echo "$string"
x       x

# Try putting $LINE_READ in double quotes to keep the original spacing.
echo "$LINE_READ" >> matched_1sp.md5 # write md5 + relative path + filename
 
1 members found this post helpful.
Old 09-21-2010, 09:04 AM   #4
peter1234
Member
 
Registered: Apr 2009
Posts: 42

Original Poster
Rep: Reputation: 2
Thank you two for your input.

Kenhelm:
"Double quotes to keep the original spacing."
It works.


druuna:
Reading line into two strings and using double quotes also works.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Write only unique strings to text file (cat or while read question) SilversleevesX Programming 32 08-11-2010 02:24 AM
[SOLVED] [bash] read file, modify, write - possible without tmpfile? hashbang#! Programming 2 02-11-2010 05:27 AM
Write into File from Kernel and read the same file from User space saurabhchokshi Programming 0 05-01-2009 02:26 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
bash sort files by date in file name thedude2010 Programming 6 05-12-2006 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration