awk to del files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to del files
# 1  
Old 09-17-2011
awk to del files

Hi,

I want to delete files but it has space beween name & .log

For ex
Code:
-rw-r--r-- 1 root other 5429 Mar 19 2008 \AnjuP .log
-rw-r--r-- 1 root other 1352 Mar 21 2008 \ranjap .log
-rw-r--r-- 1 root other 1355 Mar 29 2008 \varghp .log

so if I run ls -ltr | head -3 | awk '{print $9 $10} it lists as
Code:
\AnjuP.log
\ranjap.log

i.e it omits space.
so It wont get delete with below command
Code:
ls -ltr | head -3 | awk '{print "rm " $9 $10} | sh

Thanks & Regards,
Nachiket

Last edited by Scott; 09-18-2011 at 11:34 AM.. Reason: Code tags, please...
# 2  
Old 09-17-2011
Hi,

Use 'for' instead:
Code:
$ touch "one .log" "two .log" "three .log"
$ ls -1 *.log
one .log
three .log
two .log
$ for file in *.log; do echo "rm $file"; done
rm one .log
rm three .log
rm two .log
$ for file in *.log; do rm "$file"; done

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 09-17-2011
how did you create such a file?

did you try
Code:
rm *.log

or
Code:
rm Anju.*.log

this will give you the log name you want
Code:
ls -ltr | head -3 | awk '{print $9" "$10}

--ahamed
# 4  
Old 09-18-2011
Hi,

files are genrated through applications.
I dont want to del all files so didn't use *.log and same case with for loop

so tried mv `find . -mtime +365 -exec ls -l {} \;` /test21/ but givin error
Code:
mv: cannot access ./\NASOLKA
mv: cannot access .log
mv: cannot access ./\jkeRkar
mv: cannot access .log
mv: cannot access ./\AshwinP
mv: cannot access .log

I think files in quotes with as it is space working
Code:
bash-2.03# ls -ltr | head -2
total 15697046
-rw-r--r-- 1 root other 5429 Mar 19 2008 \AnjuP .log

bash-2.03# ls -l \AnjuP .log
AnjuP: No such file or directory
.log: No such file or directory
bash-2.03# ls -l "\AnjuP   .log"
-rw-r--r-- 1 root other 5429 Mar 19 2008 \AnjuP   .log

How do I put files with spaces in quotes.?

Last edited by Scott; 09-18-2011 at 11:33 AM.. Reason: Added code tags
# 5  
Old 09-18-2011
If you use ls -tr instead of ls -ltr you do not need to concatenate fields but you can use the entire line instead
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 09-20-2011
ls -tr is listing exactly considering exact spaces.

But am facing problem while deleting these files.
for eg:
Code:
#rm \AnjuP  .log
AnjuP No such file or directory
.log No such file or directory

If i issue#rm "\AnjuP .log" file gets deleted.
So How do I put file-name in double quotes?
# 7  
Old 09-20-2011
Umm.... The way you just did it?

OR if you mean processing ls -tr's output:

Code:
ls -tr | while IFS="" read LINE
do
        echo rm "$LINE"
done

Remove the 'echo' once you're sure it does what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Del Line if specific issue

HI Guys I have below input : PO 121255 SorkO_RM_,rlsmel=310_410_3_11104_43831 PO 125564 SorkO_RM_,rlsmel=007G43823 I want delete line which have "_" after = Sign Output : PO 125564 SorkO_RM_,rlsmel=007G43823 (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

not del a file not affecting other permissions

i was faced by a question from a friend. i found it very tricky. all my months of learning unix i never can figure it out still. heres the question he faced me with. wondering if you all can help me figure a solution. ill let him know the forums helped me out :D What command would you use... (10 Replies)
Discussion started by: sunny231
10 Replies

3. UNIX for Dummies Questions & Answers

del lines

i have file that looks like this: 031000503 2000049262784 LCK00000050 i want to look like this: 031000503 2000049262784 LCK00000050 i'd like to del 10-15 Please use code tags! (6 Replies)
Discussion started by: lawsongeek
6 Replies

4. UNIX for Dummies Questions & Answers

How to del word by random input?

Hi, I got big problem. a line contains like this: Hell "A,B,C", how to delete A or B or C by using sed or other tools? I do not have any idea. Many thanks. (2 Replies)
Discussion started by: lemon_06
2 Replies

5. UNIX for Dummies Questions & Answers

route del trouble

Hi, I'm having trouble deleting a route: # route del 192.168.0.0 SIOCDELRT: Invalid process # route del -net 192.168.0.0 SIOCDELRT: Invalid argument The route is in the table: # route Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 ... (1 Reply)
Discussion started by: spaesani
1 Replies

6. UNIX for Dummies Questions & Answers

del 1st 2 lines

filename.txt VOL1000499 HDR1ACHTAP 101 031100225 0311002250911030803A094101WACHOVIA BANK OF 5200MASTER PAY GROUP 4231352166PPDAP PAYMENT 091026 1031100220000001 6221110000123756570069 00071351691000- 4022 FISHER HEALTHCARE ... (4 Replies)
Discussion started by: tjmannonline
4 Replies

7. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

8. Shell Programming and Scripting

Del - trash - restore CSH commands!!

Anyone who could help me with those three commands, i'm really new in cshell script so please be understandable.. Need to demostrate those three commands del trash (incl -ai) i that you will be asked to confirmed that they can be deleted a to move all those files from dustbin and to... (2 Replies)
Discussion started by: Visi_Ks
2 Replies

9. UNIX for Dummies Questions & Answers

how tp del progs

ermm... wats the general commands to uninstall programs.....izzit rpm -e cos it keep telling me package not installed..... anyway anyone know any good links for licq for redhat 9.0 shrike ... cos downloaded one ... dun seems to be working ... trying to get rid of it appreciate if anyone can... (3 Replies)
Discussion started by: kaya
3 Replies
Login or Register to Ask a Question