Flat File Extraction


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Flat File Extraction
# 1  
Old 10-31-2008
Flat File Extraction

Hi all,

I'm new in the unix environment. I'm having a challenge in extracting data from a flat file and convert it to a CSV file format or I should be able to open it with MS Excel.

The input data in my flat file looks like this:

AV00001001155000063637143326711145412082EM SITHOLE
AV00002015137000000170166517002130286080PM KUBEKA
AV00003050511000008329472412107030051008BHJ VAN SCHALKWYK

I can settle with a csv file format since I can still open it in MS Excel:

AV,00001,001155000063637,143326711145412082,EM, SITHOLE

Your assistance in the above regard will be appreciated.

Regards,
Mthimbana.......


# 2  
Old 10-31-2008
With Perl:

Code:
perl -lne'print join",",
  /(..)((?:.){5})((?:.){15})((?:.){18})(..)(.*$)/
' infile

With you data the above code produces:

Code:
% cat file
AV00001001155000063637143326711145412082EM SITHOLE
AV00002015137000000170166517002130286080PM KUBEKA
AV00003050511000008329472412107030051008BHJ VAN SCHALKWYK

ubuntu% perl -lne'print join",",/(..)((?:.){5})((?:.){15})((?:.){18})(..)(.*$)/' file
AV,00001,001155000063637,143326711145412082,EM, SITHOLE
AV,00002,015137000000170,166517002130286080,PM, KUBEKA
AV,00003,050511000008329,472412107030051008,BH,J VAN SCHALKWYK

# 3  
Old 10-31-2008
Code:
 sed -e 's/^.\{2\}/&,/' -e 's/^.\{8\}/&,/' -e 's/^.\{24\}/&,/' -e 's/^.\{43\}/&,/' -e 's/ /, /1' infile
AV,00001,001155000063637,143326711145412082,EM, SITHOLE
AV,00002,015137000000170,166517002130286080,PM, KUBEKA
AV,00003,050511000008329,472412107030051008,BHJ, VAN SCHALKWYK

Not sure if the bold part up there is correct with 3 letters at the end or one of those letters is extra.
# 4  
Old 10-31-2008
Hammer & Screwdriver and with awk

Code:
> cat file24
AV00001001155000063637143326711145412082EM SITHOLE
AV00002015137000000170166517002130286080PM KUBEKA
AV00003050511000008329472412107030051008BHJ VAN SCHALKWYK
> awk '{print substr($0,1,2)","substr($0,3,5)","substr($0,8,15)","substr($0,23,8)","substr($0,41,2)","substr($0,43,20)}' file24
AV,00001,001155000063637,14332671,EM, SITHOLE
AV,00002,015137000000170,16651700,PM, KUBEKA
AV,00003,050511000008329,47241210,BH,J VAN SCHALKWYK
>

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

2. Shell Programming and Scripting

data extraction from a file

Hi Freinds, I have a file1.txt in the following format File1.txt I want to get 2 files from the above file filextra.txt should have the lines which are ending with "<" and remaining lines in the filecompare.txt file. Please help. (3 Replies)
Discussion started by: i150371485
3 Replies

3. Shell Programming and Scripting

Fixed length flat file extraction

Hii ,I am new to Unix ,i have a flat file which is (fixed length) sitting in unix,Which is holding the data for a table.I want to extract one column(length7-10) on the basis of another column(length13-15) and want only one single row Example: Below is the sample of flat file. 1111 AAAA 100 ... (4 Replies)
Discussion started by: laxmi1166
4 Replies

4. Shell Programming and Scripting

File Extraction

Hi, I have three files as below: AA.DAT20110505063903.Z AA.DAT20110405062903.Z AA.DAT20110305061903.Z All the above files are appended with Date and timestamp in compressed format. I need to extract AA.DAT20110505063903.Z(which is the latest file) from one server and uncompress it... (2 Replies)
Discussion started by: pyaranoid
2 Replies

5. Shell Programming and Scripting

File extraction without awk

Hello everybody, Here is my problem : I cannot find a way to extract data from a particular file and more precisely I cannot extract the result of my awk script to an external file because I am currently working on HP-UX. I would like a simple script (without awk) which asks for a date like... (4 Replies)
Discussion started by: freyr
4 Replies

6. Shell Programming and Scripting

Data Extraction From a File

Hi All, I have a requirement where I have to search the file with some text say "Exception". This exception word can be repeated for more then 10 times. Suppose the "Exception" word is repeated at line numbers say x=10, 50, 60, 120. Now I want to extract all the lines starting from x-5 to... (3 Replies)
Discussion started by: rrangaraju
3 Replies

7. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies

8. Shell Programming and Scripting

Extraction of various lines from a hugh file

Dear Members, I have a huge file generated by the command 'whois' for hundred of IPs. Each section in the file starts with I want to extract those lines which start with any of these words: Input: OrgName: University of C OrgID: U1 Address: OIT Address: NH City: ... (5 Replies)
Discussion started by: srsahu75
5 Replies

9. Shell Programming and Scripting

date-extraction from a file in KSH

Hi, everyone, Now I have a ".csv" file and I want to extract a string-like date value from the file, say, it is in the 2nd row of the file and it is in the fixed context like: " This is the file title, //Row 1 The lastest update happened on: 10-Mar-2006 //Row2 ....//other irrelavent rows "... (6 Replies)
Discussion started by: homer_hn
6 Replies

10. UNIX for Dummies Questions & Answers

help on file extraction

Hello, Im trying to extract a portion of a big file. Using unique pattern /occurrence , (ex. loginname1,logoff and loginname2,logoff ), I like to print the lines that contain the patterns and the lines between them. Also, create a file for every login occurrence. Thanks for everyone's... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question