Finding files with one row then moving them


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding files with one row then moving them
# 1  
Old 04-25-2012
Finding files with one row then moving them

Hi guys can you please help me with a script to find files with one row the move the file to another directory
the files are in
Code:
/optima/prd/optdir/ZTE_BSS/Combiner/ZTE_2G/out/BSC_PS_Basic_Meas

and I want to move them to
Code:
/optima/prd/optdir/ZTE_BSS/Loader/error/ZTE_2G/BSC_PS_Basic_Meas


Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 04-25-2012 at 07:33 AM.. Reason: code tags
# 2  
Old 04-25-2012
"One row" means with 1 line of content? What did you try so far?
# 3  
Old 04-25-2012
Code:
from=/optima/prd/optdir/ZTE_BSS/Combiner/ZTE_2G/out/BSC_PS_Basic_Meas
to=/optima/prd/optdir/ZTE_BSS/Loader/error/ZTE_2G/BSC_PS_Basic_Meas

for file in $from/*
do
    # look for regular files with one line
    if [ -f $file ] ; then
           # deliberate use of cat 
        if [ $(cat $file | wc -l) -eq 1] ; then  # get the file name no path
           bname=$(basename $file)
           mv $file $to/$bname                   # mv the file to path/bname
        fi   
    fi
done

Try something like the above
# 4  
Old 04-25-2012
The script runs but nothing happens the files do not move


Code:
from=/optima/prd/optdir/testbsc/dist/
to=/optima/prd/optdir/testbsc/dist_old/
for file in $from/*.
do
    if [ -f $file ] ; then
      if [ $(cat $file | wc -l) -eq 1] ;
          then
           bname=$(basename $file)
           mv $file $to/$bname
      fi
    fi
done

# 5  
Old 04-25-2012
Didn't see what OS, shell, etc., you're running, but this may help:

Code:
awk -v DIR="/path/to/dir" 'END{if(NR==1) system("mv -vv "FILENAME" "DIR"")}' /path/to/file

This uses awk to set a variable pointing to the destination directory, determine if the file only has a single line, and then use the system function to run the move command to move the current input file (FILENAME) to the destination directory.

Hope this helps.
# 6  
Old 04-26-2012
Oh sorry I am using Hp UX

---------- Post updated at 08:45 AM ---------- Previous update was at 08:38 AM ----------

Do i add this into my previous script? sorry I am very new to this

Quote:
Originally Posted by in2nix4life
Didn't see what OS, shell, etc., you're running, but this may help:

Code:
awk -v DIR="/path/to/dir" 'END{if(NR==1) system("mv -vv "FILENAME" "DIR"")}' /path/to/file

This uses awk to set a variable pointing to the destination directory, determine if the file only has a single line, and then use the system function to run the move command to move the current input file (FILENAME) to the destination directory.

Hope this helps.
---------- Post updated 04-26-12 at 04:14 AM ---------- Previous update was 04-25-12 at 08:45 AM ----------

guys can anyone help please
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding only one error in a row

I am using below code to validate whether datatype,length and date format is correct as defined in file_layout. Below code is able to find only one error in a row,if there is more than one error in a same row then code is not able to highlight second error. How to customize the below code to... (8 Replies)
Discussion started by: srivalli
8 Replies

2. Shell Programming and Scripting

In php, Moving a new row to another table and deleting old row

Hi, I already succeed moving a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). But it can't delete the old row. Please help me with the script. my php script: INSERT INTO... (2 Replies)
Discussion started by: jazzyzha
2 Replies

3. Shell Programming and Scripting

Moving new row and deleting old row to another table

Hi, I want to move a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). I already use this script but doesn't work as I expected. CHECK_KEYWORD="$( mysql -uroot -p123456 smsd -N... (7 Replies)
Discussion started by: jazzyzha
7 Replies

4. UNIX for Dummies Questions & Answers

Finding row number along with length of row

I have a fixed length file and I want to find out row number along with row length. I have a program that give me the line length if it satisfy the condition; but i would like to add row number as well? How do I do that? while IFS= read -r line; do if ; then echo ${line} echo... (8 Replies)
Discussion started by: princetd001
8 Replies

5. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

6. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

Finding files older than the current date and time and renaming and moving

Hi, I have a very urgent requirement here. I have to find all files in the specified directory but not in the sub directories(The directory name is stored in a variable) which are older than the current date as well as current time and rename it as filename_yyyymmddhhmmss.ext and move it into a... (7 Replies)
Discussion started by: ragavhere
7 Replies

9. Shell Programming and Scripting

Finding a XML element and moving the file

Hi All, I am looking for a awk/shell which can find an element named REFERENCE in a XML file and check whether it is empty or not. If there is no value in the REFERENCE element then correspondingly move the file to some other folder. The Unix server is AIX version 4. Any inputs... (9 Replies)
Discussion started by: karansachdeva
9 Replies

10. UNIX for Advanced & Expert Users

Finding last row

Using Linux and Bash, I have a script that outputs filenames with complete path, like this:I would like the following output:And I would like to get the filenames only. Tricky part is that I cannot predict how many levels deep the filename is located, so I cannot use standard Bash-kungfu to solve... (7 Replies)
Discussion started by: indo1144
7 Replies
Login or Register to Ask a Question