split single line into two line or three lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split single line into two line or three lines
# 1  
Old 04-08-2010
split single line into two line or three lines


Dear All,

I want to split single line into two line or three lines wherever “|” separated values comes using

Input line
Code:
test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,



output shoule be
Code:
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,


# 2  
Old 04-08-2010
Code:
awk -F '|' '{for(i=2;i<=NF;i++){ print $1.$i}}' filename

HTH,
PL
# 3  
Old 04-08-2010
Getting following Error

Code:
awk -F '|' '{for(i=2;i<=NF;i++){ print $1.$i}}' test1 

 syntax error The source line is 1.
 The error context is
                {for(i=2;i<=NF;i++){ print >>>  $1. <<< $i}}
 awk: The statement cannot be correctly parsed.
 The source line is 1.

# 4  
Old 04-08-2010
try...

Code:
 awk -F"|" '{print $1$2",","\n"$1$3}' file

# 5  
Old 04-08-2010
MySQL

Code:
[root@sistem1lnx tes1]# cat 2
test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,

[root@sistem1lnx tes1]# sed -e 's/\([[:graph:]][[:graph:]].*\)/\1\n\1/' -e 's/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\2/' -e 's/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\3/' 2
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,

# 6  
Old 04-08-2010
Something like this: ?
Code:
 
awk -F"|" '{ for ( i=2;i<=NF;i++) $i=$1","$i"\n" ;$1=""; print $0 }' inputfile

# 7  
Old 04-08-2010
And now it's time for a Perl solution... Smilie

Code:
$
$
$ cat -n f9
     1  test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,
     2  PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,
$
$
$ perl -lne 'if(/^(.*?)\|([\d|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2}' f9
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,
PQRS,abcdefghi201001019999998877,,,,1234567,
PQRS,abcdefghi201001019999998877,,,,8901234,
PQRS,abcdefghi201001019999998877,,,,5678901,
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How do I split a single-line input into five lines?

Example input: John:Shepherd:770-767-4040:U.S.A:New York Mo Jo:Jo Jo: 666-666-6666:U.S.A:Townsville Expected Output: First Name: John Last Name: Shepherd Phone Number: 770-767-4040 Country: U.S.A State: New York First Name: Mo Jo Last Name: Jo Jo Phone Number: 666-666-6666... (10 Replies)
Discussion started by: Camrikron
10 Replies

3. Shell Programming and Scripting

Write the lines in one single line

Hi, I need some iteration to do the following work. Sample: ANS|26-Jan-2012|26|MON|12536.1 ANS|26-Jan-2012|26|TUE|2536.1 ANS|26-Jan-2012|26|THUR|789.1 SED|26-Jan-2013|32|MON|258.1 SED|26-Jan-2013|32|TUE|369.1 SED|26-Jan-2013|32|THUR|2145.1 OUTPUT: ... (3 Replies)
Discussion started by: anshaa
3 Replies

4. 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

5. Shell Programming and Scripting

Split the a single line into two

Hi all, I wanted to split a single line into two line. For example: A/B/C 10 i want this output var1 = A/B/C var2 = 10 How do i get this. (2 Replies)
Discussion started by: ch33ry
2 Replies

6. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

7. Shell Programming and Scripting

Split line in to 3 lines

Hi, I have a file which contains 1000's of lines. Each line is a log which is pretty long. So i want to split the each line based on 3 category. 1> Date 2><REQUEST> 3><RESPONSE> So below is the example of a line. 2010-11-16 00:45:12,314<REQUEST><VALIDATION-ERROR><soapenv:Envelope... (16 Replies)
Discussion started by: raghunsi
16 Replies

8. 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

9. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

10. Shell Programming and Scripting

Need output in different lines not in one single line

I am getting the coutput like this as show below in one single line, where as the command is executed is several lines and the output should also be requied in several lines, not in one single line. Anyone any idea? p4 opened -a | grep *locked* | awk '{ printf $8 }' >/tmp/aa $ cat... (1 Reply)
Discussion started by: csaha
1 Replies
Login or Register to Ask a Question