LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   help with delete script (https://www.linuxquestions.org/questions/linux-general-1/help-with-delete-script-890051/)

crxlurp 07-05-2011 11:59 AM

help with delete script
 
Hey guys! First off, thanks for any help you can provide me. I am trying to figure out how to write an awk script(or something better if it exists) to read a text file that contains lines like below.

4517-s-1295546289-10:58:09-2011-01-20.wav | 19990 KiB | 2940.5 KiB/s | binary | 100%
4517-s-1303247233-15:07:13-2011-04-19.wav | 167 KiB | 2918.3 KiB/s | binary | 100%
4517-s-1301431748-14:49:08-2011-03-29.wav | 36220 KiB | 2589.3 KiB/s | binary | 100%
4517-s-1295974735-09:58:55-2011-01-25.wav | 20038 KiB | 2646.0 KiB/s | binary | 100%
4517-s-1295648478-15:21:18-2011-01-21.wav | 45561 KiB | 2154.1 KiB/s | binary | 100%
4517-s-1300384178-11:49:38-2011-03-17.wav | 6752 KiB | 2216.0 KiB/s | binary | 100%

I want to find lines that start with 4517-s and end with 100% and delete them from a directory.

I am sorry, I just am too new to know where to even start.

crxlurp 07-05-2011 12:18 PM

I have gone over my output.txt file that I will be reading and it looks like I can just delete any file whose line ends in 100% Not sure if that makes things easier or not.

TB0ne 07-05-2011 12:18 PM

Quote:

Originally Posted by crxlurp (Post 4405840)
Hey guys! First off, thanks for any help you can provide me. I am trying to figure out how to write an awk script(or something better if it exists) to read a text file that contains lines like below.

4517-s-1295546289-10:58:09-2011-01-20.wav | 19990 KiB | 2940.5 KiB/s | binary | 100%
4517-s-1303247233-15:07:13-2011-04-19.wav | 167 KiB | 2918.3 KiB/s | binary | 100%
4517-s-1301431748-14:49:08-2011-03-29.wav | 36220 KiB | 2589.3 KiB/s | binary | 100%
4517-s-1295974735-09:58:55-2011-01-25.wav | 20038 KiB | 2646.0 KiB/s | binary | 100%
4517-s-1295648478-15:21:18-2011-01-21.wav | 45561 KiB | 2154.1 KiB/s | binary | 100%
4517-s-1300384178-11:49:38-2011-03-17.wav | 6752 KiB | 2216.0 KiB/s | binary | 100%

I want to find lines that start with 4517-s and end with 100% and delete them from a directory.
I am sorry, I just am too new to know where to even start.

Well, your post is a bit confusing. Do you want to delete the LINES from the text file, or do you want to delete the .wav files themselves??? And you obviously know where to start, since you mention awk. Did you try to look up any tutorials, or read the man pages??

I'd grep the text file first, and look for anything with "100%", then read that and pipe it into rm. Of course, you should back up your data first, whenever you're doing something that deletes files. Read the man pages for grep and awk, and read one of the THOUSANDS of bash scripting tutorials you can find with Google. This:
Code:

rm `grep "100\%" <filename> | awk {'print $1'}`
may work. Untested, back up your data first, and read the man pages. Also, note those are backticks, NOT single quotes....

acid_kewpie 07-05-2011 12:21 PM

grep 100% file.txt | cut -d\ -f1 | xargs rm -f

PTrenholme 07-05-2011 12:41 PM

Or, if you want to do it in gawk, gawk -F'|' '/^4517.*100%$/{print "rm -f " $1}' input_text_file_name should print out all the remove commands (for QA check), and replacing the print "rm -f " $1 with a system("rm -f " $1) should run it.

Note: Untested code.

crxlurp 07-05-2011 01:09 PM

Quote:

Originally Posted by PTrenholme (Post 4405881)
Or, if you want to do it in gawk, gawk -F'|' '/^4517.*100%$/{print "rm -f " $1}' input_text_file_name should print out all the remove commands (for QA check), and replacing the print "rm -f " $1 with a system("rm -f " $1) should run it.

Note: Untested code.

If I do this, would I put my list file where it says "input_text_file_name" and it will print on the screen? When I run this, I see no output on the screen

This is the command I ran.

Code:

gawk -F'|' '/.*100%$/{print "rm -f " $1}' output.txt

crxlurp 07-05-2011 01:12 PM

Quote:

Originally Posted by acid_kewpie (Post 4405863)
grep 100% file.txt | cut -d\ -f1 | xargs rm -f

I am so sorry I am so new, but can you break this down and explain to me what it is all doing? I get the grep part, but after that I am not sure.

TB0ne 07-05-2011 01:25 PM

Quote:

Originally Posted by crxlurp (Post 4405904)
I am so sorry I am so new, but can you break this down and explain to me what it is all doing? I get the grep part, but after that I am not sure.

Read the man page for cut and xargs. Type in "man cut" and "man xargs"...this is a good idea for ANY linux command, as it will tell you what it does, what the options mean, and how to use the command.

MTK358 07-05-2011 01:27 PM

This is a duplicate thread:

http://www.linuxquestions.org/questi...delete-890037/

Anyway, the "cut" command gets rid of everything after the first space character on every line, and the "xargs" command splits the input into separate strings using whitespce as the delimiter, and then passes them as arguments to rm.

crxlurp 07-05-2011 01:30 PM

Yes, sorry about the duplicate thread. I need to delete the newbie one. I don't think that was the right place to put it. anyway, could I run grep 100% file.txt | cut -d\ -f1 | xargs rm -f without the delete and have it output what it finds and cuts?

MTK358 07-05-2011 01:33 PM

Quote:

Originally Posted by crxlurp (Post 4405915)
anyway, could I run grep 100% file.txt | cut -d\ -f1 | xargs rm -f without the delete and have it output what it finds and cuts?

Sure, just replace "rm -f" (By the way, I'm not sure that using "-f" is a good idea here) with "echo".

crxlurp 07-05-2011 01:37 PM

when I run
Code:

grep 100% output.txt | cut -d\ -f1 | echo
I get
Code:

cut: the delimiter must be a single character
Try `cut --help' for more information.


acid_kewpie 07-05-2011 01:37 PM

!
Quote:

Originally Posted by MTK358 (Post 4405919)
Sure, just replace "rm -f" (By the way, I'm not sure that using "-f" is a good idea here) with "echo".

I can think of nothing more fun than pressing "y" 4,000 times!

MTK358 07-05-2011 01:39 PM

Quote:

Originally Posted by crxlurp (Post 4405921)
when I run
Code:

grep 100% output.txt | cut -d\ -f1 | echo
I get
Code:

cut: the delimiter must be a single character
Try `cut --help' for more information.


There must be two spaces after the backslash. The backslash quotes the first one as a literal character, and the sacond one acts as the argument separator. Also, you can use quotes instead, which I think looks nicer:

Code:

cut -d' ' -f1

crxlurp 07-05-2011 02:47 PM

when I run
Code:

grep 100% output.txt | cut -d" " -f1 | echo
It returns nothing. I have pasted the first few lines of the file below. It acts like it can't find anything in field 1 but shouldn't that be everything before a space?


Beginning of file

C:\Data\bkup\recording>cd\

C:\>cd data

C:\Data>cd Shared

C:\Data\Shared>cd "10-Recording Archive"

C:\Data\Shared\10-Recording Archive>cd 4511

C:\Data\Shared\10-Recording Archive\4511>winscp /script=C:\Data\bkup\recording\recordingftp4511.script
batch on
confirm off
Searching for host...
Connecting to host...
Authenticating...
Using username "admin".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Reading remote directory...
Session started.
Active session: [1] admin@192.168.90.15
/var/spool/asterisk/monitor
transfer binary
4511-s-1295472704-14:31:44-2011-01-19.wav | 165 KiB | 3446.4 KiB/s | binary | 100%
4511-s-1299187431-14:23:51-2011-03-03.wav | 153 KiB | 2521.2 KiB/s | binary | 100%
4511-s-1295539212-09:00:12-2011-01-20.wav | 8472 KiB | 2975.7 KiB/s | binary | 100%
Session 'admin@192.168.90.15' closed.
No session.

C:\Data\Shared\10-Recording Archive\4511>cd ..

C:\Data\Shared\10-Recording Archive>cd 4513

C:\Data\Shared\10-Recording Archive\4513>winscp /script=C:\Data\bkup\recording\recordingftp4513.script
batch on
confirm off
Searching for host...
Connecting to host...
Authenticating...
Using username "admin".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Reading remote directory...
Session started.
Active session: [1] admin@192.168.90.15
/var/spool/asterisk/monitor
transfer binary
Session 'admin@192.168.90.15' closed.

This is the end of the snippet of file

This goes on for hundreds of lines, but essentially the same thing repeating.


All times are GMT -5. The time now is 07:04 AM.