I want to grep all file names in a directory, modify names, and print modified names by echo command:
file names:
merged.reversed.m444.txt.gz
merged.reversed.m445.txt.gz
...
modified should be:
m444
m445
for i in *;
ModifiedName = sed 's/merged.reversed.//g' $i | sed 's//.txt.gz/g'
do echo "modified name of file $i is $ModifiedName"; done
To create a variable from sed
's output use it like this:
for i in *; do
ModifiedName=`echo "$i" | sed -e 's/merged\.reversed\.//' -e 's/\.txt\.gz//'`
echo "modified name of file $i is $ModifiedName"
mv "$i" "$ModifiedName"
done
Under BASH you could do:
ModifiedName=$(sed -e 's/merged\.reversed\.//' -e 's/\.txt\.gz//' <<< "$i")
I think you have some errors in your script.
A more correct version would be this one:
for i in *;
do
ModifiedName = `ls $i | sed -e s/merged\.reversed\.//g | sed -e s/\.txt\.gz//g`;
echo "modified name of file " $i " is " $ModifiedName;
mv $i $ModifiedName;
done
Note: To assign the value of the result of a command to a variable put the command into backticks (or $()) The . character is a special character so you need to backslash it to say to sed that you want to look for dots (otherwise you instruct it to look for ANY character)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.