listing (ls) regex (regular expression) / command with basename / remove extension
Posted November 4th, 2008 by WilliamCarrier
Log from IRC server Freenode (IPv6) channel #SQL.
| <me> | I have a options file that will most likely be accessed repeatedly most likely up to 4 times and also have to run through light processing, overall what's faster/better : to run the process repeatedly or to set a session variable to the processed value so it save the repetition ? |
| <me> | sorry |
| <me> | I've searched how to pass find result through sed with a regex but can't find how, does someone have a simple example using like : (part1)(part2) : $1$2 Also can I pipe the results to make commands like : find args? | sed args? | command? $1 $2 |
| <jbggbj> | me: I don't understand what you mean |
| <me> | I need to filter the find result, specifically remove the directories and the extension, the second part from this regex should do the trick : (/?.*/)(.*)\.(.*) that's the most important part. after I need to use the result to make a command let's say echo it, but it could be a copy. |
| <jbggbj> | me: Easier to pipe whole find output to while read files loop, ie find ... | while read files; do echo "$files"; done |
| <jbggbj> | me: then you can parse each $files variable using basename to strip dirs and a regex to strip extension |
| <jbggbj> | me: may also have find options to do this, but I'd have to check man page myself |
| <me> | henux: you know how to run regex ? |
| <me> | *filter with |
| <jbggbj> | me: did it not help, eg to output the list in required form do, find ... | while read files; do basename "${files%.*}" ; done |
| <jbggbj> | me: where "find ..." is you find expression eg find . -name "*.mp3" |
| <jbggbj> | me: Advantage of this method is filenames can have spaces etc, and you can construct complex sequence of commands within the while loop |
| <jbggbj> | me: the "basename" command removes dirs, ${files%.*} removes suffix |
| <almostdvs> | in gnome; if i open a k app, i know it loads something called k libraries. but if i close it and no other k app is open does it automatically close those k libraries |
| <me> | jbggbj: great thanks |
| <me> | jbggbj: you figure it out or it's documented ? |
| <jbggbj> | me: It's fairly well-known in bash scripts |
| <me> | jbggbj: cool so I'd do somehting like find ... | while read files; do cp other_path/(basename "${files%.*}").other_extension destination_path ; done ? |
| <jbggbj> | me: Yeah, maybe a missing $ there, find ... | while read files; do newfile =cp other_path/$(basename "${files%.*}").other_extension destination_path ; done |
| <jbggbj> | me: sorry, messed up the edit, find ... | while read files; do cp other_path/$(basename "${files%.*}").other_extension destination_path ; done |
| <me> | jbggbj: thank you kindly |
| <jbggbj> | me: and use quotes around the full paths if there are spaces :) |
| <me> | jbggbj: cool ;) |
