pasting two files while transposing one of them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pasting two files while transposing one of them
# 1  
Old 11-26-2010
pasting two files while transposing one of them

hey,

I have more a structural problem. I have two input files:
1.inp:

1 2 3 a b c
2 3 4 d f g

and the 2.inp

6 6 6
7 7 7
8 8 8

The goal is to get as much output files (with a name 1_2_3.dat) as lines in 1.inp are like this:

6 6 6 a
7 7 7 b
8 8 8 c

The output in 2_3_4.dat would be then:
6 6 6 d
7 7 7 f
8 8 8 g

So. I know this is a bit confusing, but thats how it is.
My script looks like this:

Code:
cat grid_ill.pts| awk '{print $1, $2, $3}'> ds_erg_falsecolor.dat


paste 1.inp 2.inp | awk '{
for (row=1;row<=NR;row++) {
        if (NR==$row){
             for (col=10;col<=NF;col++) {
                arr[NR,col]=$col;
                a=sprintf("echo \"%5$s \" \"%6$s \" \"%7$s \" \"%4$s \">> ds_erg_%1$s_%2$s_%3$s.dat\n", $1, $2, $3, col[row,col], $42542, $42543, $42544)
                print "str", a
                system(a)
                }
            }
        }
}'

Does someone has an idea how to deal with it?
Thanks a lot for any suggestions,
Ergy
# 2  
Old 11-26-2010
Code:
while read a b c d e f
do
echo "$d $e $f" | tr ' ' '\n' | paste -d " " 2.inp - >"$a"_"$b"_"$c".dat
done <1.inp

Code:
# cat in1
1 2 3 a b c
2 3 4 d f g
# cat in2
6 6 6
7 7 7
8 8 8
# cat mtst
while read a b c d e f
do
echo "$d $e $f" | tr ' ' '\n' | paste -d " " in2 - >"$a"_"$b"_"$c".dat
done <in1
# ksh mtst
# ls -rt | tail -2
1_2_3.dat
2_3_4.dat
# cat 1_2_3.dat
6 6 6 a
7 7 7 b
8 8 8 c
# cat 2_3_4.dat
6 6 6 d
7 7 7 f
8 8 8 g
#

# 3  
Old 11-27-2010
With sh,using echo and \n instead of tr, from the script above
Code:
#!/bin/sh

while read a b c d e f; do
    echo "$d\n$e\n$f"| paste -d' ' 2.inp - > "$a"_"$b"_"$c".dat
done < 1.inp

exit 0


Last edited by tukuyomi; 11-27-2010 at 04:45 PM.. Reason: Added [code] x_X
# 4  
Old 11-27-2010
With awk:

Code:
awk 'NR == FNR {
  inp2[NR] = $0
  nr = NR; next
  }
{
  fn && close(fn)
  fn = j = x
  for (i = 0; ++ i <= 3;)
    fn = fn ? fn "_" $i : $i
  fn = fn ".dat"
  for (i = 3; ++ i <= NF;)
      print inp2[++ j], $i > fn
  }' 2.inp 1.inp


Last edited by radoulov; 11-28-2010 at 03:55 AM..
# 5  
Old 11-28-2010
Code:
awk '
NR==FNR{a[NR]=$0;s=NR;next} 
{for (i=4;i<=NR;i++) for (j=1;j<=s;j++) print a[j],$(i++) >$1"_"$2"_"$3".dat"}
' 2.inp 1.inp

# 6  
Old 11-30-2010
perl

Code:
open FH,"<file2";
my @arr=<FH>;
@arr=map {s/\n//;$_;} @arr;
close FH;
my $cnt = $#arr;
open FH,"<file1";
while(<FH>){
  chomp;
  my @tmp = split;
  for(my $i=0;$i<=$#arr;$i++){
    print $arr[$i]," ",$tmp[$#tmp-$#arr+$i],"\n";
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pasting multiple files using awk with delimiter

hi, i want to PASTE two files, with a delimiter in between, using awk and pipe the output to another file. i am able to achive the reqirement using PASTE command. but it has a limitation of length till 511 bytes. Example: ------- File1: ---- sam micheal file2: ---- bosco... (11 Replies)
Discussion started by: mohammedsadath
11 Replies

2. Shell Programming and Scripting

pasting problem

Hi Friends, Please help. I have 2 pipe delimited files; each one has different number of rows in it. I want to use "paste" to concatenate these files. However, while doing it, due to difference in number of rows in each files resulting file is in haphazard manner. E.g. file 1 Reliance Info|... (1 Reply)
Discussion started by: anushree.a
1 Replies

3. Shell Programming and Scripting

pasting two files in every directory (+100 directories)

Hi, I have around 400 directories each one named as hour_1/ , hour_2/ .....hour_400/ and each of these contains two files, namely: File1: hour_1.txt (in hour_1/) , hour_2.txt (in hour_2/) ....hour_400.txt (in hour_400/) etc... File2: client_list_hour_1.txt (in hour_1/),... (7 Replies)
Discussion started by: amarn
7 Replies

4. Shell Programming and Scripting

help with pasting files in filesystem

quick question.. say i have few files in D or E drive.. i want to paste them in Filesystem that is /home/vivek folder... but when i try to do that it shows some error saying "There is not enough space on the destination. Try to remove files to make space." but i think its due to authorization... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

reading files and pasting in another text file

Hi all, I have certain task to do, which involves reading the first column of 1.txt file. This is variable "event" 28434710 23456656 3456895 & finding this "event" in some other text file 2.txt, which has information in the following format #Zgamma: 1 run: 160998 event: ... (7 Replies)
Discussion started by: nrjrasaxena
7 Replies

6. Shell Programming and Scripting

Pasting files with different number of lines

Hi all, I tried to use the paste command to paste two files with different number of lines. e.g. file1 A 1 B 1 C 2 D 2 file2 A 2 B 3 C 4 D 4 E 4 (2 Replies)
Discussion started by: f_o_555
2 Replies

7. UNIX for Dummies Questions & Answers

[Q] cutting from one column and pasting to another

I have a file laid out in columns with the first two lines line being: 219 432 4567 219 432 4587 I need to create a single line command to cut the characters in the 5th column and paste them back to the first column in the same file. (Hint:Two good solutions exist, one in which you use a... (9 Replies)
Discussion started by: mcampos7
9 Replies

8. Shell Programming and Scripting

pasting fields from two files into one

i have two files with contents file a 1234,abcf 2345,drft 4444,befr file b tom,3 sam,5 dog,7 i want to print first column of file b and join to file a and get output as below tom,1234,abcf sam,2345,drft dog,4444,befr (2 Replies)
Discussion started by: dealerso
2 Replies

9. Shell Programming and Scripting

Copying and pasting columns from different files

hi all, first time posting, hoping i can get some help on this. I have about 80 text files containing text in this format: # Rg Mass Density Rcm-x Rcm-y Area Rsph/sqrt(2) # == ==== ======= ===== ===== ==== ... (9 Replies)
Discussion started by: Arlamos
9 Replies

10. UNIX for Dummies Questions & Answers

Trouble pasting multiple files together!!

Hi, I would like to paste multiple files together into one large file. I have 23 of them and I would like to link them on a common variable without writing all the file names out (like in a simple join). Each has about 28,000 columns, but only 17 rows. So the final product would be a single file... (2 Replies)
Discussion started by: etownbetty
2 Replies
Login or Register to Ask a Question