Outputting data from multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Outputting data from multiple lines
# 1  
Old 10-22-2008
Outputting data from multiple lines

Hi guys, looking for a bit of advise, and as I am a complete novice, please excuse the daft questions!!

I have a list of events and of which entry looks like this;

Code:
#
# Event 1
# NAME = Event 1
#

12345 : 123  : 1 : 1  : L,1,N : 1,0  : Event

#
# Event 2
# NAME = Event 2
#

12346 : 124  : 1 : 1  : L,1,N : 1,0  : Event

I am looking for a grep which can search for the various event code (in this case 12345) and print the Event "NAME" to screen. I have been racking my brains for a while now, but cant seem to think of a way to do this. Can anyone suggest a way?

So I will have something like;

"Event code $eventCode is $eventName" printed to the screen if that makes sense? Obviously using read to enter the event code in each time the script is ran...

Thank you in advanced.
JayC89
# 2  
Old 10-22-2008
Code:
awk -v v=12345 '$1==v{print s}$2=="NAME"{s="Event code" FS v FS "is" FS substr($0,10)}' file


Last edited by danmero; 10-22-2008 at 11:34 PM.. Reason: original solution was wrong
# 3  
Old 10-22-2008
Code:
#!/bin/bash

read -p " Enter id: " id

 awk -v id="$id"  '$1==id{ printf "Event code %s is %s\n", s, $1 } /NAME =/{ s=$(NF-1) FS $NF }' file

# 4  
Old 10-22-2008
Code:
nawk -F"[=|:|#]" -v e="$1" '{
if ($2==" NAME ")
	t=$3
if($1==e)
	print t
}' filename

# 5  
Old 10-23-2008
Thanks for all your help guys.

Here is what I am using at the moment;

Code:
#!/bin/bash

nawk -F"[=|:|#]" -v e="$1" '{
if ($2==" NAME ")
        t=$3
if($1==e)
        print t
}' comps

However this is printing duplicates (x4), e.g;

Code:
Event 1
Event 1
Event 1
Event 1
Event 2
Event 2
Event 2
Event 2
Event 3
Event 3
Event 3
Event 3

Could this have something to do with the source file displaying the event name twice, e.g;

Code:
#
# Event 1
# NAME = Event 1
#

Any advise would be great.

Thanks again all! Smilie
JayC89
# 6  
Old 10-23-2008
How about something like:

Code:
#  awk '/NAME/{FS="=";t="Event "$2" is ";getline;getline;getline;print t$0}' infile
Event  Event 1 is 12345 : 123  : 1 : 1  : L,1,N : 1,0  : Event
Event  Event 2 is 12346 : 124  : 1 : 1  : L,1,N : 1,0  : Event

or

Code:
#  awk '/NAME/{FS="=";t="Event "$2" is ";getline;getline;getline;FS=" ";print t$1}' infile
Event  Event 1 is 12345
Event  Event 2 is 12346

or am I misreading what you're after ?
# 7  
Old 10-23-2008
Thanks Tytalus,

I have had a little play around and;

Code:
awk '/NAME/{FS="=";t="Event "$2" is event id ";getline;getline;FS=" ";print t$3}' comps

seems to output the id and relevent event name perfectly. And looks something like

Code:
Event Event 1 is event id 587

Is there anyway to prompt the script to ask the user for an event id and then pull back the name of that event, such as;

Code:
#!/bin/sh
echo "Enter the event id"
read event

awk '/NAME/{FS="=";t="Event "$2" is event id ";getline;getline;FS=" ";print t$3}' comps

What would be needed to link that user input to the awk command?
JayC89
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple lines of data?

I use this to get 8 random letters: cat /dev/urandom | tr -dc 'A-Z' | fold -w 8 | head -n 1 Result is, WLGFJFZY What I'm trying to do is get 10 lines of random letters, separated by a line and each block having ascending numbers i.e; 00 IWMTDFIM 01 KZZZCHPQ 02 YBTGFHGT 03 (4 Replies)
Discussion started by: jenny-mac
4 Replies

2. Shell Programming and Scripting

Outputting data from log file to report

I have a log file that looks like this. the lines are grouped. 2 lines per entry. M: 2019-01-25 13:02:31.698 P25, received network transmission from KI4EKI to TG 10282 M: 2019-01-25 13:02:35.694 P25, network end of transmission, 4.3 seconds, 1% packet loss M: 2019-01-25 13:02:38.893 P25,... (7 Replies)
Discussion started by: ae4ml
7 Replies

3. Shell Programming and Scripting

Split data into multiple lines

Hi, if i have data like below: Control|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241 Im getting output in required format as below Control|AC-00011-CN-2475208 Control|AC-00011-CN-2475211 Control|AC-00007-CN-2475238 Control|AC-00007-CN-2475241 using awk... (9 Replies)
Discussion started by: JSKOBS
9 Replies

4. Shell Programming and Scripting

Extracting data from multiple lines

Hi All, I am stuck in one step.. I have one file named file.txt having content: And SGMT.perd_id = (SELECT cal.fiscal_perd_id FROM $ODS_TARGT.TIM_DT_CAL_D CAL FROM $ODS_TARGT.GL_COA_SEGMNT_XREF_A SGMT SGMT.COA_XREF_TYP_IDN In (SEL COA_XREF_TYP_IDN From... (4 Replies)
Discussion started by: Shilpi Gupta
4 Replies

5. Shell Programming and Scripting

Finding several patterns and outputting 4 lines after

I an trying to parse a file looking for pattern1, or pattern2, or pattern3 and when found print that line and the next 4 lines after it. I am using korn shell script on AIX and grep -A isn't available. (1 Reply)
Discussion started by: daveisme
1 Replies

6. Shell Programming and Scripting

Outputting discarded GREP lines to a file

I had a question about grep. If I grep for something in a file, the output shows me all the lines in which that 'something' is contained in. Is there a way to also output all the lines in which that 'something' wasnt contained in. Say I have a file with a bunch of names and I do: grep scott... (2 Replies)
Discussion started by: ndedhia1
2 Replies

7. Shell Programming and Scripting

How do you use pull data from multiple lines to do a for statement?

Guys I am having a problem with being able to find missing monitors in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the pool... (7 Replies)
Discussion started by: scottzx7rr
7 Replies

8. Shell Programming and Scripting

outputting data in table from grep results

Hi all. I'm a real unix newbie and looking for some help on a shell scripting problem. I'm going the longest ways around everything but I'm getting there. My next problem however has me stumped. I have set up a program that takes inputs from a user for a particular month and year (and stores them... (2 Replies)
Discussion started by: Chillspark
2 Replies

9. UNIX for Dummies Questions & Answers

Split data into multiple lines

All, I have a requirement where I will need to split a line into multiple lines. Ex: Input: 2ABCDEFGH2POIYUY2ASDGGF2QWERTY Output: 2ABCDEFGH 2POIYUY 2ASDGGF 2QWERTY The data is of no fixed lenght. Only the lines have to start with 2. How can this be done. (5 Replies)
Discussion started by: kingofprussia
5 Replies

10. Shell Programming and Scripting

Problem with outputting multiple lines

Dear Gurus, I have this output file: F1BDEV13 NTIAF101 2006/09/21 14:54:51 14:55:29 1 0560-0570 LAN F1BDEV14 NTIAF101 2006/09/21 14:55:30 14:55:49 1 0000-0000 LAN F1FSP001 NTIAF101 2006/09/21 14:55:51 14:55:53 1 0000-0000 LAN F1NSP001 ... (6 Replies)
Discussion started by: lweegp
6 Replies
Login or Register to Ask a Question