Get values block by block in same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get values block by block in same file
# 8  
Old 06-09-2014
Code:
awk -F'/' '
        
        # Function to validate number
	function isnum(x){return(x==x+0)}

	/P1/,/P2/{
			# Found start increment i reset variables go to next line
			if(/P1/){
				   ++i 
				   s1 = s2 ="" 
				   next
				}

			# Found end validate variable and print go to next line
			if(/P2/){
				 printf "%s %s %d %s\n%s %s %d %s\n\n", "10,9","Value :",i,isnum(s1)?s1:"NULL", \
					 "11,9","Value :",i,isnum(s2)?s2:"NULL"
				  next 
				}

			# Here we search first pattern
			if(!s1 && /10,9:.*/)
				{
					s1 = $NF
				}

			# Here we search second pattern
			if(!s2 && /11,9:.*/)
				{
					s2 = $NF
				}
		  }
	    ' file

Resulting
Code:
10,9 Value : 1 18013013582
11,9 Value : 1 310410532169026

10,9 Value : 2 13233361170
11,9 Value : 2 310410645017141

10,9 Value : 3 NULL
11,9 Value : 3 31041064501714

Input
Code:
$ cat file
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
12,18:15/013329002130500
P2
1,64:1
,70:H
,97:1
2,1:20140518031625
,2:18000
P3
42,3:1.
,4:3
,300:1.
43,3:1.

P1
11,9:15/310410645017141
,10:1
,167:0
,487:5/E.164
11,9:15/310410645017141
10,9:11/13233361170
P2
1,64:1
,70:H
,97:1

P1
11,9:15/310410645017141
,10:1
,167:0
P2
1,64:1
,70:H
,97:1
2,1:20140518031625
,2:18000
P3
42,3:1.
,4:3
,300:1.
43,3:1.

# 9  
Old 06-09-2014
Actually, its my mistake I wasn't clear. My example quoted not correct.
Here is the correct input.

Code:
P1
10,9:11/18013013582
,10:1
,167:0
,487:5/E.164
11,9:15/310410532169026
,10:60
,167:0
12,18:15/013329002130500
P2
1,64:1
,70:H
,97:1
2,1:20140518031625
,2:18000
P3
42,3:1.
,4:3
,300:1.
43,3:1.

P1
11,9:15/310410645017141
,10:1
,167:0
,487:5/E.164
10,9:11/13233361170
P2
1,64:1
,70:H
,97:1

P1
11,9:15/310410645017141
,10:1
,167:0
P2
1,64:1
,70:H
,97:1
2,1:20140518031625
,2:18000
P3
42,3:1.
,4:3
,300:1.
43,3:1.

UPDATE: I don't only want to find 10,9 value. I need a script wherein I can find multiple values also from each P1-P2 block.

Inside a P1-P2 block value will occur only once if at all it occurs.

For eg, in above Input I want values for 10,9 as well as 11,9.

Output Required

Code:
10,9 Value1:18013013582
11,9 Value1:310410532169026

10,9 Value2:13233361170
11,9 Value2:310410645017141

10,9 Value3:NULL
11,9 Value3:310410645017141

I hope I am clear about my problem. Thanks in advance. Smilie
# 10  
Old 06-09-2014
I can see you edited your previous post, I just posted answer in post #8
# 11  
Old 06-09-2014
[QUOTE=Akshay Hegde;302905096]
Code:
awk -F'/' '
        
        # Function to validate number
	function isnum(x){return(x==x+0)}

	/P1/,/P2/{
			# Found start increment i reset variables go to next line
			if(/P1/){
				   ++i 
				   s1 = s2 ="" 
				   next
				}

			# Found end validate variable and print go to next line
			if(/P2/){
				 printf "%s %s %d %s\n%s %s %d %s\n\n", "10,9","Value :",i,isnum(s1)?s1:"NULL", \
					 "11,9","Value :",i,isnum(s2)?s2:"NULL"
				  next 
				}

			# Here we search first pattern
			if(!s1 && /10,9:.*/)
				{
					s1 = $NF
				}

			# Here we search second pattern
			if(!s2 && /11,9:.*/)
				{
					s2 = $NF
				}
		  }
	    ' file

Resulting
Code:
10,9 Value : 1 18013013582
11,9 Value : 1 310410532169026

10,9 Value : 2 13233361170
11,9 Value : 2 310410645017141

10,9 Value : 3 NULL
11,9 Value : 3 31041064501714


Thats Perfect !! Smilie
Helps a lot. I can now figure out rest of the code. Thanks a lot.
Will surely update if any help needed.Smilie
# 12  
Old 06-09-2014
@garvit184
Are you learning anything? Are you trying to modify what Akshay Hegde is posting, even commented for you to understand?

You keep changing your mind and input. It would be profitable for you to try on your own a bit and show some effort.
# 13  
Old 06-09-2014
@aia: I agree with you. Actually I am working on something and the part I needed help is just this reporting part. My whole project is mostly in basic shell and AWK is like a black area for me.

Akshay's comments help a lot in understanding the logic used and will help in learning Smilie
# 14  
Old 06-09-2014
You might also want to try this slightly simpler awk script that seems to more closely match your requested output format:
Code:
awk -F/ '
$1 == "P1" {
	s1 = s2 = "NULL"
	n++
}
/^10,9:/ {
	s1 = $2
}
/^11,9:/ {
	s2 = $2
}
$1 == "P2" {
	printf("%s10,9 Value%d:%s\n11,9 Value%d:%s\n", n == 1 ? "" : "\n",
		n, s1, n, s2)
}' file

which (when given the sample input shown in message #9 in this thread) produces:
Code:
10,9 Value1:18013013582
11,9 Value1:310410532169026

10,9 Value2:13233361170
11,9 Value2:310410645017141

10,9 Value3:NULL
11,9 Value3:310410645017141

If you understand Akshay's script, I assume you will also understand this script. Let me know if there is anything in this script that you don't understand.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

2. UNIX for Dummies Questions & Answers

Add a block of code at the end of a specific block

I need to search for a block with the starting pattern say "tabId": "table_1", and ending pattern say "]" and then add a few lines before "]" "block1":"block_111" "tabId": "table_1", "title":"My title" ..... .... }] how do I achieve it using awk and sed. Thanks, Lakshmi (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

3. Shell Programming and Scripting

Formatting and replacing according to block values

please consider the following file, there are repeated blocks of m values, and nested s values within. there are 2 columns (cols 3 and 4)associated with each m,s combination. All s1 rows must get a value of a(col 3 in output), all s2 values must get a value of b(col 3 in output). If s1 and s2 rows... (1 Reply)
Discussion started by: senhia83
1 Replies

4. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

5. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

6. Shell Programming and Scripting

Grepping text block by block by using for loop

Hei buddies, Need ur help once again. I have a file which has bunch of lines which starts from a fixed pattern and ends with another fixed pattern. I want to make use of these fixed starting and ending patterns to select the bunch, one at a time. The input file is as follows. Hi welcome... (12 Replies)
Discussion started by: anushree.a
12 Replies

7. UNIX for Advanced & Expert Users

Deciding whether to get a buffer cache block or inode block

I was reading a book on UNIX internals "The design of the UNIX Operating system." There are two memory structures that are confusing me: 1) Buffer cache 2) Inode cache My questions are 1) Does a process get both buffer cache and Indoe cache allocated when it opens/creates a file? 2) if no,... (1 Reply)
Discussion started by: sreeharshasn
1 Replies

8. Shell Programming and Scripting

Getting values from a block into variables

Hi there, If I run "ipmitool bmc info" on any of my x86 boxes, i get Device ID : 32 Device Revision : 1 Firmware Revision : 1.1 IPMI Version : 2.0 Manufacturer ID : 42 Manufacturer Name : Sun Microsystems Product ID ... (7 Replies)
Discussion started by: rethink
7 Replies

9. Shell Programming and Scripting

finding a block in a file and replace with another file block.

(1) Yes but how is this block different from the other 24? You will need this information in order to identify and replace this block correctly (out of the 25). Ans: The 1st line and last line of this block are unique from other block. The 1st line is “rem Subset Rows (&&tempName.*) and The... (1 Reply)
Discussion started by: Zaheer.mic
1 Replies

10. Shell Programming and Scripting

how to append a block of statements after another block in the file

Hi I need to append the following block of statements in the middle of the file: # openpipe tsdbdwn2 set -x exec >> /tmp/tsdbdwn2.fifo 2>&1 # This needs to be appended right after another block of statements: if test $# -eq 0 ;then echo "Safety check - do you really wish to run" $0 "... (5 Replies)
Discussion started by: aoussenko
5 Replies
Login or Register to Ask a Question