Reading multiple lines with condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading multiple lines with condition
# 1  
Old 09-25-2014
Reading multiple lines with condition

Hi guys,

I need to read following lines and put them in same row ….

Code:
text: Abcd5437_XYA0_B1_WXYZ_BE
               99:00:14:42:55:01:d4:22
               99:00:14:42:70:01:d4:22
               99:00:14:42:55:03:a0:22
               99:00:14:42:70:03:a0:22
               99:06:01:55:30:55:2d:67
               99:06:01:77:30:55:2d:67
text: ABC1234_E1_A1_BE00_Abcd6520_XYA0_B1     
               99:06:01:77:39:a0:17:4b
              99:06:01:55:39:a0:17:4b
               99:00:14:42:55:01:d4:22
text: ABC1234_E1_B1_BE00_Abcd6520_XYA0_B1     
               99:00:14:42:70:01:d4:22
               99:06:01:77:39:a0:17:4b
text: ABC1234_E2_A1_BE00_Abcd6520_XYA0_B1     
               99:06:01:77:39:a0:17:4b
               99:00:14:42:55:03:a0:22
               99:06:01:55:39:a0:17:4b

I am using getline function but it is not working the way I want ….I want to see this as below

Code:
text: Abcd5437_XYA0_B1_WXYZ_BE,99:00:14:42:55:01:d4:22,99:00:14:42:70:01:d4:22,99:00:14:42:55:03:a0:22,99:00:14:42:70:03:a0:22,99:06:01:55:30:55:2d:67,99:06:01:77:30:55:2d:67
text: ABC1234_E1_A1_BE00_Abcd6520_XYA0_B1,99:06:01:77:39:a0:17:4b,99:06:01:55:39:a0:17:4b,99:00:14:42:55:01:d4:22
text: ABC1234_E1_B1_BE00_Abcd6520_XYA0_B1,99:00:14:42:70:01:d4:22,99:06:01:77:39:a0:17:4b
text: ABC1234_E2_A1_BE00_Abcd6520_XYA0_B1,99:06:01:77:39:a0:17:4b,99:00:14:42:55:03:a0:22,99:06:01:55:39:a0:17:4b

Any help will be appreciated.

Thank you ....

Last edited by Don Cragun; 09-26-2014 at 01:12 AM.. Reason: Add CODE tags.
# 2  
Old 09-25-2014
This should do the trick:
Code:
 awk '
BEGIN{s="";}
{
        if($0 ~ "text")
        {
                if(length(s))
                {
                        print s;
                        s = "";
                }
        }
        s=s$0;
}
END{print s}
' your_input_file

# 3  
Old 09-25-2014
Code:
awk 'function p(){if(length(s))print s}/^text:/{p();s=$0;next}{s = s "," $0}END{p()}' file


OR like this


Code:
awk '/^text:/{printf("%s%s",(++i == 1 ? "": RS), $0); next}{printf("%s%s", OFS,$0)}END{print ""}' OFS=',' file


Last edited by Akshay Hegde; 09-26-2014 at 12:03 AM..
# 4  
Old 09-26-2014
perfect ...... Thank you.....

how do I put a space between first line and 2nd?
# 5  
Old 09-26-2014
Quote:
Originally Posted by dc@bos
perfect ...... Thank you.....

how do I put a space between first line and 2nd?
I'm lost. Where do you want the space? Your sample output showed that you wanted commas separating input lines in each group and didn't show double spaced lines in the output. The only spaces that appeared in your sample output was the space between text:. Do you want all of the spaces that were hidden by not using CODE tags at the start of each line other than the 1st line in each group to appear after the commas?

Please show us what output you really want (and use CODE tags this time).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading multiple lines in a file

Hello, I am new in shell scripting. I need help regarding following. I have 4 files generated by backups daily. I have stored the names of these 4 files into one file. i.e I have 4 files names as a, b, c & d and these names have been put into one file abcd.txt. Now I want to cat each file in... (7 Replies)
Discussion started by: Ali Sarwar
7 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

4. Shell Programming and Scripting

Multiple if condition

Hi, I wanted to satisfy two requirements to proceed to do a task One of them is to calculate between two metrics and the other to check one of the file not empty if the condition matches the above two, it should proceed with the task below is the snippet of it, however when i run the script... (3 Replies)
Discussion started by: ajothi
3 Replies

5. Shell Programming and Scripting

How to ignore single or multiple lines between /* and */ while reading from a file in unix?

I have a file proc.txt: if @debug = 1 then message 'Start Processing ', @procname, dateformat(now(*), 'hh:mm:ss'), @julian type info to client; end if; /* execute immediate with quotes 'insert into sys_suppdata (property, value, key_name) location ''' || @supp_server || '.' ||... (5 Replies)
Discussion started by: kidncute
5 Replies

6. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

7. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

8. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

9. Shell Programming and Scripting

multiple condition in if

All, My environment is Red Hat Enterprise Linux 5. I am using the following condition -- if -0 ] above command is not working. It is telling that -a unexpected. Please help me (10 Replies)
Discussion started by: user7509
10 Replies

10. Shell Programming and Scripting

Reading multiple lines as single

Hi, Is it possible to read every 2 lines as single record, I have a file like below, ~CZK ~TSCHECHISCHE KRONE ~TSCHECH. REPUBLIK Dec 1 2005 12:00AM~ 10.840000~ ~DKK ~DAENISCHE KRONE ~DAENEMARK Dec 2 2005 12:00AM~ ... (9 Replies)
Discussion started by: braindrain
9 Replies
Login or Register to Ask a Question