LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-18-2018, 02:16 PM   #1
black-clover
Member
 
Registered: Jun 2017
Distribution: Devuan Daedalus, Debian Testing, SliTaz, Alpine Linux.
Posts: 206

Rep: Reputation: Disabled
Command to edit text in several files


Hi,

let's say that I want to edit a bunch of files (txt or html) within the same directory and change a given string called <old-line> to <new-line> in all of them at once.

Which command line could I use to do that?
 
Old 10-18-2018, 02:31 PM   #2
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Rep: Reputation: 220Reputation: 220Reputation: 220
sed
 
Old 10-18-2018, 02:33 PM   #3
black-clover
Member
 
Registered: Jun 2017
Distribution: Devuan Daedalus, Debian Testing, SliTaz, Alpine Linux.
Posts: 206

Original Poster
Rep: Reputation: Disabled
this seems to have worked:

Code:
grep -r -l <old> * | xargs sed -i 's/<old>/<new>/g'
 
Old 10-18-2018, 02:37 PM   #4
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Rep: Reputation: 220Reputation: 220Reputation: 220
Consider using xargs with -r because if no matches are found you get this:
Code:
$ grep -r -l  stuff * | xargs  sed -i 's/<old>/<new>/g'
sed: no input files
 
Old 10-18-2018, 02:41 PM   #5
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Rep: Reputation: 220Reputation: 220Reputation: 220
And use NULL as a delimiter in case you had a file with a whitespace:

Code:
grep -r -l -Z  stuff * | xargs  -0r sed -i 's/anything/something/g'

Last edited by average_user; 10-18-2018 at 03:39 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: GUI ways to view and edit big text files LXer Syndicated Linux News 0 07-30-2018 08:13 PM
Help with script to batch edit text files OnoTadaki Programming 5 10-15-2007 02:44 PM
cant edit text files properly in vi and fluxbox soldan Linux - Newbie 5 11-18-2006 12:02 PM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
How do you edit text files in termnial? everydayparadis Linux - Newbie 10 07-08-2004 01:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration