Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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....
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.
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
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.
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.
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?
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.