Awk to convert a text file to CSV file with some string manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to convert a text file to CSV file with some string manipulation
# 1  
Old 08-17-2012
Awk to convert a text file to CSV file with some string manipulation

Hi ,

I have a simple text file with contents as below:

Code:
12345678900    971,76    4234560890
22345678900   5971,72    5234560990
32345678900     71,12    6234560190

the new csv-file should be like:

Code:
Column1;Column2;Column3;Column4;Column5
123456;78900;971,76;423456;0890
223456;78900;5971,72;523456;0990
323456;78900;71,12;623456;0190

The requirements are:
  • Column1 contains first six characters of first number in text file
  • Column2 contains last five characters of first number in text file
  • Column3 contains complete semicolon separated number of text file
  • Column4 contains first six characters of last number in text file
  • Column4 contains last four characters of last number in text file

Unfortunately I'm some kind of newbie in awk. I've tried writing some scripts by myself, but not with to much success.

Any help&hints would be appreciated.

Thanks in advance.
# 2  
Old 08-17-2012
Code:
awk '{sub(/.{6}/,"&"OFS,$1);sub(/.{6}/,"&"OFS,$3)}1' OFS=";" file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 08-17-2012
try this...

Code:
awk '{ print substr($1, 1, 6), substr($1, 7, 11) , $2 , substr($3, 1, 6), substr($3, 7, 11) }' OFS=\; test_temp

This User Gave Thanks to pamu For This Post:
# 4  
Old 08-17-2012
Does it have to be AWK?
Code:
perl -ple 's/\b\d{6}/$&;/g;s/\s+/;/g' file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 08-17-2012
Thx for the quick answer. Works like a charm :-)

---------- Post updated at 08:06 AM ---------- Previous update was at 07:34 AM ----------

Hi again,

probably a silly question but I also need the static first line in my csv file as a headline:

Column1;Column2;Column3;Column4;Column5
...

How can I combine this requirement using the script:
Code:
awk '{ print substr($1, 1, 6), substr($1, 7, 11) , $2 , substr($3, 1, 6), substr($3, 7, 11) }' OFS=\; test_temp

Thx a lot!

---------- Post updated at 08:09 AM ---------- Previous update was at 08:06 AM ----------

Hi again,

probably a silly question but I also need the static first line in my csv file as a headline:

Column1;Column2;Column3;Column4;Column5
...

How can I combine this requirement using the script:
Code:
awk '{ print substr($1, 1, 6), substr($1, 7, 11) , $2 , substr($3, 1, 6), substr($3, 7, 11) }' OFS=\; test_temp

Thx a lot!
# 6  
Old 08-17-2012
Code:
sed 's|\(.\{6\}\)\(.[^ ]*\) \{1,\}\(.[^ ]*\) \{1,\}\(.\{6\}\)\(.*\)|\1;\2;\3;\4;\5|g' infile

# 7  
Old 08-17-2012
Using awk, try

Code:
cat input.file | awk ' BEGIN {print "Column1;Column2;Column3;Column4;Column5" } { print  substr($0,1,6) ";" substr($0,7,5) ";" $2 ";" substr($0,26,6) ";" substr($0,7,5) ";" $2 ";" substr($0,26,6) ";" substr($0,32,4) } '

This User Gave Thanks to mjf For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

Need to Preprocess a text file and convert into csv

Hello, I was working with Machine learning and would like to apply my regression algorithms on binary classification datasets. So I came across this adult dataset, LIBSVM Data: Classification (Binary Class) It is a binary dataset, features have values only 1 and 0. and I wanted to... (4 Replies)
Discussion started by: ajayram
4 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

How to convert excel file to csv file or text file?

Hi all, I need to find a way to convert excel file into csv or a text file in linux command. The reason is I have hundreds of files to convert. Another complication is the I need to delete the first 5 lines of the excel file before conversion. so for instance input.xls description of... (6 Replies)
Discussion started by: johnkim0806
6 Replies

5. Shell Programming and Scripting

Trying extract from text file and convert csv

I want to extract IP address, system ID and engine IDs of this file ( marked in red) and put in a csv. E.g. 1.1.1.1, SYSTEMID, 000012345678981123548912 I get these file by running an expect script from solaris. Here is the text file output of my expect script. working on 1.1.1.1 SNMP... (5 Replies)
Discussion started by: pbshillong
5 Replies

6. Programming

String Manipulation in a text file

Hi I have a requirement to write a script but not sure which is the best way to approach this I have not worked in sed but I'm aware that its robust for file extraction requirements I have a scripting task. I already developed the code in perl but the script is taking almost 2 mins for... (5 Replies)
Discussion started by: John Trevor
5 Replies

7. Programming

awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database.. the input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB... (2 Replies)
Discussion started by: certteam
2 Replies

8. UNIX for Dummies Questions & Answers

Convert entries in a text file to values in a string

How to convert entries in a text file to values in a string? ex: I have a text file list.txt with following entries : aaaa bbbb cccc dddd I want to move the values in the list.txt file to an array (names) need a small script to do that? Thanks for your help in advance (3 Replies)
Discussion started by: kukretiabhi13
3 Replies

9. Programming

convert text file to csv

hi all, i have a select query that gives me the output in the following way... SYSTYPE -------------------------------------------------------------------------------- Success Failures Total RFT ---------- ---------- ---------- ---------- TYP 1 0 ... (3 Replies)
Discussion started by: sais
3 Replies

10. Shell Programming and Scripting

Awk to convert a flat file to CSV file

Hi , I have a file with contents as below: Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008... (14 Replies)
Discussion started by: rkumudha
14 Replies
Login or Register to Ask a Question