convert text contents/paragraph to cvs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert text contents/paragraph to cvs
# 1  
Old 11-24-2008
convert text contents/paragraph to cvs

hi all, iam new to awk & sed cmd and appreacite help from all the script expert from this forum, i have a file like below:

Fri Aug 01 00:01:19 2008
NAS-IP-Address = 172.16.160.1
Class = "P1-SHT-AAA01;1217116976;70999"
Calling-Station-Id = "00-11-FB-01-03-06"
Acct-Status-Type = Start
Acct-Session-Id = "31908b485a370000"
Framed-IP-Address = 172.16.160.10
BS-ID = 000012A00433
User-Name = "user1"

Fri Aug 01 00:02:02 2008
NAS-IP-Address = 172.16.160.1
Class = "P1-SHT-AAA01;1217116976;71005"
Calling-Station-Id = "00-11-FB-01-03-06"
Acct-Status-Type = Stop
Acct-Session-Id = "31908b485d370000"
Framed-IP-Address = 172.16.160.20
BS-ID = 000012A00413
User-Name = "user2"

Fri Aug 01 00:02:02 2008
NAS-IP-Address = 172.16.160.1
Class = "P1-SHT-AAA01;1217116976;71005"
Calling-Station-Id = "00-11-FB-01-03-06"
Acct-Status-Type = Stop
Acct-Session-Id = "31908b485d370000"
Framed-IP-Address = 172.16.160.20
BS-ID = 000012A00413
User-Name = "user3"

this only 3 records from the file but it actually have more than 5-7k records. and i need import this record into mysql server which mean I need to convert this record to csv format (1 record per line) like below:

2008-08-01;172.16.160.1;P1-SHT-AAA01;1217116976;71005;00-11-FB-01-03-06;Stop;31908b485d370000; 172.16.160.20;000012A00413;user3


thanks in advance.
# 2  
Old 11-24-2008
One approch:

Code:
awk -F= '!/^$/{if ($0 !~ /=/) {str="date -d \""$0"\" \"+%d-%m-%Y\""; val=system(str); } else {print $2;} }'  < filename | xargs -n9 | tr " " ";"

# 3  
Old 11-24-2008
dennis.jasob, you are genuis, thanks alot, let me try and see how is the output.
# 4  
Old 11-24-2008
dennis,

just tried your code and I almost there, only have some problem, the ouput

01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;70999;00-1F-FB-00-02-26;Start;31908b485a370000;120.140.0.54;000012A00433;1001100551@P1
24-11-2008;01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;71005;00-1F-FB-00-06-61;Start;31908b485d370000;120.140.0.184;000012A00413
1001101634@P1;01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;70999;00-1F-FB-00-02-26;Start;31908b485a370000;120.140.0.54;000012A00433
1001100551@P1;24-11-2008;01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;71005;00-1F-FB-00-06-61;Start;31908b485d370000;120.140.0.184
000012A00413;1001101634@P1

notice the date keep repeating on the next lines ? any solution for this ? thanks
# 5  
Old 11-24-2008
Quote:
Originally Posted by xajax7
dennis,

just tried your code and I almost there, only have some problem, the ouput

01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;70999;00-1F-FB-00-02-26;Start;31908b485a370000;120.140.0.54;000012A00433;1001100551@P1
24-11-2008;01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;71005;00-1F-FB-00-06-61;Start;31908b485d370000;120.140.0.184;000012A00413
1001101634@P1;01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;70999;00-1F-FB-00-02-26;Start;31908b485a370000;120.140.0.54;000012A00433
1001100551@P1;24-11-2008;01-08-2008;172.16.160.1;P1-SHT-AAA01;1217116976;71005;00-1F-FB-00-06-61;Start;31908b485d370000;120.140.0.184
000012A00413;1001101634@P1

notice the date keep repeating on the next lines ? any solution for this ? thanks

This is because of the blank lines. Try this:

Code:
awk -F= '!/^ /{if ($0 !~ /=/) {str="date -d \""$0"\" \"+%d-%m-%Y\""; val=system(str); } else {print $2;} }'  < filename  | xargs -n9 | tr " " ";"

# 6  
Old 11-24-2008
still the same.
# 7  
Old 11-24-2008
perl:

Code:
%hash=('Jan','01','Feb','02','Mar','03','Aug','08'); # add all the enumerate value 
$/="\n\n";
open FH,"<d:/perlscript/a.txt";
@arr=<FH>;
foreach(@arr){
	my @temp=split("\n",$_);
	map{s/.*=//;s/\"//g;} @temp;
	my @t=split(" ",$temp[0]);
	print $t[4],"-",$hash{$t[1]},"-",$t[2],";";
	print $_,";" foreach (@temp[1..$#temp-3]);
	print "\n"; 
	print $_,";" foreach (@temp[$#temp-2..$#temp]);
	print "\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print each paragraph in a text file into excel file

Hi, I have a below text file, which needs to be printed into attached excel file. Please help me. Thanks (4 Replies)
Discussion started by: prash358
4 Replies

2. Shell Programming and Scripting

Easy way to pull a paragraph from a text file?

I have a collection of text files that comprise a mailing list archive. I grep them to find an email that interests me, then open the file in a text editor to find the surrounding paragraph of text. Is there an easy way to do this from the shell instead? (2 Replies)
Discussion started by: CRGreathouse
2 Replies

3. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

4. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

5. Shell Programming and Scripting

Search for a particular string in a paragraph in a text

Hi all, i'm new to this community. I am trying to write a script which will fetch ftp completion time of a file from a paragraph of a big text file ( which contains multiple paragraphs) . Each paragraph will have ftp details.. Now I dont know how to fetch process time within a paragraph of... (3 Replies)
Discussion started by: prachiagra
3 Replies

6. Shell Programming and Scripting

Convert contents of file to lower case with SED

Hi I what to add option to existing sed code to convert target file to lower case #!/bin/ksh SOURCE_DATA_DEST=/ora TARGET_DATA_DEST=/home/oracle/alexz TARGET_DB_SID=T102_test sed -e "s/REUSE/SET/g" \ -e "s/NORESETLOGS/RESETLOGS/g" \ T102_ccf.sql > target.sql Thanks (2 Replies)
Discussion started by: zam
2 Replies

7. Shell Programming and Scripting

Need help in parsing text file contents

Hi, I need some help in extracting the Exception block between the lines 21 Feb 01:18:54:146 ERROR com.orbits.frameworks.integrationframework.ValidationException - Caught exception in validateRequest() (PID=565584) and 21 Feb 01:18:55:149 INFO ... (0 Replies)
Discussion started by: Alecs
0 Replies

8. Shell Programming and Scripting

extracting last paragraph from a text

Hi All, I want to extract last paragraph from a text for example aaaaa sadafsa sdfdsaf asfdsad asfdsfad asfdfsafd adfssas asdfsafdsa asfdasdfa sadfasdsaf i need the last paragraph how to do it using linux commands (1 Reply)
Discussion started by: uvrakesh
1 Replies

9. Shell Programming and Scripting

Convert a paragraph to single line

I need to check the count of pipes on each line of the data to make sure we 4 pipes if its less i need to keep adding the string to form a single line ( Need to join/ concat the below lines until i get the 4 pipes which is end of record ). This fields is basicall a memo where the user would have... (6 Replies)
Discussion started by: rimss
6 Replies

10. UNIX for Dummies Questions & Answers

How do I convert unix text to to win text?

How do I convert unix text files into readable text for windows. Dave (1 Reply)
Discussion started by: nucca
1 Replies
Login or Register to Ask a Question