The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
adding spaces for a variable value dnat Shell Programming and Scripting 2 02-26-2008 02:32 AM
Adding spaces to record nvenkat010 Shell Programming and Scripting 3 01-28-2008 10:24 AM
Adding an extra route to the ip routing table Japie89 IP Networking 2 10-18-2007 04:16 PM
adding spaces to a line mgirinath Shell Programming and Scripting 4 03-23-2007 09:38 AM
Adding Trailing Spaces to a file 222001459 UNIX for Dummies Questions & Answers 1 11-04-2004 12:23 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 05-15-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 321
Hi all,

Thks for the advice.
I think i have some idea on how to do it.
However, i have another question.
For eg, i have the below table, and i need to list the coordinates of every character in the table. Can any expert help ?

Code:
04  D  H  -  -                                 
03  C  G  K  -                                           
02  B  -  L  -                                                
01  A  E  I  -                                            
   01 02 03 04
Output:
0101 A
0102 B
0103 C
0404 D
0201 E
0202 -
0203 G
0204 H
0301 I
0302 L
0303 K
0304 -
0401 -
0402 -
0403 -
0404 -
Reply With Quote
Forum Sponsor
  #9  
Old 05-15-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 123
Hi try this,

Number=`tail -1 temp | wc -w`
j=2
Target=`expr $Number + 1`
while [ $j -le $Target ]
do
a[$j]=`tail -1 temp | tr -s " " | cut -d" " -f $j`
j=`expr $j + 1`
done

Outer=2
while [ $Outer -le $Target ]
do
Inner=2
while [ $Inner -le $Target ]
do
field=`sed -n "$!/^${a[$Inner]}/p" temp | tr -s " " | cut -d" " -f $Outer`
Index=${a[$Outer]}${a[$Inner]}
echo "$Index $field"
Inner=`expr $Inner + 1`
done
Outer=`expr $Outer + 1`
done


Thanks
Penchal
Reply With Quote
  #10  
Old 05-15-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 321
Hi penchal_boddu,

Thks but i am using csh and awk.
Do you have codes in csh or awk version ?
Reply With Quote
  #11  
Old 05-15-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 321
Hi All,

To make it simpler, we remove the coordinates numbers in the table but keeping in mind those are the coordinates we should be seeing.

This is the input:

Code:
D  H  -  -                                 
C  G  K  -                                           
B  -  L  -                                                
A  E  I  -
I have an awk code below but it doesn;t really get what i wanted.
Can any experts give some advice?

Code:
{
for (x=1;x<=4;x++)
        {
        for (y=1;y<=4;y++)
                {
                #print x;
                #print y;
                if (NR == y ) { printf("%.2d %.2d %s\n", 5 - x, y, $y) }
                }
        }
}
Reply With Quote
  #12  
Old 05-16-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
I answered you in the main thread , anyway:
Code:
sed -ne '1!G;h;$p' file|awk '{
for (i=1;i<=NF;i++)
      printf("%02d%02d %s\n",i,NR,$i)
 }' |sort -k1
0101 A
0102 B
0103 C
0104 D
0201 E
0202 -
0203 G
0204 H
0301 I
0302 L
0303 K
0304 -
0401 -
0402 -
0403 -
0404 -
Regards.

Last edited by Klashxx; 05-16-2008 at 01:08 AM.
Reply With Quote
  #13  
Old 05-16-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 491
Hi,

Below is the shell to reverse a array, it means put row as column and meanwhile put column as row. Hope useful for you!

input:
Code:
a b c d
A B C D
1 2 3 4
output:
Code:
a A 1
b B 2
c C 3
d D 4
code:
Code:
line=`cat file | wc -l`
nawk -v l="$line" '{
c=NF
for(i=1;i<=NF;i++)
{
	t=sprintf("%s%s",NR,i)
	var[t]=$i
}
}
END{
for(i=1;i<=c;i++)
{
	for(j=1;j<=l;j++)
	{
		t=sprintf("%s%s",j,i)
		printf("%s ",var[t])
	}
	print ""
}
}' file
Reply With Quote
  #14  
Old 05-17-2008
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 321
Quote:
Originally Posted by Klashxx View Post
I answered you in the main thread , anyway:
Code:
sed -ne '1!G;h;$p' file|awk '{
for (i=1;i<=NF;i++)
      printf("%02d%02d %s\n",i,NR,$i)
 }' |sort -k1
0101 A
0102 B
0103 C
0104 D
0201 E
0202 -
0203 G
0204 H
0301 I
0302 L
0303 K
0304 -
0401 -
0402 -
0403 -
0404 -
Regards.
Hi Klashxx,

What is the sed portion trying to do ?
Can your code be used if the input is not a square matrix ?
Pardon my ignorance as i am a novice in this area.

I have devised the below code which seems to work for both square and non-square matrix.
Seems that it can work.
Code:
for (y=1;y<=4;y++) 
        { 
        for (x=1;x<=4;x++) 
                {  
                if (NR == y ) { printf("%.2d%.2d %s\n", x, 5 - y, $x) } 
                } 
        } 
}
Hi Summer Cherry,

That' pretty useful. Thks alot!!

Last edited by Raynon; 05-17-2008 at 09:34 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0