?
Solved

shell script : moving  files to other folder after grep fiter

Posted on 2014-04-16
11
Medium Priority
?
625 Views
Last Modified: 2014-04-21
i have some hundreds of file  in directory of our application

each file has  2/3 file extention in directory,
example :

file01.ext1,
file01.ext2
file01.ext3
----

to simply which file, have how many counts , I am using below script

ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3'

The above shell script  which display output in below format, for the current directory files..... working well.
---- O/P from the above  script -------------

3    file01
3    file02
2    file03
----------------

where

Column-1,    file count of its different file extension
column-2,    is filename,

now ...I need

i want to move files into some directory, which  has count=3


please advice the needed enhancement
0
Comment
Question by:mac_g
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 501 total points
ID: 40003407
try this

for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3' | grep -x 3 | awk '{ print $2 }'`
do
    mv $file.* targetdir
done
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 747 total points
ID: 40003432
The lines with three extensions are already selected by the awk, so you don't need the "grep -x" command.

for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3{print $2}'`
do
    mv $file.* targetdir
done
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 501 total points
ID: 40003451
The grep is based on posted output:


3    file01
3    file02
2    file03
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:mac_g
ID: 40003507
i have created directory name -temp, in same location

altered your script  to
-----
for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3' | grep -x 3 | awk '{ print $2 }'`
do
    mv $file.*  ./temp
done
----

no files has moved ..
any idea how to fix this issue
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 747 total points
ID: 40003515
Yes, remove the "grep -x 3" - the "-x" matches an entire line, and since none of the lines contain just a "3", none match.
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 747 total points
ID: 40003520
@omarfarid - it was confusing, but the posted output was of the command up to the "uniq -c" - it didn't include the "awk '+$1 == 3'" bit.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 252 total points
ID: 40012079
for f in $(ls |awk -F"." '{print $1}'|uniq -c|awk '$1==3 {print $2}')
do
   echo "Moving files starting with $f"
   mv $f*  /some/dir
done

Open in new window

0
 
LVL 19

Expert Comment

by:simon3270
ID: 40012159
One small comment on the accepted answer - you need the dot after the filename in the "mv" command (as I had in my answer #a40003432)
for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3{print $2}'`
do
    mv $file.* targetdir
done

Open in new window

If you have, for example:
    file1.ext1
    file1.ext2
    file1.ext3
    file10.ext
in your input directory, the accepted answer would copy the file10.ext1 file, because it matched "file1*", even though there was only one file with the file10 prefix.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40012286
Well picked up Simon.
0
 

Author Comment

by:mac_g
ID: 40012306
tintin...

I did not see while choose the solution ..

apologies ... you deserve most
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40012662
No problem.  Only spotted it because I'd been hit by the same bug recently!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question