| | |
Quick question about text processing
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hello everyone. I need to write a Bash script to remove blocks of text from a given file. The idea is, the text to be removed will be marked by appearing between certain delimiter characters i.e.
Could someone tell me the kind of commands I need to research to do stuff with text like this? I can work out the rest. Any help appriciated.
Steven.
Shell Scripting Syntax (Toggle Plain Text)
Here's some text and ~this bit gets removed~, where tilda is the delimiter.
Could someone tell me the kind of commands I need to research to do stuff with text like this? I can work out the rest. Any help appriciated.
Steven.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
You need to use sed, and you have to learn about regular expressions.
based on your data a very specific (not generalized) solution is:
based on your data a very specific (not generalized) solution is:
Shell Scripting Syntax (Toggle Plain Text)
$> echo "Here's some text and ~this bit gets removed~, where tilda is the delimiter." |read var $> echo $var Here's some text and ~this bit gets removed~, where tilda is the delimiter. $> echo $var | sed 's/~[A-Za-z ]*.~//1' Here's some text and , where tilda is the delimiter.
•
•
Join Date: Apr 2006
Posts: 149
Reputation:
Solved Threads: 40
•
•
•
•
Could someone tell me the kind of commands I need to research to do stuff with text like this? I can work out the rest. Any help appriciated.
Shell Scripting Syntax (Toggle Plain Text)
indices=[] #define array list to keep "~" indexes s = "Here's some text and ~this bit gets removed~, where tilda is the delimiter." for num,ch in enumerate(s): if ch == "~": indices.append(num) s = list(s) #turn s into a list, so we can make changes to it del s[indices[0]: indices[1]+1] print ''join(s)
•
•
Join Date: Jan 2007
Posts: 10
Reputation:
Solved Threads: 0
If your needs are more complicated, you can use fancier tools like perl or python, or if your needs are fairly simple, sed will probably fit your needs just fine.
Take your example, you have a file "input.txt" with the text you posted, and you want to strip of it the un-wanted text. You can do this with a single line of sed command:
And you will be left with the "clean" version of your input.txt, with the text removed. Look up "regular expression" if you do a lot of pattern matching, it will help you a lot.
-Josh
www.qbangsolutions.com
Take your example, you have a file "input.txt" with the text you posted, and you want to strip of it the un-wanted text. You can do this with a single line of sed command:
Shell Scripting Syntax (Toggle Plain Text)
$ sed -ie 's/~[A-Za-z ]*.~//g' input.txt
-Josh
www.qbangsolutions.com
![]() |
Similar Threads
- A quick question about "rank" (DaniWeb Community Feedback)
- Quick Question: Is J# the same thing as Java? (Java)
- A quick question (Game Development)
- quick question (C++)
- text processing? (Java)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
Other Threads in the Shell Scripting Forum
- Previous Thread: Spare few minutes
- Next Thread: Big Favour!!!
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





