|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Data in Rows to Columns
Hi,
I am a beginner in bash&perl. I have data in form of:- A 1 B 2 C 3 D 4 E 5 I would like your help to find a simple way to change it to :- A B C D E 1 2 3 4 5 Any help would be highly appreciated. |
| Sponsored Links | |
|
|
|
#2
|
|||
|
|||
|
Code:
cat abc.txt | perl -e '$i=0 ;
while(<>){
chomp;
($row[$i],$col[$i])= split(" ");
$i++;
}
map {print "$_ " } @row; print"\n";
map {print "$_ " } @col;print "\n";'HTH, PL |
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
Hello umaars: Welcome to the forums. The following one way to accomplish that task using sh scripting: Code:
while read a b; do
x=$x\ $a
y=$y\ $b
done < data
echo $x
echo $yDemonstration: Code:
$ cat data A 1 B 2 C 3 D 4 E 5 $ unset x y $ while read a b; do x=$x\ $a; y=$y\ $b; done < data; echo $x; echo $y A B C D E 1 2 3 4 5 Cheers, Alister |
|
#4
|
||||
|
||||
|
Code:
use strict;
use warnings;
my ($str,$str1);
open FH, "<file" or die "Can't Open $!";
while(<FH>)
{
if(/([A-Z])\s+([0-9])/)
{
$str=$str." $1";
$str1=$str1." $2";
}
}
print "$str\n$str1\n"; |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
You can use the following code also Code:
open FH,"<file" or die "Can't open file : $!\n";
my @res;
while ( <FH> )
{
push(@res,split);
}
for ( my $i=0; $i <= $#res; $i=$i+2 )
{
print "$res[$i] "
}
print "\n\n";
for ( my $i=1; $i <= $#res; $i=$i+2 )
{
print "$res[$i] "
}Last edited by thillai_selvan; 03-14-2010 at 11:48 PM.. |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Code:
awk '
{
for (i=1;i<=NF;i++)
{
arr[NR,i]=$i;
if(big <= NF)
big=NF;
}
}
END {
for(i=1;i<=big;i++)
{
for(j=1;j<=NR;j++)
{
printf("%s%s",arr[j,i], (j==NR ? "" : OFS));
}
print "";
}
}' urfile |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
Using bash script you can get the required output in simple way. Code:
field1="$field1 `cut -d' ' -f 1 file`" field2="$field2 `cut -d' ' -f 2 file`" echo $field1 echo $field2 |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Rearrange columns and rows with awk | Tom46 | UNIX for Dummies Questions & Answers | 3 | 07-17-2009 03:22 AM |
| # of rows and columns | kylle345 | Shell Programming and Scripting | 3 | 07-02-2009 08:15 PM |
| Search for & edit rows & columns in data file and pipe | tintin72 | UNIX for Dummies Questions & Answers | 8 | 03-27-2009 09:26 AM |
| convert rows into columns | loperam | Shell Programming and Scripting | 2 | 12-01-2008 02:57 AM |
| Columns to rows | mgirinath | Shell Programming and Scripting | 16 | 11-29-2007 06:03 PM |
|
|