Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print on single line after comparing the no. and GREP

Status
Not open for further replies.

RVSachin

Technical User
Nov 12, 2001
77
IN
Is there a easy way to do the following in (Korn) shell?

My text file contains lines similar to the following, each on a different line:
Code:
	#  reg1.inst_abc[0].a unmatched
	#  reg1.inst_abc[1].a unmatched
	....
	....
	.....
	#  reg1.inst_abc[n].a unmatched

	# inst_abc[0].a unmatched
	# inst_abc[1].a unmatched
	...
	...
	...
	# inst_abc[n].a unmatched
[highlight]Note that n can be any no. and need not be in a sequence.[/highlight]


I want these lines sorted out and [highlight]printed on a single line [/highlight]such that I print the following lines into a file:
reg1.inst_abc[0].a matches with inst_abc[0].a
The no. 'n' on left should match the one on the right side.

It will be of great help if the commands used to achieve this is explained.

Thanks,

RV
 
Hi there,
I don't think there is an "easy way to do the following in (Korn) shell?", otherwise someone would have posted it by now. There are a number of questions that need answers, before a complete solution can be found, eg:
Will there always be matching pairs of entries ?
What happens if there are not ?
What is the range of values for the no. 'n' ?
Although the numbers can be out of sequence, are there any missing? and is that an error if there are ?

So without making too many assumtions, how about the following ideas,

First idea:
- cut the source file in half by grep'ing into 2 files the different lines (reg1.inst_abc[n].a into one file and inst_abc[n].a into the other)
- sort the 2 files (beware that if you use a character sort, then when n is 21 to 29 those lines will appear between the lines when n is 2 and n is 3)
- read the files, or paste the files (process them in some way) to produce the desired output
- check that all lines from the source file are in the output file, any non-shows would be an error.

Second idea:
- grep the file for an incrementing count in square brackets (eg grep -F [${count}] <file>) in a while loop (till count greater than the no. 'n')
- check that 2 lines are present and output the combination of these 2 lines in the desired format to the output file
- as a final check, test that the important bit of each line of the source file (ie reg1.inst_abc[n].a and inst_abc[n].a) are present in the output file, any non-shows would be an error.
This idea automatically sorts the lines into true numeric order. This fulfills your requirement "I want these lines sorted out and printed on a single line"

Third idea:
- process the source file line by line (read while do loop) and save the important part of the line (ie reg1.inst_abc[n].a) in an array variable (eg array[n])
- when the second item is read (ie inst_abc[n].a ), extract the no. 'n', get the contents of array[n] and output the two items in the desired format to the output file and empty the array variable
- at the end, any array variables that contain values must be errors so print them in a list.

Three ideas for you to peruse and develop further into a script to do the job.

I hope that helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top