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