2

I believe it is really easy, but I didn't find anywhere an answer for me. What I'm trying to do is:

echo file.war | sed s/.war// | rm -rf ???

to pass to the rm -rf the output of the sed command.

Not sure if it is the right way to get this...

Thanks for your help in advance!

mr.nothing
  • 5,141
  • 10
  • 53
  • 77
  • It would help if you described exactly what you're trying to do, since it's easy to interpret wrongly from code that doesn't do what you want it to. – Wug Jun 20 '13 at 14:56
  • @legoscia, yeah, thanks. That's exactly what I wanted. – mr.nothing Jun 20 '13 at 14:57

3 Answers3

3

This is what 'xargs' does.

echo file.war | sed s/\.war$// | xargs rm -rf

And note my changes to your regex. It needs to be anchored and the '.' needs to be escaped.

ccarton
  • 3,556
  • 16
  • 17
1

Other possibilities, in :

This was in fact just to show you the command substitution $(...) thing (avoiding xargs that will fail miserably if you have file names containing funny symbols like spaces). Also to show you that in your case, sed is useless (thanks to the shell parameter expansion) and, if you really need sed, that the echo | sed thing can be avoided in bash.

I don't know what exactly you're trying to achieve. I could imagine:

  • you're trying to delete all files file such that file.war exist in current directory. In this case, I would do:

    for file in *.war; do
        rm -rf -- "${file%.war}"
    done
    
  • You have a file called filenames that contains lines like:

    file1.war
    file2.war
    ...
    filen.war
    

    and you want to delete all files file1, file2, …, filen. Then I would do:

    while read -r file; do
        [[ ( $file = *.war ) && ( -f $file ) ]] || continue
        rm -rf -- "${file%.war}"
    done < filenames
    
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
1

An alternative approach is

rm -rf `echo file.war | sed s/\.war$//`
Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55