AshBrad
AshBrad

Reputation: 492

bash sed regexp replace not finding results

I have a file with rows that look like:

mv -v DF-02239.jpg DF-02239.jpg
mv -v DF-02240.jpg DF-02240.jpg
mv -v DF-02241.jpg DF-02241.jpg
mv -v DF-02242.jpg DF-02242.jpg

I'm trying to replace the FIRST .jpg with .JPG and am using the following sed:

sed 's/\(mv -v .*?\)\(\.jpg\)\(.*\)/\1.JPG \3/' finalDuplicates.txt > finalCaseDuplicates.txt

I have verified the regexp works here: http://regex101.com/r/cU9xV2/1

Upvotes: 1

Views: 101

Answers (3)

Kalanidhi
Kalanidhi

Reputation: 5092

You are simply replace the jpg to JPG , sed replace the first occurrence only

sed 's/jpg/JPG/' file_name 

output

mv -v DF-02239.JPG DF-02239.jpg
mv -v DF-02240.JPG DF-02240.jpg
mv -v DF-02241.JPG DF-02241.jpg
mv -v DF-02242.JPG DF-02242.jpg

Upvotes: 2

hwnd
hwnd

Reputation: 70750

You can consider the following regex which uses a negated character class.

sed 's/^\(mv -v[^.]\+\.\)jpg/\1JPG/'

Ideone Demo

Upvotes: 1

user3620917
user3620917

Reputation:

Modifying your syntax just a bit:

sed 's/\(mv -v .*\.\)jpg\(.*jpg\)/\1JPG\2/'

The trick is to involve second jpg string into equation, so that greedy sed doesn't match the whole line.

Upvotes: 1

Related Questions