LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-05-2011, 11:59 AM   #1
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Rep: Reputation: Disabled
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.
 
Old 07-05-2011, 12:18 PM   #2
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-05-2011, 12:18 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,613

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by crxlurp View Post
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....
 
1 members found this post helpful.
Old 07-05-2011, 12:21 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
grep 100% file.txt | cut -d\ -f1 | xargs rm -f
 
Old 07-05-2011, 12:41 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 07-05-2011, 01:09 PM   #6
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme View Post
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

Last edited by crxlurp; 07-05-2011 at 01:11 PM.
 
Old 07-05-2011, 01:12 PM   #7
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 07-05-2011, 01:25 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,613

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by crxlurp View Post
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.
 
Old 07-05-2011, 01:27 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 07-05-2011, 01:30 PM   #10
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
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?
 
Old 07-05-2011, 01:33 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by crxlurp View Post
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".
 
Old 07-05-2011, 01:37 PM   #12
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-05-2011, 01:37 PM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
!
Quote:
Originally Posted by MTK358 View Post
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!
 
Old 07-05-2011, 01:39 PM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by crxlurp View Post
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
 
Old 07-05-2011, 02:47 PM   #15
crxlurp
LQ Newbie
 
Registered: Jul 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
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.

Last edited by crxlurp; 07-05-2011 at 02:50 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk script to filter and delete crxlurp Linux - Newbie 6 07-05-2011 01:33 PM
help - script to delete the data samengr Linux - Newbie 12 09-14-2008 08:16 PM
Delete old files script simpi Linux - Newbie 11 04-25-2008 02:37 AM
script to delete some processes Carlos2dub Programming 1 12-14-2007 09:18 AM
Help with a delete user script nazs Linux - General 1 02-20-2006 05:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration