Redirecting stdout and xargs

Posted on 2005-03-08
Medium Priority
Last Modified: 2010-05-18
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?

Question by:dignified
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 13492619
You can always do

for i in *.txt
  sed -e 'stuff' $i >$i.new

Author Comment

ID: 13492626
that's no fun though. This question has been irking me for quite some time.
LVL 16

Expert Comment

ID: 13492661
ls *.txt | sed -e 's/^\(.*\)$/cp "\1" "\1.new"/' |sh
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.


Author Comment

ID: 13493029
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

LVL 16

Accepted Solution

manav_mathur earned 750 total points
ID: 13493328
find . -name "*.txt" -exec echo "sed 's/\\N//g' < {} > {}.new" | sh

LVL 51

Expert Comment

ID: 13495993
can we do it the other way around: rename your old one
  perl -i.old -pe 's/lamb/sheep/g' *.txt

Author Comment

ID: 13499036
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.

Author Comment

ID: 13499128
find . -name "*.txt" -exec sh -c "sed 's/\\N//g' < {} > {}.new" \;

this seems to do the trick.

Featured Post

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question