extract strings from file and display in csv format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract strings from file and display in csv format
# 1  
Old 03-17-2010
extract strings from file and display in csv format

Hello All,
I have a file whose data looks something like this

Code:
[id=4][name=alpha][state=ny][city=abc]
[id=2][name=beta][state=wv][city=pqr]
[id=3][name=theta][state=ca][city=xyz]
[id=5][name=gamma][state=tx][city=jkl]
[id=1][name=psi][state=ga][city=zzz]

I want to extract just the id, name and city fields in a csv format and sort them by id. Output should look like this.
Code:
1,psi,zzz
2,beta,pqr
3,theta,xyz
4,alpha,abc
5,gamma,jkl

I could extract the individual strings values using the sed command but could not arrange them in csv format. Could someone please help.
# 2  
Old 03-18-2010
how about awk
Code:
awk '{gsub(/\[|\]| |=/, " "); print $2 "," $4 "," $8}'  oldfile > newfile.csv

# 3  
Old 03-18-2010
Code:
 
sed -r 's/\[[a-z]+=([0-9]+)\]\[[a-z]+=([a-z]+)\].*\[[a-z]+=([a-z]+)\]/\1,\2,\3/' filename  | sort

# 4  
Old 03-18-2010
Assuming the number of fields are fixed,

Code:
perl -lane 'print  join ",", (split /=|]|\[/,$_)[2,5,8,11];' file

# 5  
Old 03-18-2010
MySQL

See the following Sed command.

Code:
sed -r 's/(.*)(=[a-z0-9]*)(.*)(=[a-z0-9]*)(.*)(=[a-z0-9]*)(.*)(=[a-z0-9]*)(.*)/\2,\4,\8/g;s/=//g' Input_file | sort

# 6  
Old 03-18-2010
Thank you all for your prompt response.

What should I do, If my data is as follows.

Code:
[id=4][name=alpha][state=ny][city=abc]
[employee=234]
[id=2][name=beta][state=wv][city=pqr]
[employee=254]
[id=3][name=theta][state=ca][city=xyz]
[employee=432]
[id=5][name=gamma][state=tx][city=jkl]
[employee=239]
[id=1][name=psi][state=ga][city=zzz]
[employee=222]

and I want m data as follows

Code:
1,222,psi,zzz
2,254,beta,pqr
3,432theta,xyz
4,234,alpha,abc
5,239,gamma,jkl

# 7  
Old 03-18-2010
Try this perl script ,

Code:
#!/usr/bin/perl

use strict;
use warnings;

open FH,"<file";
my $count=1;
my $id;
my $name;
my $state;
my $city;
my %hash=();
while(<FH>)
{
    if(($count%2)==0)
    {
        $_=~s/^.+=([0-9]+)]$/$1/g;
        chomp;
        push @{$hash{$id}},$_,$name,$city;
    }
    else
    {
        if(/\[.+=([0-9]+).+=(.+)].+=(.+)].+=(.+)]$/g)
        {
            $id=$1;
            $name=$2;
            $state=$3;
            $city=$4;
        }
    }
    $count++;
}

foreach (sort keys %hash)
{
    print $_ .",". $hash{$_}[0]. "," . $hash{$_}[1] . "," . $hash{$_}[2] ."\n";
}

Here 'file' contains the input .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

3. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

4. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

5. Shell Programming and Scripting

Extract strings from file - Help

Hi, I have a file say with following lines (the lines could start from any column and there can be many many create statements in the file) create table table1....table definition... insert into table1 values..... create or replace view view1....view definition.... What i want is to... (2 Replies)
Discussion started by: whoami191
2 Replies

6. Shell Programming and Scripting

Extract strings from multiple lines into one csv file

Hi all, Please go through my requirement. I have a log file in the location /opt/WebSphere61/AppServer/profiles/EMQbatchprofile/logs/EMQbatch This file contains the follwing pattern data <af type="tenured" id="42" timestamp="May 14 13:44:13 2011" intervalms="955.624"> <minimum... (8 Replies)
Discussion started by: satish.vampire
8 Replies

7. Shell Programming and Scripting

Extract multiple xml tag value into CSV format

Hi All, Need your assistance on another xml tag related issue. I have a xml file as below: <INVOICES> <INVOICE> <BILL> <BILL_NO>1234</BILL_NO> <BILL_DATE>01 JAN 2011</BILL_DATE> </BILL> <NAMEINFO> <NAME>ABC</NAME> </NAMEINFO> </INVOICE> <INVOICE> <BILL> <BILL_NO>5678</BILL_NO>... (12 Replies)
Discussion started by: angshuman
12 Replies

8. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

9. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question