Splitting Single line into multiple line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Splitting Single line into multiple line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-15-2013
diehard diehard is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 9 December 2014, 12:47 AM EST
Posts: 48
Thanks: 19
Thanked 1 Time in 1 Post
Splitting Single line into multiple line

Hi All,

I am reading a line from a file and writing it to other file.
Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is


Code:
U_ABC  connector3  pin24E  connector4  pin25E  connector5  pin26E  connector6  pin27E  connector7  pin27F  connector8  pin26F  connector9  pin25F  connector10  pin24F  connector11  pin23F  connector0  pin21E  connector12  pin22F  connector1  pin22E  connector13  pin21F  connector2  pin23E

I want above line to be splitted into four line as below.

1.Every line will start from first name U_ABC.
2.First line will have connection0 and its subsequent pin number,connection1 and its subsequent pin number, connection2 and its subsequent pin.
3. Same for other three line in seq.
4. Connection7 and connection13 along with their pin number should be not be included.

I can split it into four line as

Code:
awk '{for (i = 1; i <= NF; i += 3) printf "%d %d %d\n", $i, $(i+1), $(i+2)}' file

but dont no how to write it in sequence as above. Do I need to save it in some form of dictionary first?

Thanks

Moderator's Comments:
You need CODE tags for input and output samples as well as for code samples. Especially when there is more than a single space between fields and when a line will be split into multiple lines when CODE tags are not used.

Last edited by Don Cragun; 10-15-2013 at 06:52 AM.. Reason: Add CODE tags.
Sponsored Links
    #2  
Old 10-15-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 December 2014, 4:11 PM EST
Location: San Jose, CA, USA
Posts: 5,263
Thanks: 207
Thanked 1,757 Times in 1,497 Posts
Assuming that you really want to translate "connectorX" from your input to "connectionX" in your output, and that you might want to feed multiple lines into this script, the following seems to do what you want:

Code:
awk '
{       mc = 0
        for(i = 2; i < NF; i += 2) {
                c = substr($i, 10) + 0
                if(c != 7 && c != 13) {
                        p[c] = $(i + 1)
                        if(c > mc) mc = c
                }
        }
        lc = 0
        for(i = 0; i <= mc; i++)
                if(i in p) {
                        if(lc++ == 0) o = $1
                        o = o "  connection" i "  " p[i]
                        if(lc == 3) {
                                print o
                                lc = 0
                        }
                        delete p[i]
                }
        if(lc) print o
}' file

This could be simplified slightly if you will always only process one line at a time.
If each line to be processed always has connector0 through connector12 (except for connector7) and connector7 and connector13 might also appear, but no others, it could be simplified some more. I will leave those simplifications as an exercise for the reader.

With your sample input, this script produces:

Code:
U_ABC  connection0  pin21E  connection1  pin22E  connection2  pin23E
U_ABC  connection3  pin24E  connection4  pin25E  connection5  pin26E
U_ABC  connection6  pin27E  connection8  pin26F  connection9  pin25F
U_ABC  connection10  pin24F  connection11  pin23F  connection12  pin22F

The Following User Says Thank You to Don Cragun For This Useful Post:
diehard (10-15-2013)
Sponsored Links
    #3  
Old 10-15-2013
diehard diehard is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 9 December 2014, 12:47 AM EST
Posts: 48
Thanks: 19
Thanked 1 Time in 1 Post
Thanks a lot!!!!!!!
That will solve my problem.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed command to grep multiple pattern present in single line and delete that line vivek d r Shell Programming and Scripting 14 05-14-2013 09:07 AM
convert single line output to multiple line kamauv234 Shell Programming and Scripting 6 08-07-2012 12:06 PM
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 06:40 PM
single line input to multiple line output with sed mitch Shell Programming and Scripting 8 07-16-2008 02:57 PM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 04:56 AM



All times are GMT -4. The time now is 07:12 PM.