Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to write this script?

Status
Not open for further replies.

bluelake

Technical User
Jan 20, 2003
57
CN
I have a file which reads like:
PKGINST: LGTOclnt
VERSION: 6.1.1.Build.238
PKGINST: NSCPcom
VERSION: 20.4.70,REV=1999.10.13.17.54
PKGINST: SMCcvs
VERSION: 1.11.5
PKGINST: SMCossh
VERSION: 3.4p1
PKGINST: SMCossl
VERSION: 0.9.6g

how can I write a script to convert it like:

PKGINST: LGTOclnt VERSION: 6.1.1.Build.238
PKGINST: NSCPcom VERSION: 20.4.70,REV=1999.10.13.17.54
PKGINST: SMCcvs VERSION: 1.11.5
PKGINST: SMCossh VERSION: 3.4p1
PKGINST: SMCossl VERSION: 0.9.6g

That is to say, to merge every two line to one line, Thank you.

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
Simple way:

say your raw data file is: file1
issue the command:
grep PKGINST file1 > file2
grep VERSION file1 > file3
paste file2 file3 > file4

Now file4 contains ur required format.

-sbsaikia
 
assumed the file looks exactly like you show,
try the old fantastic sed.
put this in sed-cmd:
{
N
s/\n/ /
}

then run: sed -f sed-cmd filename
sure, sed can more.

:) guggach
 
You can use paste, e.g...
[tt]
paste -s -d"\t\n" file1
[/tt]
...or...
[tt]
paste - - < file1
[/tt]
 
In Perl:

Code:
#!/usr/local/bin/perl -w
use strict;
my $output;
open(INFILE, "< /file/to/read/from");
open(OUTFILE, "> /file/to/write/to");

while(<INFILE>){
 chomp;
 if ($_ =~ /PKGINST/){
    $output = $_;
 }elsif($_ =~ /VERSION/){
    $output .= $_
    print OUTFILE $output."\n";
 }
}

___________________________________
[morse]--... ...--[/morse], Eric.
 
Thank you all, With all of your hints, I also got an awk way:

awk '/PKGINST/ {printf $0"\t"}; /VERSION/ {print $0}' f1

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top