Solved

shell script : moving  files to other folder after grep fiter

Posted on 2014-04-16
11
609 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
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 167 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 249 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 167 total points
ID: 40003451
The grep is based on posted output:


3    file01
3    file02
2    file03
0
 

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 249 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 249 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 84 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now