Splitting number (no delimiter)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting number (no delimiter)
# 8  
Old 03-05-2013
HI I fixed source file manually, even though I edited, I am losing some bit of information, time being can anyone tell me now how to split 6 digit number?
# 9  
Old 03-06-2013
Is this what you want..?

Code:
$ cat file
2597
121297
13599
11295
130498

Code:
$ awk 'length($0)==4{printf "%0.2d/%0.2d/%s\n", substr($0,1,1),substr($0,2,1),"19"substr($0,3,2)}
length($0)==6{printf "%0.2d/%0.2d/%s\n", substr($0,1,2),substr($0,3,2),"19"substr($0,5,2)}
length($0)==5{if(substr($0,2,2)>12){printf "%0.2d/%0.2d/%s\n", substr($0,1,2),substr($0,3,1),"19"substr($0,4,2)}else{printf "%0.2d/%0.2d/%s\n", substr($0,1,1),substr($0,2,2),"19"substr($0,4,2)}}' file

02/05/1997
12/12/1997
13/05/1999
01/12/1995
13/04/1998

This User Gave Thanks to pamu For This Post:
# 10  
Old 03-06-2013
Yes. Pamu..will you please explain
Code:
substr

logic. As I never did splitting of number with no delimiter
# 11  
Old 03-06-2013
Quote:
Originally Posted by Akshay Hegde
Yes. Pamu..will you please explain
Code:
substr

logic. As I never did splitting of number with no delimiter
substr means print sub string or part of a string and we give parameter as from where to where.

for 5 digit string it will be having length as 5 so we can print first 1 digit as

substr($0,1,1) here $0 denotes from which string we needs to print. first 1 denotes from which position and second 1 denotes how many digits we have to print.

And same for middle two digits(starting from second position)substr($0,2,2)

And logic is i've divided files into 3 categories base on length 4, 5 and 6.
And for length == 5 i've added one more condition to check month.

Hope this helps

pamu
This User Gave Thanks to pamu For This Post:
# 12  
Old 03-06-2013
Thank you so much Pamu for explanation
# 13  
Old 03-06-2013
@pamu, you could improve your script's robustness by using $1 instead of $0, otherwise it will not print a number if there is any kind of whitespace on the line .
# 14  
Old 03-06-2013
Quote:
Originally Posted by Scrutinizer
@pamu, you could improve your script's robustness by using $1 instead of $0, otherwise it will not print a number if there is any kind of whitespace on the line .
Thanks Scrutinizer.

Also I've added function to get more clear codeSmilie

Code:
awk '
function prt(a,b,c) {
printf "%0.2d/%0.2d/%s\n", a,b,"19"c
    }
    length($1)==4 {prt(substr($1,1,1),substr($1,2,1),substr($1,3,2))}
    length($1)==6 {prt(substr($1,1,2),substr($1,3,2),substr($1,5,2))}
    length($1)==5 {if(substr($1,2,2)>12){prt(substr($1,1,2),substr($1,3,1),substr($1,4,2))}
    else{prt(substr($1,1,1),substr($1,2,2),substr($1,4,2))}}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

2. UNIX for Dummies Questions & Answers

Splitting strings based on delimiter

i have a snippet from server log delimited by forward slash. /a/b/c/d/filename i need to cut until last delimiter. So desired output should look like: /a/b/c/d can you please help? Thanks in advance. (7 Replies)
Discussion started by: alpha_1
7 Replies

3. UNIX for Advanced & Expert Users

Inserting delimiter after a specific number of chars

Hello guys, I have a problem where I need to add a delimiter, that can be | for example, after each 28000 chars. The problem is that sometimes 1 row, which should contain 28000 chars is split in 2, so I want to put the delimiter after each 28000 so I will know the end of each row. Please... (2 Replies)
Discussion started by: Diogo R Jesus
2 Replies

4. UNIX for Dummies Questions & Answers

Splitting the file basis of Line Number

Can u pls advise the unix command as I have a file which contain the records in the below format 333434 435435 435443 434543 343536 Now the total line count is 89380 , now i want to create a separate I am trying to split my large big file into small bits using the line... (2 Replies)
Discussion started by: punpun66
2 Replies

5. Shell Programming and Scripting

Splitting a file based on line number

Hi I have a file with over a million lines (rows) and I want to split everything from 500,000 to a million into another file (to make the file smaller). Is there a simple command for this? Thank you Phil (4 Replies)
Discussion started by: phil_heath
4 Replies

6. Shell Programming and Scripting

Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below, Data,Filename Row1 -> abc#Test1.xml Row2 -> xyz#Test2.xml Row3 -> ghi#Test3.xml The content in first field has to be written into a file where filename should be considered from second field. So from... (4 Replies)
Discussion started by: jayakkannan
4 Replies

7. Shell Programming and Scripting

Counting number of records with string row delimiter

HI, i have a file like this t.txt f1|_f2|_ f1|_f2|_ f1|_f2|_ as if col delimiter is |_ and row delimiter |_\n trying to count number of records using awk $ awk 'BEGIN{FS="|_" ; RS="~~\n"} {n++}END{print n} ' t.txt 7 wondering how can i count this to 3 ? thx (9 Replies)
Discussion started by: aksforum
9 Replies

8. Shell Programming and Scripting

Splitting file based on number of rows

Hi, I'm, new to shell scripting, I have a requirement where I have to split an incoming file into separate files each containing a maximum of 3 million rows. For e.g: if my incoming file say In.txt has 8 mn rows then I need to create 3 files, in which two will 3 mn rows and one will contain 2... (2 Replies)
Discussion started by: wahi80
2 Replies

9. Shell Programming and Scripting

Cut Number which appear before a delimiter

Hi All, How can i use the cut option to only output the number of rows, which is 2 is this case ? Pls note that the number of digit before the delimiter ":" is always varying. $ grep -n uuu xxx 2:** xxx yyy gg 44 tt uuu 2007 $ (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

splitting file with more than one delimiter

Hi, I just wandering how to split a record which has more than one delimiter, i have a file which contains pattern as group separtor and ~ as field separtor, Ultimately I need consider even the groups as a field, So i need to make this multi-delimited file into ~ delimited file. My record... (4 Replies)
Discussion started by: braindrain
4 Replies
Login or Register to Ask a Question