'How do I replace ".net" with space using sed in Linux?

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?



Solution 1:[1]

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).

Solution 2:[2]

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

Solution 3:[3]

This way:

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

It will create a backup for safeness

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew
Solution 2 Gilles Quenot
Solution 3