Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in grep -E not working as expected

I'm working on a bash shell script, which goes through a folder and eventually makes new directories based off file names. Now I want to go through each file and strip away unwanted slashes of the path and ignore the fileextension before making a new directory. To test this, I'm echoing my file like this:

#!/bin/sh

cpath=`pwd`
for file in $cpath/*;do
echo $file | grep -E '(?!.*/).+(?=\.)'
done

But grep filters out everything and I get no output. I worked out the regex with RegExr http://gskinner.com/RegExr/?2vu6b
Negative lookahead to match the last slash and positive lookahead matching the last dot.

like image 661
Jonathan Frampton Avatar asked Mar 15 '26 13:03

Jonathan Frampton


1 Answers

I am not sure if Negative lookahead is a part of Extended RE. But you can do something like this -

#!/bin/bash

for file in $PWD/*; do
basename "${file%.*}"
done 
like image 164
jaypal singh Avatar answered Mar 18 '26 02:03

jaypal singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!