select a portion of a file into a CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select a portion of a file into a CSV
# 1  
Old 01-15-2008
select a portion of a file into a CSV

How will i convert a file

<LDATE>10-12-07</LDATE><LTIME>13:47:48.553</LTIME><LTEXT>name:anju;city:blore;ph:123</LTEXT>
<LDATE>10-12-07</LDATE><LTIME>13:47:48.553</LTIME><LTEXT>name:anju;city:blore;ph:123</LTEXT>
<LDATE>10-12-07</LDATE><LTIME>13:47:48.553</LTIME><LTEXT>name:anju;city:blore;ph:123</LTEXT>
<LDATE>10-12-07</LDATE><LTIME>13:47:48.553</LTIME><LTEXT>name:anju;city:blore;ph:123</LTEXT>

to this
10-12-07,13:47:48.553,b'lore
10-12-07,13:47:48.553,b'lore
10-12-07,13:47:48.553,b'lore

Basically i want some portions of a file to be merged and form a new comma seperated file...
Please help this is urgent...Smilie

anju
# 2  
Old 01-15-2008
using Perl:
Code:
perl -pi -e 's#\<(.*?)\>(.*?)\<\/\1\>#\2,#g' tagged_file
perl -pi -e 's/,$//' tagged_file

# 3  
Old 01-15-2008
Quote:
Originally Posted by Yogesh Sawant
using Perl:
Code:
perl -pi -e 's#\<(.*?)\>(.*?)\<\/\1\>#\2,#g' tagged_file
perl -pi -e 's/,$//' tagged_file

could you please explain what u have done.coz i am new to perl and have no much idea abt this.
# 4  
Old 01-15-2008
The poster wanted only the city portion of the third "field" to be ouputted. Yogesh Sawant's perl solution outputs all of the third field.

Here is a solution based on sed.

Code:
sed -e 's/></>,</g' -e 's/<[^<>]*>//g' -e 's/\(^.*,.*,\)\(.*city:\)\(.*\)\(;.*$\)/\1\3/g' tagged_file

which outputs

Code:
10-12-07,13:47:48.553,blore
10-12-07,13:47:48.553,blore
10-12-07,13:47:48.553,blore
10-12-07,13:47:48.553,blore

It works by:

1. Place a comma between sets of right and left angle brackets
2. Remove XML tags
3. Extract city data from third field
# 5  
Old 01-15-2008
GNU awk
Code:
awk '{
    match($0,/<LDATE>(.*)<\/LDATE>/,mldate)   
    match($0,/<LTIME>(.*)<\/LTIME>/,mltime)  
    match($0,/<LTEXT>(.*)<\/LTEXT>/,mltxt)  
    split(mltxt[1], mltext,"[;:]")    
    print mldate[1]","mltime[1]","mltext[4]   
}' "file"

# 6  
Old 01-15-2008
Another one with gawk:

Code:
awk 'BEGIN {FS="[<>]"} {
  split($11, s, "[;:]")
  print $3 "," $7 "," s[4]
}' file

Regards
# 7  
Old 01-16-2008
Quote:
Originally Posted by ghostdog74
GNU awk
Code:
awk '{
    match($0,/<LDATE>(.*)<\/LDATE>/,mldate)   
    match($0,/<LTIME>(.*)<\/LTIME>/,mltime)  
    match($0,/<LTEXT>(.*)<\/LTEXT>/,mltxt)  
    split(mltxt[1], mltext,"[;:]")    
    print mldate[1]","mltime[1]","mltext[4]   
}' "file"

when i execute this code i am getting this error
awk: syntax error near line 2
awk: illegal statement near line 2
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 4
awk: illegal statement near line 4
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

5. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

6. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

7. Shell Programming and Scripting

select data from oracle table and save the output as csv file

Hi I need to execute a select statement in a solaris environment with oracle database. The select statement returns number of rows of data. I need the data to be inserted into a CSV file with proper format. For that we normally use "You have to select all your columns as one big string,... (2 Replies)
Discussion started by: rdhanek
2 Replies

8. Shell Programming and Scripting

awk program to select a portion of a line

Hi all, I am new to awk programs.I have a file like this vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd... (3 Replies)
Discussion started by: anju
3 Replies

9. Shell Programming and Scripting

Select a portion of file based on query

Hi friends :) I am having a small problem and ur help is needed... I have a long file from which i want to select only some portions after filtering (grep). My file looks like : header xxyy lmno xxyy wxyz footer header abcd xy pqrs footer . . (14 Replies)
Discussion started by: vanand420
14 Replies
Login or Register to Ask a Question