sed random \n for "n" range of character occurrences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed random \n for "n" range of character occurrences
# 1  
Old 07-20-2016
sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that?

In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in the file.

Thanks in advance for help.
# 2  
Old 07-20-2016
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum with posts following special homework rules.

If you did post homework in the main forums, please repost your question in the proper forum as described above.

If this is not homework please explain:
  1. why you would want to do this to a normal text file,
  2. why you think sed would be an appropriate tool to calculate random numbers,
  3. what operating system you're using,
  4. what shell you're using, and
  5. what you have tried to solve this problem on your own?
# 3  
Old 07-20-2016
Hi, thanks, it's not homework.

1) because I like to experiment with word lists and grammar.
2) because sed has a random function, and sed examples from forums have helped me so far.
3) Linux Mint.
4) Bash shell.
5) Something like this:
sed '(RANDOM(5~10)s/$/\n/g)'< in.txt > out.txt
# 4  
Old 07-20-2016
I'm not aware of a random function in sed; which version do you use?

And, while your code sample (ignoring the RANDOM stuff) will add a <NL> char to some EOL (end of line), in post#1 you specify that dots should be replaced. Do I guess correctly that dots are not necessarily at EOLs?

Would a non-sed solution be acceptable?
# 5  
Old 07-20-2016
Hi, thanks for responding,
Code:
sed (GNU sed) 4.2.2

No, dots are not replaced. The idea is to put random paragraph breaks in a chunk of text, every 5 - 10 sentences, for example.

Yes I'm open to any other method.

Last edited by p1ne; 07-20-2016 at 06:32 PM..
# 6  
Old 07-20-2016
Quote:
Originally Posted by p1ne
Hi, thanks for responding,
Code:
sed (GNU sed) 4.2.2

No, dots are not replaced. The idea is to put random paragraph breaks in a chunk of text, every 5 - 10 sentences, for example.

Yes I'm open to any other method.
Generally speaking: you have two problems to solve if you are using sed for this: the first is to generate a random number. Probably it will take a big lot of effort to do so (although it should in principle be possible because sed is a turing-complete language).

The second is that you are not working context-free: this is always a hassle because sed has little to offer save for a powerful regex-engine. Such problems are generally more easily solved in awk becuse this offers a programming language in addition to the regex engine.

I hope this helps.

bakunin
# 7  
Old 07-20-2016
Please show us some sample input (i.e., the contents of the file in.txt) and the output you are hoping to have produced (in the file out.txt) and the output that you did get (both diagnostic messages produced, if there were any, and the output stored in out.txt) when you ran the command:
Code:
sed '(RANDOM(5~10)s/$/\n/g)'< in.txt > out.txt

on your system.

Running that command on my system produces the diagnostic message:
Code:
sed: 1: "(RANDOM(5~10)s/$/\n/g)": invalid command code (

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing character "|" in given character range

Hi I am having file : 1|2443094 |FUNG SIU TO |CLEMENT 2|2443095 |FUNG KIL FO |REMENT This file contains only 3 fields delimeted by "|". Last field is a decsription filed and it contains character "|". Due to this my output if breaking in 4 fields. I... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

3. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

4. Shell Programming and Scripting

sed escape character for comment string "/*"

Good afternoon all, I'm hoping my newbie question can help bolster someone's street_cred.sh today. I'm trying to "fingerprint" SQL on its way into the rdbms for a benchmarking process (so I can tie the resource allocation back to the process more precisely). To do this, I'm essentially... (4 Replies)
Discussion started by: toeharp
4 Replies

5. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

10. Shell Programming and Scripting

removing the "\" and "\n" character using sed or tr

Hi All, I'm trying to write a ksh script to parse a file. When the "\" character is encountered, it should be removed and the next line should be concatenated with the current line. For example... this is a test line #1\ should be concatenated with line #2\ and line number 3 when this... (3 Replies)
Discussion started by: newbie_coder
3 Replies
Login or Register to Ask a Question