Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract specific strings from line in file and output to another file with modifications

Tags:

find

bash

sed

awk

cut

New to linux and trying to escape doing this the hard way. I have a file ("output.txt") that contains the results of a 'find' command. Example first three lines from "output.txt":

/home/user/temp/LT50150292009260GNC01/L5015029_02920090917_MTL.txt
/home/user/temp/LT50150292009276GNC01/L5015029_02920091003_MTL.txt
/home/user/temp/LT50150292009292GNC01/L5015029_02920091019_MTL.txt

I'd like to use awk or sed (or similar) to extract two parts from the path listed for each line, and output to a new file ("run.txt") with extra information added on each line like so:

cd /home/user/temp/LT50150292009260GNC01; $RUNLD L5015029_02920090917_MTL.txt
cd /home/user/temp/LT50150292009276GNC01; $RUNLD L5015029_02920091003_MTL.txt
cd /home/user/temp/LT50150292009292GNC01; $RUNLD L5015029_02920091019_MTL.txt

I'm guessing this might also involve something like "cut", but I can't get my head wrapped around how to account for changing folder and file names.

Any help would be much appreciated.

like image 369
Prophet60091 Avatar asked Jan 22 '26 02:01

Prophet60091


1 Answers

sed -e 's/^/cd /; s|/\([^/]*\)$|; \$RUNLD \1|' file

This prepends "cd " and replaces the last / with "; $RUNLD ". Voila!

like image 77
Jens Avatar answered Jan 24 '26 20:01

Jens



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!