How to cut a flat file according to a certain number of characters?


 
Thread Tools Search this Thread
Operating Systems AIX How to cut a flat file according to a certain number of characters?
# 1  
Old 01-29-2010
How to cut a flat file according to a certain number of characters?

hello everybody

i am looking for a shell to cut a flat file (with a long unique line) according to a certain number of characters and redirect every result to an output file.

here is an example

MyFile :

12 3 456 12 3 456 12 3 456 .....

and i took every 9-characters including BLANKS in MyFile
so the ouput file OutFile will be like that

12 3 456
12 3 456
12 3 456
.......

i have flat files with an average of 2 Millions of characters that's why i really want to do it with a shell

Tried with awk, cut and sed but i am not really a specialist so if there is a clue just answer me cause i am in a hurry to find a solution
# 2  
Old 01-29-2010
Used dd before?
# 3  
Old 01-29-2010
2 megabytes really isn't that big.

Try 'man split'.
# 4  
Old 01-29-2010
hello and thx for answering me so fast

sorry i was wrong, actually it's a text file of 200 MegaBytes !
i need to change the format of the content so it will be possible to insert the datas in a database.

i googled for "split" but i found it works with bytes and i am not sure that 1 character = 1 byte

i've heard about awk and i keep looking, if there's something that works i'll tell you

---------- Post updated at 09:36 PM ---------- Previous update was at 09:28 PM ----------

Quote:
Originally Posted by vbe
Used dd before?
no not yet but i sounds really good, i am gonna give it a try.

anything with awk (i heard that it's a powerful tool)?
# 5  
Old 02-04-2010
Quote:
Originally Posted by fastlane3000
hello and thx for answering me so fast

sorry i was wrong, actually it's a text file of 200 MegaBytes !
Still doable. As long as you're not doing a sort on it or any other sort of thing that requires chunking it all into memory at once, the only difference is it'll take longer.
Quote:
i googled for "split"
... Try reading its man page. No googling, it just comes up. 'man split'. The version I have works with bytes OR lines, which makes it pretty clear to me that 1 byte = 1 char. But you want it in one file, not separate files, so that suggestion of mine was wrong...
Quote:
i've heard about awk and i keep looking, if there's something that works i'll tell you
I don't think awk, or any other line-based tool, can operate on a line 200 megabytes long. I'll try and find something in bash...

[edit] bash took minutes and minutes to process a 5 meg file. dd works better but you have to read the man page... the 'unblock' mode converts trailing spaces to newlines, so, for the data you gave:

Code:
dd cbs=9 ibs=9 obs=9 if=longline of=longfile conv=unblock

converted a 10 meg file in 5 seconds.

Last edited by Corona688; 02-04-2010 at 04:22 PM..
# 6  
Old 02-06-2010
MySQL

Thank you very much !
you guys are great, dd works perfectly Smilie
49 seconds to convert a 200Mb-File it's really awesomeSmilie

Quote:
Used dd before?
Merci beaucoup Monsieur vbe, vous êtes le meilleur!

Quote:
Originally Posted by Corona688
I don't think awk, or any other line-based tool, can operate on a line 200 megabytes long.
THANK'S Corona, you're also the best, and you're right , i found this
Code:
 cat File | awk ' { for ( i = 1; i <= length($0); i++ ) a[i]=substr($0,((i-1)*9)+1,9) } END { for ( i in a ) print a[i] } ' >> output

which is couldn't work if the lengh is more than 3000 (awk limitation)

Quote:
Originally Posted by Corona688
the 'unblock' mode converts trailing spaces to newlines, so, for the data you gave:
Code:
dd cbs=9 ibs=9 obs=9 if=longline of=longfile conv=unblock

converted a 10 meg file in 5 seconds.
i must confess i was lost with the configuration of "dd" being a newbie in Unix i was really in need of help
# 7  
Old 02-08-2010
Quote:
Originally Posted by fastlane3000
i must confess i was lost with the configuration of "dd" being a newbie in Unix i was really in need of help
dd's configuration has always been a little... a lot odd compared to other UNIX tools. When browsing the jargon file I found, much to my surprise, that its interface was perpetrated on UNIX as a practical joke poking fun at a computing platform, architecture, and interface they all hated! Smilie but they wrote it too well and it became indispensable, its basic form kept unaltered(and/or faithfully duplicated!) for decades now. The revenge of the System/360...

Last edited by Corona688; 02-08-2010 at 12:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding distinct characters from flat file

Hi....I need one help.... I'm having a files which is having the data as follows... a b c c d d d e f Now I need to find out distinct characters from this file and the output should be as follows - a b c d e f Can you please help me on this? I'm using KSH script. (18 Replies)
Discussion started by: Krishanu Saha
18 Replies

2. Shell Programming and Scripting

Get line number in flat file

Hi, Is there a way to find out the line number from where the data starts? like if the data contains column header, irrespective of the text in the column header we should get the line number from which contains the column header. I am sorry if I haven't explained the problem clearly. ... (8 Replies)
Discussion started by: kedar_laveti
8 Replies

3. UNIX for Dummies Questions & Answers

How to remove numeric characters in the flat file

HI, can any one help me please .. i have flat file like qwer123rt ass3242ccf jjk654 kjh838ppp nhdg453ok hdkk34 i want remove numeric characters in the flat file i want output like this qwerrt assccf jjk kjhppp nhdgok hdkk help me... (4 Replies)
Discussion started by: rafimd1985
4 Replies

4. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

5. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

6. Shell Programming and Scripting

Need to cut first 21 and 32-25 characters from file

Guys, can you help me in doing cut first 21 and 32-35 characters from file. I tried with cut -c to cut first 21 characters ,It is succeeded. But i need both first 21 and 32-35. (1 Reply)
Discussion started by: mohan_xunil
1 Replies

7. Shell Programming and Scripting

cut between characters of a file

Hi All, Need to convert file names to upper case using tr command in Unix. In a folder -> /apps/dd01/misc there are two files like: pi-abcd567sd.pdf pi-efgh1.pdf The output of should be like: pi-ABCD567SD.pdf pi-EFGH1.pdf I have used the command to work as below: for f... (3 Replies)
Discussion started by: a1_win
3 Replies

8. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies

9. UNIX for Advanced & Expert Users

foreign characters in flat file

Hey, Is there anyway I anks, Pocha (12 Replies)
Discussion started by: pochaman
12 Replies

10. Shell Programming and Scripting

Help Replacing Characters in Flat File

I was wondering if somebody could help me with something on UNIX. I have a file that looks like this - "nelson,bill","bill","123 Main St","Mpls","MN",55444,8877,william I want to replace all comma with pipes (|), except if the comma is within double quotes. (The first field is an example of... (8 Replies)
Discussion started by: nelson553011
8 Replies
Login or Register to Ask a Question