LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk script to filter and delete (https://www.linuxquestions.org/questions/linux-newbie-8/awk-script-to-filter-and-delete-890037/)

crxlurp 07-05-2011 10:53 AM

awk script to filter and delete
 
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 11:02 AM

Also, I am not sure if I have posted this in the wrong place or not so if I have, please let me know and I will move it.
Thanks

aysheaia 07-05-2011 12:00 PM

For example (with a) input file named list.in, b) no spaces in any wav files, c) directory where to delete files located in current directory and named sound) :
Code:

$ dir=sound
$ awk -vDIR=$dir ' /^4517.*100%$/ { system("rm "DIR"/"$1) } ' list.in


crxlurp 07-05-2011 12:13 PM

First off thanks for your help. After looking at my file, I noticed that I can just go ahead and delete all files ending in 100% so I think I can just write this:

Code:

dir=/var/spool/asterisk/monitor
awk -vDIR=$dir ' /*100%$/ { system("rm "DIR"/"$1) } ' output.txt

is that correct?

aysheaia 07-05-2011 12:43 PM

Quote:

Originally Posted by crxlurp (Post 4405854)
Code:

dir=/var/spool/asterisk/monitor
awk -vDIR=$dir ' /*100%$/ { system("rm "DIR"/"$1) } ' output.txt

is that correct?

No, you must put a dot before the star :
Code:

$ awk -vDIR=$dir ' /.*100%$/ { system("rm "DIR"/"$1) } ' output.txt
In a regular expression (regexp), dot means any single character and star means that the previous regexp can be repeated 0 to n times (and dollar means the end of record).
See awk regexp at http://www.gnu.org/software/gawk/man...wk.html#Regexp

MTK358 07-05-2011 01:23 PM

Actually, you don't need the dot and star at all. This will work:

Code:

/100%$/

crxlurp 07-05-2011 01:33 PM

closing. Please look at http://www.linuxquestions.org/questi...pt-890051-new/

sorry for duplicate.


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