Adding character to spaces in a table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding character to spaces in a table
# 8  
Old 05-15-2008
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 -
# 9  
Old 05-15-2008
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
# 10  
Old 05-15-2008
Hi penchal_boddu,

Thks but i am using csh and awk.
Do you have codes in csh or awk version ?
# 11  
Old 05-15-2008
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.
Smilie
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) }
                }
        }
}

# 12  
Old 05-16-2008
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 05:08 AM..
# 13  
Old 05-16-2008
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

# 14  
Old 05-18-2008
Quote:
Originally Posted by Klashxx
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-18-2008 at 01:34 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

2. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

3. Programming

Oracle: adding numbering to a table...

As a disclaimer, I am not a database person. I have some basic knowledge, but my area is in other fields. Please treat me like I am stupid when it comes to this question. An Oracle script has been dumped on me, which I have been able to work out an understanding of, but I need to make a change... (5 Replies)
Discussion started by: Elric of Grans
5 Replies

4. Shell Programming and Scripting

Help with awk adding spaces.

I have a file that contains... elm,mail elm,lisp,composer,cd,ls,cd,ls,cd,ls,zcat,|,tar,-xvf,ls,cd,ls,cd,ls,vi,ls,cd,ls,vi,elm,-f,ls,rm,ls,cd,ls,vi,vi,ls,vi,ls,cd,ls,elm,cd,ls,cd,ls,vi,vi,vi,ls,vi,ls,i,vi,ls,cp,cd,fg,ls,rm,cd,ls,-l,exit elm,mail,biff,elm,biff,elm,elm elm,ls ... (2 Replies)
Discussion started by: Bandit390
2 Replies

5. Shell Programming and Scripting

adding spaces for a variable value

Hi, i have to form the header and add fillers(spaces) to it. I have done something like this. i have added 10 spaces at the end HDR="AAAABBBBCCNN " echo $HDR >> file1.dat but the spaces are not being stored in the file. How to add the spaces. (2 Replies)
Discussion started by: dnat
2 Replies

6. Shell Programming and Scripting

Adding spaces to record

Hi, I want to print spaces in a trailer record which is a single command. namely the unix command which i already have recs=`wc -l $TargetFileDir/myfile.txt|cut -c1-9`;export recs;echo 'PCPC.DXDINPT.FC0.INPUTFLE.PASS'`date +%Y%m%d``printf '%015d\n' $recs` >> $TargetFileDir/myfile1.txt I... (3 Replies)
Discussion started by: nvenkat010
3 Replies

7. IP Networking

Adding an extra route to the ip routing table

In my college dorm, there is a file sharing network in the entire building. Problem is, there is only a manual for windows with the settings on how to connect... :mad: They say that you have to give the following command in cmd in windows: route add 172.16.71.0 mask 255.255.255.0... (2 Replies)
Discussion started by: Japie89
2 Replies

8. Shell Programming and Scripting

adding spaces to a line

Is there any command to add spaces to a lline....say i need 50 spaces between the data like "aaabbbccc dddeeefff" or may be like this "aaaabbbbbbcccccdddddeeeffff " your help is appreciated. (4 Replies)
Discussion started by: mgirinath
4 Replies

9. Programming

Removing empty spaces and adding commas

I have a file which contains numbers as follows: 1234 9876 6789 5677 3452 9087 4562 1367 2678 7891 I need to remove the empty spaces and add commas between the numbers like: 1234,9876,6789,5677,3452, 9087,4562,1367,2678,7891 Can anyone tell me the command to do... (4 Replies)
Discussion started by: jazz
4 Replies

10. UNIX for Dummies Questions & Answers

Adding Trailing Spaces to a file

I have a text file which is not fixed width. I want to put trailing spaces to each line and make it a 100 byte fixed width file. Can someone please help me as soon as possible? Thanks, Denis (1 Reply)
Discussion started by: 222001459
1 Replies
Login or Register to Ask a Question