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
# 8  
Old 04-08-2010
Code:
$ echo 'test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,' | sed "s/\([^|]*\)|\([^|]*\)|\(.*\)/\1\2,\\
> \1\3/"
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,

# 9  
Old 04-19-2010
is this posible to get last string in each line??

my input is
Code:
test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#flase#no

output should be
Code:
test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#flase#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#flase#no

# 10  
Old 04-19-2010
MySQL

Quote:
Originally Posted by arvindng
is this posible to get last string in each line??

my input is
Code:
test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#flase#no

output should be
Code:
test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#flase#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#flase#no


You can use also same my code..

Code:
[root@sistem1lnx ~]# a="test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#flase#no"
 
[root@sistem1lnx ~]# echo $a | sed 's/\([[:graph:]][[:graph:]].*\)/\1\n\1/
s/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\2/
s/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\3/'
 
test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#flase#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#flase#no

# 11  
Old 04-19-2010
Quote:
Originally Posted by arvindng
is this posible to get last string in each line??

my input is
Code:
test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#flase#no

output should be
Code:
test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#flase#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#flase#no

The Perl script posted earlier should work for this as well, since everything after the digits and "|" character is stored in $3. So $3 was just "," for the former input file and it would be ",true#false#no" for this input file.

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

tyler_durden
# 12  
Old 04-19-2010
Code:
sed "s/\([^|]*\)|\([^|]*\)|\([^,]*\),\(.*\)/\1\2,\4\\
> \1\3,\4/" file

# 13  
Old 04-20-2010
Quote:
Originally Posted by durden_tyler
The Perl script posted earlier should work for this as well, since everything after the digits and "|" character is stored in $3. So $3 was just "," for the former input file and it would be ",true#false#no" for this input file.

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

tyler_durden

it not working when Alphanumeric value is comming

Code:
cat test1
test,DEMTEMPUT20100404010012,,,,,,,,|0a70086|007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,|123c567|89a1234,true#false#no
 
output is 
perl -lne 'if(/^(.*?)\|([\d|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2>
test,DEMTEMPUT20100404010012,,,,,,,,0a70086|007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,1234567,true#false#no
PQRS,abcdefghi201001019999998877,,,,8901234,true#false#no
PQRS,abcdefghi201001019999998877,,,,5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,123c567|89a1234,true#false#no

# 14  
Old 04-20-2010
This should work in that case:

Code:
$ 
$ cat test1
test,DEMTEMPUT20100404010012,,,,,,,,|0a70086|007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,|123c567|89a1234,true#false#no
$ 
$ 
$ perl -lne 'if(/^(.*?)\|([\w|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2}' test1
test,DEMTEMPUT20100404010012,,,,,,,,0a70086,true#false#no
test,DEMTEMPUT20100404010012,,,,,,,,007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,1234567,true#false#no
PQRS,abcdefghi201001019999998877,,,,8901234,true#false#no
PQRS,abcdefghi201001019999998877,,,,5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,123c567,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,89a1234,true#false#no
$ 
$

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