Link to home
Start Free TrialLog in
Avatar of dignified
dignified

asked on

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?


Avatar of Tintin
Tintin

You can always do

for i in *.txt
do
  sed -e 'stuff' $i >$i.new
done
Avatar of dignified

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can we do it the other way around: rename your old one
  perl -i.old -pe 's/lamb/sheep/g' *.txt
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.
find . -name "*.txt" -exec sh -c "sed 's/\\N//g' < {} > {}.new" \;

this seems to do the trick.