Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir from grep and pipe

Tags:

grep

mkdir

pipe

I have this little script that puts out some dir paths. I want to mkdir all those paths.

for filename in $(git ls-files | grep .java | grep -v 'com/foo' | sed -e 's/\(.java[^/]*\).java/\1/' | uniq)
do
echo "$filename" | sed -e 's/com\/old\/old/com\/new/' | sed 's/\(.*\)\/.*/\1/'
done

So new dir are created... com/old/old = com/new

But I cannot get that mkdir to work... I tried..

path=$("$filename" | sed -e 's/com\/old\/old/com\/new/' | sed 's/\(.*\)\/.*/\1/')
mkdir -p "$path"

That is just messing with the file contents.

like image 442
klind Avatar asked Nov 27 '25 16:11

klind


1 Answers

You should be able to do all the substitutions in one sed command, and it can also filter out com/foo. Then pipe the output to a while read loop.

git ls-files | grep .java |
        sed -e '/com\/foo/d' -e 's/\(.java[^/]*\).java/\1/' -e 's/com\/old\/old/com\/new/' -e 's/\(.*\)\/.*/\1/' |
        uniq | while read path; do
    mkdir -p "$path"
done

Here's how to do your git mv:

git ls-files | grep .java |
        sed -e '/com\/intuit/d' -e 's/\(.java[^/]*\).java/\1/' | uniq | 
    while read path; do
        dir=$(echo "$path" | sed -e 's/com\/old\/old/com\/new/' -e 's/\(.*\)\/.*/\1/')
        mkdir -p "$dir"
        git mv "$path" "$dir"
done
like image 145
Barmar Avatar answered Dec 02 '25 04:12

Barmar



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!