Returning multiple outputs of a single line based on previous repeated lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning multiple outputs of a single line based on previous repeated lines
# 1  
Old 07-15-2014
Returning multiple outputs of a single line based on previous repeated lines

Hello,
I am trying to return a time multiple times from a file that has varying output just before the time instance, i.e.

Code:
cat jumped
cat jumped 
cat jumped
time = 1.1
cat jumped
cat jumped
time = 1.2
cat jumped
cat jumped
time = 1.3

In this case i would like to output a time.txt file which would show
Code:
1.1
1.1
1.1
1.2
1.2
1.3
1.3

My current command only returns the single instance of the time result. Any help in returning the desired result would be great.

Note that i have tried both

Code:
awk '/time/ { time = $3 }; {print time };' inputfile >& time.txt

and the more simple,

Code:
grep 'time' inputfile | cut -d' ' -f3 >& time.txt


Last edited by Scrutinizer; 07-15-2014 at 02:52 PM.. Reason: CODE tags
# 2  
Old 07-15-2014
For exactly the sample given, try
Code:
awk '{CNT++} /time/ {for (;--CNT>0;) print $3}' file
1.1
1.1
1.1
1.2
1.2
1.3
1.3

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-15-2014
That works great! now i am trying to add an if statement to the beginning which will allow to choose which word is counted in the line
Code:
cat jumped
dog
cat jumped 
cat jumped
time = 1.1
cat jumped
cat jumped
dog
time = 1.2
cat jumped
cat jumped
dog
time = 1.3

Code:
1.1
1.1
1.1
1.2
1.2
1.3
1.3

so that dog is not counted and only cat is
I think it needs to be written, using RudiC's style

Code:
awk '{if($1=='cat') CNT++} /time/ {for (;--CNT>0;) print $3}' file

but this returns an error.
# 4  
Old 07-15-2014
Use double quotes instead of single quotes around cat
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 07-15-2014
Try
Code:
awk '/cat/ {CNT++} /time/ {for (;--CNT>0;) print $3}' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 07-15-2014
Try:
Code:
awk '/cat|time/{c++} /time/{while (--c>0) print $3}' file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-15-2014
Scrutinizer, that last one works perfect, the other two were missing one line from each section some how. Thanks a bunch!
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 to single line

I have code as below # create temporary table `temp4277`(key(waybill_no)) select waybill_no,concat_ws('',card_type,card_series_no) cardinfo from rfid_temp_ticket where waybill_no='4277' group by... (4 Replies)
Discussion started by: kaushik02018
4 Replies

2. UNIX for Beginners Questions & Answers

Merging multiple lines into single line based on one column

I Want to merge multiple lines based on the 1st field and keep into single record. SRC File: AAA_POC_DB.TAB1 AAA_POC_DB.TAB2 AAA_POC_DB.TAB3 AAA_POC_DB.TAB4 BBB_POC_DB.TAB1 BBB_POC_DB.TAB2 CCC_POC_DB.TAB6 OUTPUT ----------------- 'AAA_POC_DB','TAB1','TAB2','TAB3','TAB4'... (10 Replies)
Discussion started by: raju2016
10 Replies

3. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

4. Shell Programming and Scripting

Multi line document to single lines based on occurance of string

Hi Guys, I am new to awk and sed, i am working multiline document, i want to make make that document into SINGLE lines based on occurace of string "dwh". here's the sample of my problem.. dwh123 2563 4562 4236 1236 78956 12394 4552 dwh192 2656 46536 231326 65652 6565 23262 16625623... (5 Replies)
Discussion started by: victor369
5 Replies

5. Shell Programming and Scripting

Combine multiple lines in single line

This is related to one of my previous post but now with a slight difference: I need the "Updated:" to be in one line as well as the "Information:" on one line as well. These are in multiple lines right now as seen below. These can have 2 or more lines that needs to be in one line. System name:... (8 Replies)
Discussion started by: The One
8 Replies

6. Shell Programming and Scripting

Multiple lines into a single line

Hi, I've some files with the following data and i need to convert the lines between the separator ---, into a single line. I've tried with the paste cmd but my main problem is that the number of lines between the separator is not fix, it can very between 1-4 lines. Input --- 2010-02-22... (4 Replies)
Discussion started by: RickyC9999
4 Replies

7. Shell Programming and Scripting

Getting multiple messy lines into one single line

I have a file that contains the following: :@:176:@:4:@:name:@:file:@:this is a summary:@:description can be long but who knows can even have <br> tags.:@:how to:@:type:@:18544:@:550:@:400:END: :@:177:@:9:@:name:@:file:@:summary:@:this will containg... (18 Replies)
Discussion started by: sysrenan
18 Replies

8. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

9. Shell Programming and Scripting

merge lines into single line based on symbol \t

The symbols are \t and \t\t (note: not tab) If the line starts with \t merge them into a single line upto symbol \t\t \t\t to end and start new line I able to join in a single line but not ending at \t\t and I completely confused help would be appreciated:b::D Input \ta tab XXXXXXXXXX \te... (5 Replies)
Discussion started by: repinementer
5 Replies

10. Shell Programming and Scripting

replacing multiple lines with single line

Can any one give me the idea on replacing multiple blank lines with a single blank line? Please conside it for a file having more than 100 number of characters. Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies
Login or Register to Ask a Question