Put delimeter in data based on value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put delimeter in data based on value
# 1  
Old 11-07-2012
Question Put delimeter in data based on value

Hi Friends,
I have a file as below

source.txt

Code:
 
12345JackYKing32N
1235 JulyYoig 31N

i am using cut command for cutting the fields

Code:
 
cut -c 1-5 source.txt
 
12345
1235

like above i have to use each time to cut all the fieds manually. I have a file
Code:
(pre.txt)

which tells about the length of each fields

pre.txt

Code:
 
5,4,1,4,2,1

I need to use the above pre.txt and get the output file as below

output.txt

Code:
 
12345|Jack|Y|King|32|N
1235 |July|Y|oig |31|N

Plz help
# 2  
Old 11-07-2012
Code:
sed 's!\(.\{5\}\)\(.\{4\}\)\(.\)\(.\{4\}\)\(.\{2\}\)\(.\)\(.*\)!\1|\2|\3|\4|\5|\6!g' infile

This User Gave Thanks to complex.invoke For This Post:
# 3  
Old 11-07-2012
try

Code:
$ cat file
12345JackYKing32N
1235 JulyYoig 31N

$ awk -v var="5,4,1,4,2,1" '{n=split(var,P,",");k=1;for(i=1;i<=n;i++){s=s?s"|"substr($0,k,P[i]):substr($0,1,P[i]);k+=P[i]}print s;s=""}' file

12345|Jack|Y|King|32|N
1235 |July|Y|oig |31|N

EDIT: modified lit bit..Smilie

Code:
awk -v var="5,4,1,4,2,1" 'BEGIN{n=split(var,P,",")}{k=1;for(i=1;i<=n;i++){s=s?s"|"substr($0,k,P[i]):substr($0,1,P[i]);k+=P[i]}print s;s=""}' file


Last edited by pamu; 11-07-2012 at 05:32 AM..
This User Gave Thanks to pamu For This Post:
# 4  
Old 11-07-2012
@Complex invoke : Thanks for the reply . it works as expected.

@ Pamu: Thanks for the reply. It works as expected .. I have changed the code to accept the file .

Code:
 
awk -v var="$(<pre.txt)" '{n=split(var,P,",");k=1;for(i=1;i<=n;i++){s=s?s"|"substr($0,k,P[i]):substr($0,1,P[i]);k+=P[i]}print s;s=""}' file

Thanks very much pamu once again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script - redirecting out put based on mapping

Need awk solution. Please advise. inputfile.txt 1,NY, 1111 2,MI, 222 3,NY,333 4,OH,444 5,OH,555 mapping.txt NY NYNY IL ILLINOIS OH OHIO Need to write a code which will compare 2nd column of inputfile.txt with mapping file and redirect output based on the... (2 Replies)
Discussion started by: vegasluxor
2 Replies

2. Shell Programming and Scripting

Match word and put data before it

Hi all I have 7 words ina file called "lookupfile" CAD CD HT RA T1D T2D BD in other file I have data like this in which columns are seaprated by comma but the names among above seven names are in one column menas comma between these seven words doesnt mean that they are separated by... (9 Replies)
Discussion started by: manigrover
9 Replies

3. Shell Programming and Scripting

Replacing the delimeter with other delimeter

Hi Friends, I have a file1.txt as below 29123973Ç2012-0529Ç35310124Ç000000000004762Ç00010Ç20Ç390ÇÇÇÇF 29123974Ç20120529Ç35310125Ç0000000000046770Ç00010Ç20Ç390ÇÇÇÇF 29123975Ç20120529Ç35310126Ç0000000000046804Ç00010Ç20Ç390ÇÇÇÇF 29123976Ç20120529Ç35310127Ç0000000000044820Ç00010Ç20Ç390ÇÇÇÇF i have a file2.txt... (4 Replies)
Discussion started by: i150371485
4 Replies

4. Web Development

split line based on delimeter SQL

I have a SQL query SELECT BLAH_ID, BLAH_CODE, DATEFORMAT(CALENDAR_DATE, 'MM-DD-YYYY') AS CALENDAR_DATE_STR, HOURS, 'N', FROM blah_tmp_tbl order by CALENDAR_DATE_STR,BLAH_ID,BLAH_CODE; OUTPUT TO 'MyExport.CSV' quote '' FORMAT ASCII; That gets me the below output; ... (2 Replies)
Discussion started by: ffdstanley
2 Replies

5. Shell Programming and Scripting

How to merge 2 files based on delimeter in perl?

Hi, I have 2 files. a.txt & b.txt # a.txt contains the following text. apple grapes # b.txt contains the following text. banana pine My question is. (1 Reply)
Discussion started by: vanitham
1 Replies

6. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

7. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

8. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

9. Shell Programming and Scripting

Put raw data to column data

Dear all, I want below data to make it in column format.so i will see the data like this cdrID teleServiceCode chargedPartyNumber ... ... ... ... "egmailcom0w10ggzx00" 'sMS (5)' "716323770" "m17ifi5z30w0z6o7200" 'sMS (5)' ... (7 Replies)
Discussion started by: Nayanajith
7 Replies

10. Shell Programming and Scripting

Delimeter used in data

Blasted data inputters :mad: they always have to screw my data up....My comma delimited file with three fields ( firstname,surname and address ) has been screwed up by people entering addresses like this (putting a comma in between the house number and the street name) 142,Stonewall Avenue ... (8 Replies)
Discussion started by: hcclnoodles
8 Replies
Login or Register to Ask a Question