LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 09-25-2008, 09:27 PM   #1
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
KFileReplace Issues


I'm using KDE 3.5.9 in Slackware 12.1.

I would like to know how to configure KFileReplace to simply delete a block of text --- to replace something with nothing. This seems so simple of an idea --- even the most basic of text editors do this.

I wanted to delete a multiline block of text that is similar within a few hundred files. By deleting, I mean searching but replacing the block of text with nothing.

Although the size of each multiline block was the same, the text within the blocks was not exactly the same in each document. Therefore I needed to use regular expressions to represent the text between known strings of text.

Eventually I discovered the ".*" wild card to create a regular expression search string that KFileReplace accepted. I also had to use the backslash to escape almost every non alpha-numeric character. Fortunately for me, the line of text preceding the block of text I wanted to delete was the same in each document. Therefore I used that line as my Replace string.

Therein lies the conundrum. If I left the KFileReplace Replace option empty, implying replace something with nothing, then KFileReplace seems limited to performing only a search. The Replace button is ghosted if the Replace option remains empty. Perhaps there is a special string I need to enter in the Replace text box that represents "replace with nothing"?

The next time I need to perform a global deletion like this I might not be so fortunate to have at least one line of text that is the same in each document.

KFileReplace has much potential but seems terribly neglected by the KDE developers. The associated KFileReplace handbook has not been updated in four years and contains no examples. I have almost no clue about the full regex syntax KFileReplace uses. I got lucky this time using the ".*" wild card expression to represent text between two known strings of text.

In addition to understanding KFileReplace better, I am open to any command line tricks anyone wants to offer.

As always, thanks.
 
Old 09-26-2008, 03:31 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I don't know KFileReplace, but I know perl or sed are goods tool for replace text in files
Could you post a sample of your text file and which lines of text you want to remove ?
 
Old 09-28-2008, 04:15 PM   #3
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Thanks for offering. As I mentioned, I resolved my original problem with KFileReplace. I still do not know the proper syntax KFileReplace is seeking --- I just got lucky this round.

Regarding sample code, I'd have to pull the files from backups to resurrect what I already solved. However, my primary issue is how to traverse newlines (multiple lines). To my understanding, sed is intended for single lines, not multiple lines. Therefore, can sed be used to traverse multiple lines? If so, then consider using the text in this post as an example. I really would like to learn this trick.

Another related stumper is passing variables in shell scripts to sed. The best I have learned is that this is not possible, at least not directly. Are there any tricks to use variables in a search-and-replace sequence? I found an example of using awk:

awk '{gsub(a,b);print}' a="$VAR1" b="$VAR2" $FILE1 > $FILE2
mv -f $FILE2 $FILE1


Can something similar be performed with sed?
 
Old 09-28-2008, 04:32 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I am not expert in awk, far from that but I understand this code as:

For each line that matches $VAR1, replace $VAR1 with $VAR2 and print the modified line to $FILE2, then save $FILE2 back to $FILE1

Now an equivalent with sed would be:
sed -i -e 's/$VAR1/$VAR2/g' $FILE1

Last edited by keefaz; 09-28-2008 at 04:34 PM.
 
Old 09-28-2008, 09:24 PM   #5
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Quote:
Now an equivalent with sed would be:
sed -i -e 's/$VAR1/$VAR2/g' $FILE1
That is what I thought too, but sed will not accept variables.
 
Old 09-29-2008, 03:00 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe this is because the single quotes were used ? Try with double quotes instead:
sed -i -e "s/$VAR1/$VAR2/g" $FILE1
(I think the shell will replace the variables with values before sed is called)
 
Old 09-29-2008, 06:17 PM   #7
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
I created a test text document. I then wrote a short test script. I first ran the grep command with color highlighting to verify the searched text existed (two places). I ran one instance of sed with single marks and the next line with double. I did not perform inline replacement and only I displayed the results on screen.

Double quotation marks works with variables. Single quotation marks do not.

I next modified the script to perform the replacement inline.

Same result.

Huh.

Go figure. Another lesson learned in the book called life.

I suppose I have to learn why double marks work and single do not. Probably has something to do with how bash parses, escapes, and interprets everything.

I also learned that the first variable must contain back slashes to escape certain characters but the replacement variable does not. Perhaps whatever I was trying recently I did not properly use back slashes.

Seems to work and thanks!

Next, back to my original problem: how to use sed to traverse multiple lines? That is, replace something with nothing. I found a mini how-to that explained this syntax:

sed "/$VAR1/,/$VAR2/d" ~/test.txt

I defined $VAR1 as the first line I wanted to delete. I defined $VAR2 as the last line I wanted to deleted. Worked great but because the document contained empty line breaks to separate paragraphs, I was left with a double blank line.

I then found the following example to delete double blank lines:

sed "/./,/^$/!d"

I then tried the following:

sed "/$VAR1/,/$VAR2/d" ~/test.txt | sed "/./,/^$/!d"

That worked great but the output was to stdout not actual inline replacement. I next tried:

sed "/$VAR1/,/$VAR2/d" ~/test.txt | sed "/./,/^$/!d" > ~/test2.txt
cat ~/test2.txt


Worked great again. Next I tried:

sed -i "/$VAR1/,/$VAR2/d" ~/test.txt | sed -i "/./,/^$/!d" ~/test.txt
cat ~/test.txt


And that seems to be the successful end of this particular sed lesson for me.

I still think KFileReplace has much potential and is sadly neglected. Yet I hope this thread helps somebody else!

Thanks.
 
Old 09-29-2008, 06:36 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Nice sed examples Thanks for sharing!

I think you could short a little the last two examples if you want, like:

sed -i -e "/$VAR1/,/$VAR2/d" -e "/./,/^$/!d" ~/test.txt
cat ~/test.txt

(not sure as I haven't the test.txt for testing it)

Last edited by keefaz; 09-29-2008 at 06:37 PM.
 
Old 09-30-2008, 12:30 AM   #9
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Quote:
sed -i -e "/$VAR1/,/$VAR2/d" -e "/./,/^$/!d" ~/test.txt
Yup, that works!
 
  


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
ATI Ain in Wonder 8500DV LINUX LIVE boot issues (and TV issues) Flashmahn Linux - Hardware 1 03-09-2009 06:40 AM
Dell m1210 Linux issues (having issues getting network working under Ubuntu) thebert Linux - Newbie 4 03-14-2008 03:47 PM
KFileReplace error (is that blocks previews in KDE?) billgnu Linux - Newbie 2 11-29-2005 11:59 AM
New to linux, so so lost, auto mounting issues, permissions issues slowhand22 Linux - Newbie 2 02-10-2005 09:41 AM
Konqueror KFileReplace trouble ramzai Linux - Newbie 1 01-15-2005 12:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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