Problem with deleting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with deleting
# 1  
Old 04-16-2010
Problem with deleting

Hi,

I have a folder A which contains files 1,2,3... I also have a file xyz under a diff folder B containg the entries 1,2,3...

I am trying to compare and delete the nonexisting ones from xyz using the below code:
Code:
QFILE=/B/xyz
cd A
ls -1 > /tmp/file1
for fname in `cat $QFILE`
do
  if [ -f /A/$fname ]
  then
    echo "Nothing to do\n" 
  else
    sed /$fname/d $QFILE > /tmp/file2
    mv /tmp/file2 $QFILE
  fi
done

but this is emptying the whole xyz file rather than just deleting the mismatching entry. pls suggest a mechanism to achieve this.

Last edited by Franklin52; 04-16-2010 at 12:19 PM.. Reason: Please indent your code and use code tags
# 2  
Old 04-16-2010
Code:
for d in dir/* do; [ -d "$d" ] && egrep -v "^`basename $d`$" file.txt > /tmp/file.$$ && mv /tmp/file.$$ file.txt || exit 1; done

js.

Last edited by Franklin52; 04-16-2010 at 12:24 PM.. Reason: Please use code tags, thank you
# 3  
Old 04-17-2010
Quote:
Originally Posted by jsmoriss
Code:
for d in dir/* do; [ -d "$d" ] && egrep -v "^`basename $d`$" file.txt > /tmp/file.$$ && mv /tmp/file.$$ file.txt || exit 1; done


Code:
bash: syntax error near unexpected token `['

There's no point to cramming a script on to a single line; all it does is make it harder to read and debug.

Code:
for d in dir/*
do
 [ -d "$d" ] &&
   egrep -v "^`basename $d`$" file.txt > /tmp/file.$$ &&
     mv /tmp/file.$$ file.txt ||
       exit 1
done

And basename, an external command, is not necessary:

Code:
for d in dir/*/
do
  egrep -v "^${d##*/}$" file.txt > /tmp/file.$$ &&
    mv /tmp/file.$$

# 4  
Old 04-19-2010
Quote:
Originally Posted by cfajohnson
There's no point to cramming a script on to a single line; all it does is make it harder to read and debug.
True. I tend to type one-offs on a single line... Tsdk, tsk. :-)

Quote:
And basename, an external command, is not necessary:
Depends on the shell you use...

js.
# 5  
Old 04-19-2010
Quote:
Originally Posted by jsmoriss
True. I tend to type one-offs on a single line... Tsdk, tsk. :-)

Even at the prompt, I usually type multi-line commands; they're easier to read.
Quote:
Depends on the shell you use...

The standard Unix shell can do it without an external command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script problem with deleting data

The 10.Delete data doesnt work at all, plesase anyone could help on that. When I choose options 10 to delete a record it only copy the selected data on the other file dbs1 but doesnt delete it from the database where other records are. #! /bin/bash i="y" echo "Enter name of database " read db... (6 Replies)
Discussion started by: Lina_14
6 Replies

2. UNIX for Advanced & Expert Users

reg problem deleting file

hi, 1. When i am deleting file in the linux machine i am getting below error message. rm text.fil rm: remove regular file `text.fil'? y rm: cannot remove `text.fil': Read-only file system From su also i tried i am getting same error message. 2. Some times the file permissions and user name... (1 Reply)
Discussion started by: coconut.ramu
1 Replies

3. Shell Programming and Scripting

Problem in deleting files

Hi Guys, In one of my folders i have 10000 log files starting with sqlfile..... Iam trying to delete the log files alone from the folder using for loop but i am getting an error ksh: /usr/bin/ls: 0403-027 The parameter list is too long. For loop i used for i in `ls sqlfile*`... (4 Replies)
Discussion started by: mac4rfree
4 Replies

4. Web Development

Deleting databases in MYSQL problem asking...

Hi, I'm the new user of mysql. I facing one problem to delete one of the database in mysql. Hope can get all of your suggestion and advice. Input: mysql> show databases; +-------------------------+ | Database | +-------------------------+ | information_schema | |... (3 Replies)
Discussion started by: patrick87
3 Replies

5. Shell Programming and Scripting

Problem in deleting lines from the file

Hi All I am not able to delete few line from long file. I have a big file which is of 53998 B in size i want to delete lines starting from 32768 to 53998 and collect remaining file in new file. It is giving exception sed: command garbled: 32768,$countd Please find the sample script... (2 Replies)
Discussion started by: rajeshorpu
2 Replies

6. UNIX for Advanced & Expert Users

problem with deleting a file

hi friends, i need to delete a file named "-vyxx-.0.sdct.txt" i'm unable to use "rm -vyxx-.0.sdct.txt" jus give me your suggesstions thanks in advance (5 Replies)
Discussion started by: rprajendran
5 Replies

7. UNIX for Dummies Questions & Answers

problem while deleting a file

Hi , In a particular path the below mentioned three files has been created. -rw-r--r-- 1 edwprod edw 0 Jun 5 00:06 -f%*lock.clock -rw-r--r-- 1 edwprod edw 151 Jun 5 00:06 -f.log -rw-r--r-- 1 edwprod edw 4 Jun 5 00:06 -f%conn_err.out ... (5 Replies)
Discussion started by: anupam_jash
5 Replies

8. UNIX for Dummies Questions & Answers

problem in deleting a file

Hello: I made this mistake on my tru64 machine, I renamed a file from: binary.errlog to binary.errlog(B3) now when im tryign to move or delete the file Im getting: # rm binary.errlog(B3) ksh: syntax error: `(' unexpected 4# mv binary.errlog(B3) log ksh: syntax error: `(' unexpected ... (3 Replies)
Discussion started by: aladdin
3 Replies

9. UNIX for Dummies Questions & Answers

Problem while Deleting file

All, I am trying to delete a user created file from specified folder. This folder is using for my Team member (i.e, Public folder). For example, $ ls -l total 1336 -rw-rw-r-- 1 g_btha customer 2655 Dec 06 12:35 A_COL_1001 While deleting/moving this file I am getting the following... (3 Replies)
Discussion started by: nvkuriseti
3 Replies

10. UNIX for Dummies Questions & Answers

Problem deleting print job

I am running SCO OpenServer Release 5 and I had a problem with a print job and I cancelled it. The print job still shows under lpstat as cancelled but doesn't go away and now I everything I print to the printer stays in the queue, but doesn't print. I have tried to delete the print job in... (2 Replies)
Discussion started by: sgtdmc
2 Replies
Login or Register to Ask a Question