LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-25-2011, 10:37 AM   #1
timdvtemp
Member
 
Registered: Feb 2011
Distribution: Redhat/Fedora
Posts: 39

Rep: Reputation: 3
Printing Changes made using find/sed combo


...Running this command:

Code:
 find ./ -type f  -exec sed -i "s:$FIND_SNR_STR1:$FIND_SNR_STR2:" {} \;


I want to see the changes made one by one as they happen (ie showing the entire line the change is being made on), rather than a silent screen. Looking at the man page didn't help too much, as I am passing the output to sed. Any thoughts?
 
Old 02-25-2011, 10:53 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,818

Rep: Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005
Oops, got too far ahead of myself.

Last edited by corp769; 02-25-2011 at 10:55 AM.
 
Old 02-25-2011, 10:57 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
I'm afraid it's not possible, since the -i option writes any output into the file itself. Anyway you can perform multiple actions through -exec, so that something like this should work:
Code:
find . -type f -exec sed -n "/$FIND_SNR_STR1/p" {} \; -exec sed -i "s:$FIND_SNR_STR1:$FIND_SNR_STR2:" {} \;
 
Old 02-25-2011, 10:59 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,818

Rep: Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005
Quote:
Originally Posted by colucix View Post
I'm afraid it's not possible, since the -i option writes any output into the file itself. Anyway you can perform multiple actions through -exec, so that something like this should work:
Code:
find . -type f -exec sed -n "/$FIND_SNR_STR1/p" {} \; -exec sed -i "s:$FIND_SNR_STR1:$FIND_SNR_STR2:" {} \;
I was thinking of something along that line. Question though.... Doesn't find have anything about being more verbose? I could have sworn it did.
 
Old 02-25-2011, 12:29 PM   #5
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,672

Rep: Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640Reputation: 1640
Find

'Find' may not be able to display what you want. Remember: 'sed' is doing the grunt-work here.
 
Old 02-25-2011, 12:44 PM   #6
timdvtemp
Member
 
Registered: Feb 2011
Distribution: Redhat/Fedora
Posts: 39

Original Poster
Rep: Reputation: 3
Thanks colucix.. it worked, but I wanted the location and filename too. What I ended up doing was throwing it into a script, and ran a grep command.

Quote:
I was thinking of something along that line. Question though.... Doesn't find have anything about being more verbose? I could have sworn it did.
It does, but as colucix pointed out, the information is being processed, not printed. Also, I wanted the path to the file displayed, as well as the line it was on.
 
Old 02-25-2011, 01:14 PM   #7
timdvtemp
Member
 
Registered: Feb 2011
Distribution: Redhat/Fedora
Posts: 39

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by colucix View Post
I'm afraid it's not possible, since the -i option writes any output into the file itself. Anyway you can perform multiple actions through -exec, so that something like this should work:
Code:
find . -type f -exec sed -n "/$FIND_SNR_STR1/p" {} \; -exec sed -i "s:$FIND_SNR_STR1:$FIND_SNR_STR2:" {} \;
The problem with
Code:
find . -type f -exec sed -n "/$FIND_SNR_STR1/p" {} \;
Is that I am using this partly for filenames, so the slashes screw things up. I tried replacing the slashes with another delimiter (@,;,:, etc), but it still doesn't want to work.

So close to where I need to be!
 
Old 02-25-2011, 03:52 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by timdvtemp View Post
The problem with
Code:
find . -type f -exec sed -n "/$FIND_SNR_STR1/p" {} \;
Is that I am using this partly for filenames, so the slashes screw things up. I tried replacing the slashes with another delimiter (@,;,:, etc), but it still doesn't want to work.
For regular expressions as sed addresses you can use any other delimiter in place of /, but the first character must be a backslash, e.g.
Code:
find . -type f -exec sed -n "\:$FIND_SNR_STR1:p" {} \;
The character after the backslash will be the delimiter of the regular expression.

Right now I cannot find a solution to print out the filename along with the relevant line, since, as far as I know, sed has no facilities for doing that. Maybe - as you already pointed out - involving grep might be a solution.
 
Old 02-25-2011, 04:05 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
What about this?
Code:
find . -type f -exec grep -H "$FIND_SNR_STR1" {} \; -exec sed -i "s:$FIND_SNR_STR1:$FIND_SNR_STR2:" {} \;
 
1 members found this post helpful.
Old 03-03-2011, 08:34 AM   #10
__Vano
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 1
I always do like this:

Code:
for f in <list by whatever means>; do echo -e "\n$f"; sed -i.old "s:$FIND_SNR_STR1:$FIND_SNR_STR2:" $f; diff $f{.old,}; done
Then I examine the output to see if the changes are correct and move the corresponding .old file back with
Code:
mv <file>{.old,}
for cases when they are not (the file name is just there for convenient copypasting with mouse). Finally, i delete the remaining .old files with
Code:
for f in <original list>; do rm $f.old; done
ignoring 'no such file' errors.

Last edited by __Vano; 03-03-2011 at 08:38 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find URL in Debian package index via awk/sed (=find a line, then search from there) maddes.b Linux - Software 11 06-28-2013 07:37 AM
Sed and printing only part of a line jrdioko Linux - Software 12 05-17-2012 07:15 PM
Combo,combo box and combo entry box diff santhosh.linux Programming 4 02-27-2006 12:37 AM
can u find out the I.P address a yahoo i.d was made at??? peterpeter General 3 08-06-2003 07:25 AM
where to find a ready-made firewall package ? jamaso Linux - General 3 03-09-2002 05:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:56 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