Solved

shell script : moving  files to other folder after grep fiter

Posted on 2014-04-16
11
619 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

688 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