• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 868
  • Last Modified:

Redirecting stdout and xargs

I am using sed to modify a directory of files. I want the modified files to be renamed with a .new extension.

So, right now I'm doing something like this:

ls *.txt | xargs -i sed -e 'stuff' {}

This will go through and do my sed manipulating on every txt file. I want to pipe the output of sed into a new filename per file.
So if the files are originally:  first second third
Then after the xargs does its thing, I want the newly modified files in the directory as well. so the listing will look something like: first first.new second second.new third third.new

I'm trying to do something like this:

ls *.txt | xargs -i sed -e 'stuff' {} > {}.new

but obviously this doesn't work because of the redirect. How can I make this work?

1 Solution
You can always do

for i in *.txt
  sed -e 'stuff' $i >$i.new
dignifiedAuthor Commented:
that's no fun though. This question has been irking me for quite some time.
ls *.txt | sed -e 's/^\(.*\)$/cp "\1" "\1.new"/' |sh
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

dignifiedAuthor Commented:
That looks like it will just rename the files... you can do that with cp filename{,.new} or something like that.

I want to replace all \N in a text file with nothing... right now I'm using   ls *.txt | xargs -i sed -e 's/\(\\N\)//' {} > {}.new

find . -name "*.txt" -exec echo "sed 's/\\N//g' < {} > {}.new" | sh

can we do it the other way around: rename your old one
  perl -i.old -pe 's/lamb/sheep/g' *.txt
dignifiedAuthor Commented:
manav_mathur... this is more what I'm looking for. Howerver I get this error:
find: missing argument to `-exec'

I read that you need a space between the {} and also terminate things with a \;

For example, this works:
find . -name "*.txt" -exec echo ""\;

but not
find . -name "*.txt" -exec echo "hello world"\;

which will give the same error.
dignifiedAuthor Commented:
find . -name "*.txt" -exec sh -c "sed 's/\\N//g' < {} > {}.new" \;

this seems to do the trick.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now