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
# 1  
Old 06-09-2014
Get values block by block in same file

I have a file say "SAMPLE.txt" with following content,

Code:
    P1
    10,9:6/123456
    P2
    blah blah
    P1
    10,9:5/98765
    P2
    blah
    blah
    P1
    blah blah
    P2

I want a output file say "RESULT.txt" as,

Code:
    Value1:123456
    Value2:98765
    Value3:NULL

I need to first fetch content between P1 & P2 part then I want to find value of 10,9*/ which I want to save to another value. Incase some P1--P2 block doesn't contain this value I want to save it as "NULL".

How can I code the above in shell/awk ?

I am very new to scripting. Thanks for help.
# 2  
Old 06-09-2014
Welcome to Forums, try
Code:
$ cat file
    P1
    10,9:6/123456
    P2
    blah blah
    P1
    10,9:5/98765
    P2
    blah
    blah
    P1
    blah blah
    P2

Code:
$ awk -F'/' 'function isnum(x){return(x==x+0)}/P1/,/P2/{if(!/P1|P2/)print "value"++i,":",isnum($NF)?$NF:"NULL"}' file
value1 : 123456
value2 : 98765
value3 : NULL

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 06-09-2014
Try:
Code:
awk -F"\/" '/P1/{p=1}/\//&&p{x=$2}/P2/&&p{print "Value"++i":"((x)?x:"NULL");p=0;x=""}' SAMPLE.txt

# 4  
Old 06-09-2014
Code:
awk -F/ '$1 ~ /P1/ {getline; print "Value" ++c ":", (NF==2?$2:"NULL")}' SAMPLE.txt


Last edited by Aia; 06-09-2014 at 03:09 PM.. Reason: Simplified
# 5  
Old 06-09-2014
Quote:
Originally Posted by Akshay Hegde
Welcome to Forums, try
Code:
$ cat file
    P1
    10,9:6/123456
    P2
    blah blah
    P1
    10,9:5/98765
    P2
    blah
    blah
    P1
    blah blah
    P2

Code:
$ awk -F'/' 'function isnum(x){return(x==x+0)}/P1/,/P2/{if(!/P1|P2/)print "value"++i,":",isnum($NF)?$NF:"NULL"}' file
value1 : 123456
value2 : 98765
value3 : NULL

Hello Akshay,

Thanks for quick response.
Your awk works fine but there are some tweaks that need to be done in order to fit my scenario.

First of all, you awk is reading line by line and putting NULL in every place it doesn't find a 10,9 value which is not the case I am looking for.

In my scenario, 10,9 can occur anywhere inside P1-P2 block along with other data Or there can be a case where it doesn't occur at all.
But if it will occur it will be only once.

Here is a sample input that you can work with.

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
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.

Thanks again for your help. Smilie Appreciated.
# 6  
Old 06-09-2014
So what would be the expected output for given new input ?

Whether this is what you are looking for ?

Code:
$ awk -F'/' 'function isnum(x){return(x==x+0)}/P1/,/P2/{if(!/P1|P2/ && /10,9:.*/)print "value"++i,":",isnum($NF)?$NF:"NULL"}' file
value1 : 18013013582
value2 : 13233361170

# 7  
Old 06-09-2014
Quote:
Originally Posted by Akshay Hegde
So what would be the expected output for given new input ?

Whether this is what you are looking for ?

Code:
$ awk -F'/' 'function isnum(x){return(x==x+0)}/P1/,/P2/{if(!/P1|P2/ && /10,9:.*/)print "value"++i,":",isnum($NF)?$NF:"NULL"}' file
value1 : 18013013582
value2 : 13233361170

@Akshay, please see my previous post.

Its almost close to what I want, only I think you have not include the condition to print "NULL" in case it doesn't find specified value.

Last edited by garvit184; 06-09-2014 at 03:44 PM.. Reason: Could have been misleading
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