LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-11-2008, 06:47 AM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
printing column wise


Hi Friends,

I have a log file as below


siteid = HYD
spc = 100
rset = RS_D_M
siteid = DEL
spc = 200
rset = RS_K_L
siteid = DEL2
spc = 210
rset = RS_D_M


Now I need a output file where i can print column wise as below.

siteid SPC rset
HYD 100 RS_D_M
DEL 200 RS_K_L

Can anybody help me.

Is awk suitable for this
 
Old 09-11-2008, 09:23 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I'd probably use a little Perl program. This works, but it's not very elegant:

Code:
#!/usr/bin/perl

print "siteid SPC rset\n";

my %data;

clear_data();

while(<>) {
        chomp;
        if ( /^(siteid|spc|rset) = (.*)/ ) {

                if (defined($data{$1})) {
                        warn "at line $. - $1 found, but we still didn't print the previous record\n";
                }

                $data{$1} = $2;

                if ($1 eq "rset") {
                        if (!defined($data{'siteid'}) || !defined($data{'spc'})) {
                                warn "at line $.  - missing a siteid or spc\n";
                        }
                        print_record();
                        clear_data();
                }
        }
}

sub clear_data {
        foreach my $k (qw(siteid spc rset)) {
                delete $data{$k};
        }
}

sub print_record {
        printf "%s %s %s\n", $data{'siteid'}, $data{'spc'}, $data{'rset'};
}
 
Old 09-11-2008, 09:30 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Code:
IFS="
"
echo "siteid SPC rset" >t1.t 
for rec in `cat t.t`
do
    field=`echo $rec|cut -d' ' -f1`
    value=`echo $rec|cut -d' ' -f3`
    echo $field $value
    if [[ $field == "rset" ]]
    then
        echo -n $value >> t1.t
        echo >> t1.t
    else 
        echo -n "$value " >> t1.t
    fi
done
 
Old 09-11-2008, 10:00 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,267

Rep: Reputation: 99
I have one more solution.

Suppose your file name is abc

Run this

Code:
cat abc | tr '=' ' ' | awk '{print $1}' | head -3 | tr '\n' ' ' > file1
echo -e -n "\n" >> file1
cat abc | tr '=' ' ' | awk '{print $2}' | tr '\n' ' ' > file2
echo -e -n "\n" >> file2
Now, You have two files as

file1
Code:
siteid spc rset
file2
Code:
HYD 100 RS_D_M DEL 200 RS_K_L DEL2 210 RS_D_M
Now, I want to insert a new line into every 3rd column, so that file2 breaks as :-
Code:
HYD 100 RS_D_M
DEL 200 RS_K_L
DEL2 210 RS_D_M
Pls someone suggest for this step, I am sure on line awk or sed could help this BUT I am not very sure how to use it.

Thanks N Regards,
VIKAS
 
Old 09-11-2008, 12:00 PM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
For fixed ordered columns:

Code:
awk 'BEGIN { 
  OFS="\t"; FS="= *" 
  print "siteid", "spc", "rset" 
  }
(ORS = NR % 3 ? OFS : RS) && $0 = $2
'  filename
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing a column in a file.. slight confusion with my code pdklinux79 Linux - Newbie 3 06-17-2008 07:19 PM
Word to the Wise dfowensby Ubuntu 1 02-08-2008 09:04 AM
awk column printing schneidz Programming 7 09-29-2005 07:14 AM
132 column printing sachin_keluskar Linux - Software 0 07-17-2004 03:29 AM
bandwidth allocation by user wise and ip wise basbosco Linux - Networking 1 11-12-2003 03:54 AM


All times are GMT -5. The time now is 07:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration