how to write a function to get data under specific lines ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to write a function to get data under specific lines ?
# 1  
Old 11-02-2011
Question how to write a function to get data under specific lines ?

I have a text file called (msgz ) contains data :

Code:
Subscriber 
Data ID = 2 
Customer = 99
Data ID = 4 
Customer = cf99
Data ID = 5 
Customer = c99
Data ID = 11 
Customer = 9n9

Subscriber 
Data ID = 1 
Customer = 9ds9
Data ID = 2 
Customer = 9sad9
Data ID = 3 
Customer = f99
Data ID = 4 
Customer = 9sg9
Data ID = 5 
Customer = 9sg
Data ID = 8 
Customer = 99

And so on for # of subscribers
I want to write a function to get me the customer number under Data ID = 4
Note: number of lines containing data id variable and not in same place 4 every subscriber

Output file >
Code:
Cf994
9sg9
Null      ------------   if its not found ( data id = 4 under each subscriber ) 
…..

Please help I think its impossible
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-02-2011 at 08:17 AM.. Reason: code tags, please!
# 2  
Old 11-02-2011
hope it helps..
Quote:
cat <filename> | grep -A 1 "ID = 4" | grep "Customer" | awk '{print $3}' >out.file
This User Gave Thanks to gowtham.varma For This Post:
# 3  
Old 11-02-2011
Code:
$ nawk -F= '/Data ID = 4/{getline;print $2}' infile

This User Gave Thanks to jayan_jay For This Post:
# 4  
Old 11-02-2011
@ jayan_jay
thanks alot
please send me to print null if not found under subscriber
2 - if

data id = 4
id = ff
customer= c99

if customer already in all file in the 3rd line or the 2nd after data id
thanks waiting replies

---------- Post updated at 08:07 AM ---------- Previous update was at 08:06 AM ----------

Quote:
Originally Posted by jayan_jay
Code:
$ nawk -F= '/Data ID = 4/{getline;print $2}' infile

Quote:
Originally Posted by gowtham.varma
hope it helps..



thanks alot but i gotta use it in bash
it tells me illegal -A option
-A gets no of lines after it nice work
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write out specific data from log to a new file

I got a huge log in zipped files, i need to write out lines by specific data and if the line with the same contains XML message with the same sessionID will be written to the file to. The log structure: 2013-08-16 16:31:06,810 ( 122: rogate) INFO - UId:10453, GId:5422: new... (16 Replies)
Discussion started by: batka
16 Replies

2. UNIX for Advanced & Expert Users

Extracting specific lines from data file

Hello, Is there a quick awk one-liner for this extraction?: file1 49389 text55 52211 text66 file2 59302 text1 49389 text2 85939 text3 52211 text4 13948 text5 Desired output 49389 text2 52211 text4 Thanks!! (5 Replies)
Discussion started by: palex
5 Replies

3. UNIX for Dummies Questions & Answers

Filtering data -extracting specific lines

I have a table to data which one of the columns include string of text from within that, I am searching to include few lines but not others for example I want to to include some combination of word address such as (address.| address? |the address | your address) but not (ip address | email... (17 Replies)
Discussion started by: A-V
17 Replies

4. Shell Programming and Scripting

Print specific lines of a repeated set of data

I have a file that needs 1st line, 2nd line, and 26th line printed from every chunk of data. Each chunk of data contains 26 lines (#line+%line+24 data lines = 26 lines of data repeated). Input file: # This is a data file used for blockA (chunk 1). % 10576 A 10 0 1 04 (data1) 03 (data2)... (2 Replies)
Discussion started by: morrbie
2 Replies

5. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

6. Shell Programming and Scripting

how to write a function to get data under spesific lines ? using bash

I have a text file called ( bvhz ) contains data : Subscriber Data ID = 2 Customer = 99 Data ID = 4 Customer = cf99 Data ID = 5 Customer = c99 Data ID = 11 Customer = 9n9 Subscriber Data ID = 1 Customer = 9ds9 Data ID = 2 Customer = 9sad9 Data ID = 3 Customer = f99... (1 Reply)
Discussion started by: teefa
1 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

insert data into specific lines of a CSV

So I work in a 1 to 1 laptop deployment and sometimes we need to mass order parts. The vendor will send us a text file and we have to manually input serial numbers. Well I have a full blown web based inventory system which I can pull serial number reports from. I then have to input the part... (4 Replies)
Discussion started by: tlarkin
4 Replies

9. Shell Programming and Scripting

[BASH] Using a function to write data to a file

Hello, I've created a shell script, which accepts information using an input from the console. Part of the script will write a file containing this information. My code looks like (for the write) function make_file { cat <<- _EOF_ The contents of my file are here _EOF_ } ... (12 Replies)
Discussion started by: cpickering
12 Replies

10. Shell Programming and Scripting

Read Write byte range/chunk of data from specific location in file

I am new to Unix so will really appreciate if someone can guide me on this. What I want to do is: Step1: Read binary file - pick first 2 bytes, convert from hex to decimal. Read the next 3 bytes as well. 2 bytes will specify the number of bytes 'n' that I want to read and write... (1 Reply)
Discussion started by: Kbenipel
1 Replies
Login or Register to Ask a Question