Extract text N times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract text N times
# 1  
Old 09-01-2015
Extract text N times

I have a input file inputfile which looks like below

Code:
[MESSAGE BEGIN]
                                          <hdr:AddressLine>string1</soi:AddressLine>
                                          <hdr:AddressLine>string1</soi:AddressLine>
                                          <hdr:AddressLine>string1</soi:AddressLine>
                                          <hdr:AddressLine>string1</soi:AddressLine>
[MESSAGE END]

[MESSAGE BEGIN]
                                          <hdr:AddressLine>string2</soi:AddressLine>
                                          <hdr:AddressLine>string2</soi:AddressLine>
                                          <hdr:AddressLine>string2</soi:AddressLine>
                                          <hdr:AddressLine>string2</soi:AddressLine>
[MESSAGE END]

[MESSAGE BEGIN]
                                          <hdr:AddressLine>string3</soi:AddressLine>
                                          <hdr:AddressLine>string3</soi:AddressLine>
                                          <hdr:AddressLine>string3</soi:AddressLine>
                                          <hdr:AddressLine>string3</soi:AddressLine>
[MESSAGE END]

......
.......
Can repeat any number of times
[MESSAGE BEGIN]
                                          <hdr:AddressLine>stringX</soi:AddressLine>
                                          <hdr:AddressLine>stringX</soi:AddressLine>
                                          <hdr:AddressLine>stringX</soi:AddressLine>
                                          <hdr:AddressLine>stringX</soi:AddressLine>
[MESSAGE END]


I also have a file called param which looks like below

Code:
xxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyy
NUMBER_OF_TIMES=8
zzzzzzzzzzzzzzzzzzzzz

I am trying to extract All lines from inputfile between [MESSAGE BEGIN] and [MESSAGE END] --inclusive (i.e want the [MESSAGE BEGIN] and [MESSAGE END] ) and would want this for the FIRST N lines.....where N would be the number from the param file which comes right after the fixed string NUMBER_OF_TIMES ( in the above example of param file -- N=8 )

What would be the best approach to go about this ?

Should I be using "sed" ?

Am limited to ksh in Linux

Last edited by Don Cragun; 09-01-2015 at 11:16 PM.. Reason: Add CODE and ICODE tags. Add I tags.
# 2  
Old 09-01-2015
Is this a homework assignment? Homework must be posted in the Homework & Coursework Questions forum and must include a completely filled out homework template.

If this is not homework, please explain what real-life problem this request is intended to solve. And, note that there is a big difference between number of "lines" in a file and a number of [MESSAGE BEGIN] to [MESSAGE END] blocks of text. It isn't at all clear whether you are talking about "lines" or "blocks".

And, no... sed is not the tool to use for this job. (At least not for the complete job. It could be used for one or two parts of the job, but there are better tools for grabbing a number out of one file and processing another file based on that number.)
# 3  
Old 09-01-2015
thanks for the reply. when referring to N number of times , i mean N number of BLOCKS and not N number of lines.

This is for a situation where I have a inputfile with thousands of XML blocks, all these "different" XML blocks are enclosed within the [MESSAGE BEGIN] and [MESSAGE END] .

Now I would need to create a much smaller "subset" file outputfile which does not need ALL the XML blocks but N number of blocks between [MESSAGE BEGIN] and [MESSAGE END] (inclusive )

Doing this manually --i.e getting rid of thousands of blocks manually takes a lot of time -- plus its a very crude method. Hence am trying to use Shell Scripting to achieve this.
# 4  
Old 09-02-2015
I would try something like:
Code:
#!/bin/ksh
awk -F'=' '
FNR == NR {
	if($1 == "NUMBER_OF_TIMES")
		N = $2
	next
}
N
/[[]MESSAGE END[]]/ {
	if(--N <= 0)
		exit
}' param inputfile

# 5  
Old 09-02-2015
thanks Don ! this worked and was exactly what I wanted

Can you please suggest some links to understand AWK better
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract start/end times from log file to CSV file?

Hi, I have a log file (log.txt) that which contains lines of date/time. I need to create a script to extract a CSV file (out.csv) that gets all the sequential times (with only 1 minute difference) together by stating the start time and end time of this period. Sample log file (log.txt) ... (7 Replies)
Discussion started by: Mr.Zizo
7 Replies

2. Shell Programming and Scripting

[SOLVED] Extract IP from text

Dear friends, I have a file with the text: href="/archive/ip=x.x.x.x">M</a></td> I want to extract just the IP number. I tried sed, awk but i didn't succeed. Any idea? Thank you Please view this code tag video for how to use code tags when posting code and data. (3 Replies)
Discussion started by: tts0
3 Replies

3. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

4. Shell Programming and Scripting

Extract text between two strings

Hi I have something like this: EXAMPLE 1 CREATE UNIQUE INDEX "STRING_1"."STRING_2" ON "BOSNI_CAB_EVENTO" ("CD_EVENTO" , "CD_EJECUCION" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 5242880 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "DB1000_INDICES_512K"... (4 Replies)
Discussion started by: chrispaz81
4 Replies

5. Shell Programming and Scripting

Append some text to a file multiple times

Hi, I have a text file like Version=abc Tab=1 URL GOTO=www.abc.com/board=1 some text... I want to run a loop x no of times and append to the text file above text but URL GOTO should be www.abc.com/board=2 then 3,4...etc till x. Kindly help (2 Replies)
Discussion started by: krabu
2 Replies

6. Shell Programming and Scripting

Extract value from a text

Hi all, my problem is extract a value from a text, i mean, I have this text: > ala Nr of active alarms are: 16 ================================================================================================ Sever Specific Problem Cause Mo-Reference... (15 Replies)
Discussion started by: marimovo
15 Replies

7. Shell Programming and Scripting

Extract particular text

I executed a following sed command => echo "a/b/c/d/e/f/g/h" | sed 's/\/*$//g' a/b/c/d/e/f/g Now what if I want to extract "g" from "a/b/c/d/e/f/g/h" . That is second last string using SED. (4 Replies)
Discussion started by: Shell_Learner
4 Replies

8. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. UNIX for Advanced & Expert Users

extract text b/w two delimiters

I have an input file which looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (6 Replies)
Discussion started by: dowsed4u8
6 Replies
Login or Register to Ask a Question