Extract certain lines from a line to a file and output to another file with changes

New to Linux and trying to avoid this in a complicated way. I have a file ("output.txt") that contains the results of the Find command. An example of the first three lines of "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 would like to use awk or sed (or the like) to extract two parts from the path specified for each line and output to a new file ("run.txt") with additional information added on each line, for example

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 suppose this might also include something like “cut,” but I can't get my head to wrap myself around how to account for changes to the names of folders and files.

Any help would be greatly appreciated.

+3
source share
5
sed 's|^|cd |; s|/\([^/]*\)$|; $RUNLD \1|' inputfile > run

:

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

"cd" / "; $RUNLD". !

+1

bash

while IFS= read -r filename; do
  printf 'cd %s; $RUNLD %s\n' "${filename%/*}" "${filename##*/}"
done < output.txt > run

. http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

+1

, , grep- , cut awk -. :

while read x; do folder=$(echo "$x" | grep -o '^.*/'); file=$(echo "$x" | grep -o '[^/]*$'); echo "cd ${folder:0:-1}; \$RUNLD $file"; done < output.txt > run
0

This might work for you:

sed 's/\(.*\)\//cd \1; $RUNLD /' file
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
0
source

All Articles