Split a line and append pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a line and append pipe
# 1  
Old 12-13-2011
Split a line and append pipe

I have a requirement like below
Code:
030150678  bos  00156     0500405612      23       23    
 
i have split the above line based on position and append pipe to it
 
1-3 , 4-8, 9, 10-18,19-25,26-27                                        
030|15067|8| .... so on

i can't use cut because the space is not delimetr here.. some of the fields are filled as spaces too...

Need imm Hep on this
# 2  
Old 12-13-2011
Hi greenworld123,

Try next 'perl' program:
Code:
$ cat script.pl
use warnings;
use strict;

while ( <DATA> ) {
        chomp;
        printf qq[%s\n], join ( qq[|], unpack( qq[A3 A5 A1 A9 A7 A2], $_ ) );
}

__DATA__
030150678  bos  00156     0500405612      23       23
$ perl script.pl
030|15067|8|  bos  00|156| 0

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 12-13-2011
Thx birei..

Will it possible using SED ..?
# 4  
Old 12-13-2011
One possible way:
Code:
$ cat infile
030150678  bos  00156     0500405612      23       23
$ sed 's/^\(.\{3\}\)\(.\{5\}\)\(.\)\(.\{9\}\)\(.\{7\}\)\(.\{2\}\).*/\1|\2|\3|\4|\5|\6/' infile
030|15067|8|  bos  00|156    | 0

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 12-13-2011
Thanks for your grt help..
# 6  
Old 12-14-2011
Quote:
Originally Posted by birei
One possible way:
Code:
$ cat infile
030150678  bos  00156     0500405612      23       23
$ sed 's/^\(.\{3\}\)\(.\{5\}\)\(.\)\(.\{9\}\)\(.\{7\}\)\(.\{2\}\).*/\1|\2|\3|\4|\5|\6/' infile
030|15067|8|  bos  00|156    | 0

Regards,
Birei

Code:
sed 's/^\(.\{11\}\)\(.\{60\}\)\(.\{40\}\)\(.\{2\}\)\(.\{12\}\)\(.\{2\}\)\(.\{1\}\)\(.\{1\}\)\(.\{3\}\)\(.\{13\}\)\(.\{12\}\)\(.\{1\}\)\(.\{1\}\).*/\1|\2|\3|\4|\5|\6|\7|\8|\9|\10|\11|\12|\13/' file

i tried the sample for my other file but i am getting
sed: command garbled

My input file has
totally 159 byte long

^03010000 bank of communications 00156 0800405512 19D
$

Don't know whats wrong in my command
# 7  
Old 12-14-2011
Your command runs on my system (RHEL, so GNU sed). What's your OS? If you're using Solaris then try /usr/xpg4/bin/sed.

EDIT: Post your data in [code] tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append pipe | at the end of all the rows except header n trailer for all the files under a directory

Hi Experts Need help... I am looking for a Unix script to append pipe | at the end of all the rows (except header and trailer)in all the files placed under the directory /interfaces/Temp e.g. Header row1 row2 row3 Trailer The script should read all the files under... (3 Replies)
Discussion started by: phani333
3 Replies

2. UNIX for Beginners Questions & Answers

awk command to split pipe delimited file

Hello, I need to split a pipe de-limited file based on the COLUMN 7 value . If the column value changes I need to split the file Source File Payment|ID|DATE|TIME|CONTROLNUMBER|NUMBER|NAME|INDICATOR 42156974|1137937|10/1/2018|104440|4232|2054391|CARE|1... (9 Replies)
Discussion started by: rosebud123
9 Replies

3. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

4. Shell Programming and Scripting

Append Next line with current Line bassed on condition

Hi, I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support. my current input data in below format: <Node>xxxxxx <Node>yyyyy</Node> <Node>zzzzzz <Node>12345</node> I need... (9 Replies)
Discussion started by: rramkrishnas
9 Replies

5. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

7. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

8. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

9. Shell Programming and Scripting

Append line that does not contain pipe to it previous line

Hi All, I have a file which contains data as below When we see no pipe character in the line. append those lines to the previous line with pipe character till we get the next line with pipe character with ~(concat with ~) Input file looks like: 1080530944|001|john.l.bonner|Acknowledge|CN... (11 Replies)
Discussion started by: ainuddin
11 Replies

10. Shell Programming and Scripting

How to split pipe delimited file

I have a pipe delimited input file as below. First byte of the each line indicate the record type. Then i need to split the file based on record_type = null,0,1,2,6 and create 5 files. How do i do this in a ksh script? Pls help |sl||SL|SL|SL|1996/04/03|1988/09/15|C|A|sl||||*|... (4 Replies)
Discussion started by: njgirl
4 Replies
Login or Register to Ask a Question