choose random text between constant string.. using awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting choose random text between constant string.. using awk?
# 1  
Old 02-14-2010
choose random text between constant string.. using awk?

Hallo
I have maybe a little bit advanced request....
I need to choose one random part betwen %....

so i have this..
Code:
%
text1 text1 text1
text1 text1 text1
text1 text1 text1
%
text2 text2
text2 text2 text2
%
text3 text3 text3
tetx3
%

this choose text between %
Code:
awk ' /%/ {flag=1;next} /%/{flag=0} flag { print }' file

so i get
Code:
text1 text1 text1
text1 text1 text1
text1 text1 text1
text2 text2
text2 text2 text2
text3 text3 text3
tetx3

I NEED JUST ONE PART

Code:
text1 text1 text1
 text1 text1 text1
 text1 text1 text1

or
Code:
text2 text2
 text2 text2 text2

or
Code:
text3 text3 text3
 tetx3

Any help would be appreciate....

Last edited by Scott; 02-14-2010 at 06:43 AM.. Reason: Please use code tags
# 2  
Old 02-14-2010
Your solution is equivalent to:
Code:
awk '/%/{next}{print}' infile

This should print only the first segment:
Code:
awk '/%/{p++;next}p==1{print}' infile

This the second:
Code:
awk '/%/{p++;next}p==2{print}' infile

etcetera

Last edited by Scrutinizer; 02-14-2010 at 07:00 AM..
# 3  
Old 02-14-2010
Thank you very much Scrutinizer.... this helped me a lot...

Code:
awk '/%/{p++;next}p==1{print}' infile


Last edited by Scott; 02-14-2010 at 07:10 AM.. Reason: Code tags
# 4  
Old 02-14-2010
try the next code uses rand function

Code:
awk 'BEGIN{ RS="%"  ;srand() } { line[NR] = $0 } END{ print line[randint(NR)] } function randint(n) {return int(n * rand()) + 1}' infile

Or

Code:
awk 'BEGIN{ RS="%\n"  ;srand() } { line[NR] = $0 } END{ print line[int((NR* rand()) + 1)]}'  infile


Last edited by ironmask2004; 02-14-2010 at 07:43 AM..
# 5  
Old 02-14-2010
Very nice ironmask2004 it is great too ....

I have just finished it with the previous solution
Code:
vtip=$RANDOM
let "vtip=1+(vtip%2125)"
awk -v cislo=$vtip '/%/{p++;next}p==cislo' vtipy.txt


Last edited by Scott; 02-14-2010 at 07:48 AM.. Reason: Code tags, please...
# 6  
Old 02-14-2010
Quote:
Originally Posted by sandwich
Very nice ironmask2004 it is great too ....

I have just finished it with the previous solution
vtip=$RANDOM
let "vtip=1+(vtip%2125)"
awk -v cislo=$vtip '/%/{p++;next}p==cislo' vtipy.txt

but you need to know how muche recoreds you have insaide the fill to generate a random number betwwen 1 and NUM Of Rec !!!
# 7  
Old 02-14-2010
yes good question i have used

Code:
grep -c "^[%]" vtipy.txt

and the number was 2126

Code:
vtip=$RANDOM
let "vtip=1+(vtip%2125)"   #thats why here is 2125 it is modulo
awk -v cislo=$vtip '/%/{p++;next}p==cislo' vtipy.txt


Last edited by Scott; 02-14-2010 at 08:02 AM.. Reason: Code tags, PLEASE!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script that will random choose an IP address

Hi, I need to write a bash script that will random choose and login into these below ip addresses. 192.168.116.130 192.168.116.131 192.168.116.132 192.168.116.133 I'm new into scripting and I need to enhance my logic. Below is what i did ... (4 Replies)
Discussion started by: Milon
4 Replies

2. Programming

String Constant C

I wonder string constant exists permanently or temporary. For example, printf("hello, world"); the function printf access to it is through a pointer. Does it mean storage is allocated for the string constant to exist permanently in memory? :confused: (4 Replies)
Discussion started by: kris26
4 Replies

3. Shell Programming and Scripting

How to solve awk: line 1: runaway string constant error?

Hi All ! I am just trying to print bash variable in awk statement as string here is my script n=1 for file in `ls *.tk |sort -t"-" -k2n,2`; do ak=`(awk 'FNR=='$n'{print $0}' res.dat)` awk '{print "'$ak'",$0}' OFS="\t" $file n=$((n+1)) unset ak doneI am getting following error awk:... (7 Replies)
Discussion started by: Akshay Hegde
7 Replies

4. Shell Programming and Scripting

how to choose random columns

Hello! Can anybody suggest about the fastest way of extracting "n" random columns from a very large file (tab separated) having thousands of columns, where n can be any specified number. Thanks! (10 Replies)
Discussion started by: mira
10 Replies

5. Shell Programming and Scripting

Extract text between two specified "constant" texts using awk

Hi All, From the title you may know that this question has been asked several times and I have done lot of Googling on this. I have a Wikipedia dump file in XML format. All the contents are in one XML file i.e. all different topics have been put in one XML file. Now I need to separate them and... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

using awk to extract text between two constant strings

Hi, I have a file from which i need to extract data between two constant strings. The data looks like this : Line 1 SUN> read db @cmpd unit 60 Line 2 Parameter: CMPD -> "C00071" Line 3 Line 4 SUN> generate Line 5 tabint>ERROR: (Variable data) The data i need to extract is... (11 Replies)
Discussion started by: mjoshi
11 Replies

7. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

8. Programming

'strlen' of a constant string

In a declaration, I have: const char comment_begin = "<!--"; const char comment_end = "-->"; const int comment_begin_len = strlen(comment_begin); const int comment_end_len = strlen(comment_end); When I compile, I get the warnings: emhttpc.c:64: warning: initializer element is not... (10 Replies)
Discussion started by: cleopard
10 Replies
Login or Register to Ask a Question