Home > database >  How do I replace ".net" with space using sed in Linux?
How do I replace ".net" with space using sed in Linux?

Time:05-17

I'm using for loop, with arguments i. Each argument contains ".net" at the end and in directory they are in one line, divided by some space. Now I need to get rid of these ".net" using substitution of sed, but it's not working. I went through different options, the most recent one is

sed 's/\.(net)//g' $i;

which is obviously not correct, but I just can't find anything online about this.

To make it clear, lets say I have a directory with 5 files with names

file1.net
file2.net
file3.net
file4.net
file5.net

I would like my output to be

file1
file2
file3
file
file5

...Could somebody give me some advice?

CodePudding user response:

You can use

for f in *.net; do mv "$f" "${f%.*}"; done

Details:

  • for f in *.net; - iterates over files with net extension
  • mv "$f" "${f%.*}" - renames the files with the file without net extension (${f%.*} removes all text - as few as possible - from the end of f till the first ., see Parameter expansion).

CodePudding user response:

This is a work for perl's :

rename -n 's/\.net//' *.net

The -n is for test purpose. Remove it if the output looks good for you

CodePudding user response:

This way:

 sed -i.backup 's/\.net$//g' "$1";

It will create a backup for safeness

  • Related