LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2011, 07:44 AM   #1
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Search for two or more 'patterns' and move files according to results


Hi

I'm lost with searching for 2 and then more 'patterns' within a file with a large number of filenames and according model names, then I want to rename and move the files to appropriate 'found' result (the source file attached).

In short,
searching for the name in line:
Code:
D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\motorne_zage\lahke_motorne_zage\30\ms_5F250_c_be\default.htm
     284  							<div class="picture"><a href="../../../../../../../media/cache/image/30_big-b4d46b171375e7f3.jpg" class="thumbnail hs"><img src="../../../../../../../media/cache/image/30_big-ee8de7c7fa41401a.jpg" width="500" height="250" alt="MS 250 C BE"/></a></div>
     320  							<div class="picture"><a href="../../../../../../../media/cache/image/30_big-b4d46b171375e7f3.jpg" class="thumbnail hs"><img src="../../../../../../../media/cache/image/30_big-2443fdbb986dfef6.jpg" width="120" height="60" alt="MS 250 C BE"/></a></div>
4 matches in D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\motorne_zage\lahke_motorne_zage\30\ms_5F250_c_be\default.htm
should find me the filename cache/image/"30_big-b4d46b171375e7f3.jpg" and the name from alt="MS 250 C BE" (MS 250 C BE)
and then I would like to rename it "30_big-b4d46b171375e7f3.jpg" -> "MS 250 C BE.jpg" and move it to the folder "ms_5F250_c_be" (which is found in 1.st line near end: ...orne_zage\30\ms_5F250_c_be\default.htm ). There is also the smaller picture "30_big-2443fdbb986dfef6.jpg" in the second like line which can be named "MS 250 C BE(1).jpg" and moved

These are CMS uploaded files and named randomly so now the customer wants it sorted (updated and deleted unnecessary).


Since I'm trying to do this with some kind of grep/sed... search for 2 days I'm lost and I have no script made yet, thus I'm asking kindly someone (if it can be done) for the complete search code, which I could use.

I may have not provided exactly what I need to do, but I'm soooo messed up right now doing this manually, I could not appreciate more if this can do some script instead of me.

Regards
Attached Files
File Type: txt files.txt (103.2 KB, 4 views)

Last edited by lithos; 10-06-2011 at 07:57 AM. Reason: forgot attachment
 
Old 10-07-2011, 11:09 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
This may help get you started, obviously the syntax is wrong with the mixing of / and \.

Code:
#!/bin/bash

cat files.txt | dos2unix | grep -v " matches " | while read -r line
do
  a=$(echo $line | cut -c1)
  if [ $a == "D" ]
  then 
    # Target directory
    b=$(echo $line)
    n=0
  else
    # Source file
    c=$(echo $line | cut -d'"' -f8)
    # Target file name
    d=$(echo $line | cut -d'"' -f14 | tr ' ' '_')

    if [ $n = 0 ]
    then
        echo cp ${c##.*/} ${b%\\*}\\$d.jpg
    else 
        echo cp ${c##.*/} ${b%\\*}\\$d\($n\).jpg
    fi
    let "n=n+1"
  fi
done
Output:

Code:
cp 101_big-7903fadb4cddcfde.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kombi_motorji\101\km_130_r\KM_130_R.jpg
cp 101_big-df4a4bcc44d03bbb.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kombi_motorji\101\km_130_r\KM_130_R(1).jpg
cp 688_big-c4c5b0fd711cd0f4.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kombi_motorji\688\km_90_r\KM_90_R.jpg
cp 688_big-44c80e6d7ca82720.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kombi_motorji\688\km_90_r\KM_90_R(1).jpg
cp 153_big-3326289580109cb2.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kompaktni_brusilno_rezalni_stroji\153\ts_5F400\TS_400.jpg
cp 153_big-5077ebb54fe81f59.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kompaktni_brusilno_rezalni_stroji\153\ts_5F400\TS_400(1).jpg
cp 156_big-20fbb0eecaa27f95.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kompaktni_brusilno_rezalni_stroji\156\ts_700\TS_700.jpg
cp 156_big-8f287afe43caadf2.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kompaktni_brusilno_rezalni_stroji\156\ts_700\TS_700(1).jpg
cp 792_big-d1e7d51a6ccb5b2a.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kompaktni_brusilno_rezalni_stroji\792\ts_800\TS_800.jpg
cp 792_big-aaa1cbc882bf62a6.jpg D:\temp\OfflineExplorerPortable\Download\unicommerce.si\produkti\produkti\produkti_stihl\kompaktni_brusilno_rezalni_stroji\792\ts_800\TS_800(1).jpg
 
1 members found this post helpful.
Old 10-07-2011, 11:36 AM   #3
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE 12.1
Posts: 96

Rep: Reputation: 24
Wink

i create a file test.awk
Code:
/default.htm/ { h=$(NF-1); print "H="h }
/<div / { if (h!="") { x1=index($0,"image/"); x2=index($0,".jpg"); print substr($0,x1+6,x2-x1-2); }}
when doing this:
Code:
gawk -F "\\" -f test.awk file.txt
i get:
Quote:
H=km_130_r
101_big-3dfdee7212009dda.jpg
101_big-3dfdee7212009dda.jpg
H=km_130_r
H=km_90_r
688_big-6b1563a266b616c9.jpg
688_big-6b1563a266b616c9.jpg
H=km_90_r
H=ts_5F400
153_big-4ea387342346e192.jpg
153_big-4ea387342346e192.jpg
H=ts_5F400
H=ts_700
156_big-58ed5ffae3205364.jpg
156_big-58ed5ffae3205364.jpg
H=ts_700.....
which should be enought to get you where you want to be.... (if you know AWK, of GAWK)
 
Old 10-07-2011, 12:04 PM   #4
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Original Poster
Rep: Reputation: 217Reputation: 217Reputation: 217
That is so great, thank you.

I've come only about 30% manually through, with this should be done in no time
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using file content as input for awk search patterns srn Programming 2 09-13-2011 03:49 AM
LXer: Search Patterns: Design for Discovery LXer Syndicated Linux News 0 03-17-2010 06:00 PM
How to search multiple files w/ SED then echo back the filenames and results??? hgate73 Programming 7 04-06-2009 04:11 PM
LXer: Regular expressions & search patterns LXer Syndicated Linux News 0 09-23-2007 03:51 AM
search / count unique patterns in text file logicalfuzz Linux - Newbie 2 10-14-2006 08:58 AM


All times are GMT -5. The time now is 05:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration