Solved

shell script : moving  files to other folder after grep fiter

Posted on 2014-04-16
11
615 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mcrypt_create_iv() is deprecated 4 143
Like to learn Linux, need advice for a good book and setup. 3 44
open source backup solution 1 28
error log using ftp 7 38
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

786 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