Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Source_FIle:

/path/to file/A_B_C_D_201507290915.csv

Destination_File should be like :

/some/other/path/to file/A_B_C_D_201507290915.csv

I need to transform Source_FIle and storing with the same name in some other location.

Transformation is like converting all the rows of csv in column. CODE used:

#!/bin/bash
var=`echo A_B_C_D_*.csv | grep -oP '(?<=_)\d+(?=\.)'`
awk -F, '{for(i=1;i<=NF;i++){A[NR,i]=$i};if(NF>n){n=NF}}
END{for(i=1;i<=n;i++){
for(j=1;j<=NR;j++){
s=s?s","A[j,i]:A[j,i]}
print s;s=""}}' A_B_C_D_*.csv > /some/other/path/to file/A_B_C_D_$var.csv

It is working fine for one file but in case of multiple source files its giving ambiguous redirect error.

share|improve this question
    
Are you transposing the file ? –  User112638726 Aug 4 at 10:29

3 Answers 3

up vote 2 down vote accepted

The problem is with your var variable. If you want to stick to it, rather assign it to an array.

Code:

#!/bin/bash
var=`echo A_B_C_D_*.csv | grep -oP '(?<=_)\d+(?=\.)'`
arr=($var)
for i in "${arr[@]}"
do

    awk -F, '{for(i=1;i<=NF;i++){A[NR,i]=$i};if(NF>n){n=NF}}
    END{for(i=1;i<=n;i++){
    for(j=1;j<=NR;j++){
    s=s?s","A[j,i]:A[j,i]}
    print s;s=""}}' A_B_C_D_$i.csv > /some/path_to/dest/A_B_C_D_$i.csv

done
share|improve this answer
    
Yeah you are right its working fine now . –  hdk0310 Aug 4 at 11:50

You can use the filename variable in awk

Not sure what you are setting var for so i will leave that out

awk -F, '{
          for(i=1;i<=NF;i++)A[NR,i]=$i
          if(NF>n)n=NF
         }
         ENDFILE{
              sub(/.*\//,"",FILENAME)
              for(i=1;i<=n;i++){
                  for(j=1;j<=NR;j++){
                      s=s?s","A[j,i]:A[j,i]
                  }
                  print s > "NEWPATH" FILENAME;s="" 
              }
         }' A_B_C_D_*.csv
share|improve this answer
2  
this will use the last filename, modern awk have a ENDFILE clause that should be use. –  Archemar Aug 4 at 11:23
    
@Archemar Yeah ,my bad, thanks for the edit :) –  User112638726 Aug 4 at 12:47
    
@Archemar, well I am not so sure if this isn't yet another GNUism (i. e. gawk)...as I prefer my awk lines to remain portable, I always have to care for this fact... –  syntaxerror Aug 8 at 19:46

Use basename, part of coreutils.

Using bash:

for i in $(<file_with_source_file_names) 
 do  
    mv $i /some/other/path/to\ file/$(basename $i) 
 done

Or you could use cp instead of mv. Your choice.

share|improve this answer
    
OP have a transformation step. (awk ... ) –  Archemar Aug 4 at 12:13
1  
OP has a task and is overcomplicating it. –  Allen Aug 4 at 12:18
    
@Allen and OP seems to always use new username per question ;-) look this "question". Same user. 100.0% sure. –  syntaxerror Aug 8 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.