Breaking up at the second occurrence


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Breaking up at the second occurrence
# 1  
Old 02-13-2012
Breaking up at the second occurrence

hi,

My input is:
Code:
123
1234|123|123|123
123|123|456
123|123|12
12

Expected output is:
Code:
123
1234|123
123|123
123|123
456
123|123
12
12

The logic is breaking up at the 2nd occurerence of "|" delimiter and making remaining as a new line.

And also,if input contains more than 3 | delimiter then:
Code:
123|123|123|123|123

, output should be:
Code:
123|123
123|123
123

How we can do this in awk?
Thanks
# 2  
Old 02-13-2012
One solution:

Code:
awk -F \| '
    NF %2 {
        for( i = 1; i < NF-1; i += 2 )
            printf( "%s|%s\n", $(i), $(i+1) );
        printf( "%s\n", $(i) );
        next;
    }

    {
        for( i = 1; i < NF; i += 2 )
            printf( "%s|%s\n", $(i), $(i+1) );

    }
' input-file

Might be able to shirink it if I gave it some thought, but off the top of my head it does work.

---------- Post updated at 22:57 ---------- Previous update was at 22:54 ----------

A more concise approach:

Code:
awk -F \| '
    {
        for( i = 1; i < NF - (NF%2); i += 2 )
            printf( "%s|%s\n", $(i), $(i+1) );
        if( NF % 2 )
            printf( "%s\n", $(i) );
    }
' input-file

This User Gave Thanks to agama For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

3. Shell Programming and Scripting

Breaking a pipe

Here's my code - for File in "${Files}"; do uuencode "${File}" "$(basename ${File} 2>&-)" 2>&-; done | mailx -s "subject" "a@b.c" Now I want to know if there a way to *not* send an email if the "Files" variables turns out to be blank. Any suggestions? Also, just to clarify, I... (4 Replies)
Discussion started by: nexional
4 Replies

4. Shell Programming and Scripting

Breaking out of an if statement

i have a nested if statement. i need to check for a condition (highlighted in red below). if condition is true, i need the if statement to break out of the statement and then return to the if statement section i have highlighted in blue. is this possible? if ; then #return here ... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

breaking for loop

Dear Friends, Here I need your guidance once again. I have for loop which check all files in a folder for a particular string. If the string is found in a file it returns value other than 0 else returns 0 value in variable t2. At times the string which we are looking for is in first file... (1 Reply)
Discussion started by: anushree.a
1 Replies

6. Shell Programming and Scripting

Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop. Let me explain with example here: I... (6 Replies)
Discussion started by: vpv0002
6 Replies

7. Shell Programming and Scripting

File breaking

Hey, I have to take one CSV file and break into more files. Let's I have a file prices.csv and the data in the file like 1,12345 1,34567 1,23456 2,67890 2,77720 2,44556 2,55668 10,44996 based on the first column, I want to create files. in this example 1 is repeated three times... (12 Replies)
Discussion started by: bond2222
12 Replies

8. Shell Programming and Scripting

Breaking up a file

Hi, I have a file that looks like this - lets call it fileA >hhm2 IIIIIIIIILLLLLLLMMMMMMMMMNNNNNNNNNNGGGGGGHHHHHHHH >hhm4 OOOOOKKKKKKKKMMMMMHHHHHLLLLLLLLWWWWWWWWWWW >hhm9 OOOOOOOIIIIIIIIIKKKKKKKKKMMMMMHHHHHHHHHHHLLLLLLLLLL So the file is pretty straight forward. The name is indicated... (2 Replies)
Discussion started by: phil_heath
2 Replies

9. Linux

breaking out of while loop

Hi , I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other... (3 Replies)
Discussion started by: capri_drm
3 Replies
Login or Register to Ask a Question