[Solved] Paste with filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Paste with filename
# 1  
Old 02-12-2014
[Solved] Paste with filename

Hi all,

please help, I have several folders in a directory that have the same set of files (content different but names same). I want to use the paste command to merge the 5th column of all files named xyz from all directories with the directory name as col header.
Code:
for dir in *
do
[ -d "$dir" ] && paste all 5th cols (side by side) from all files with name xyz
done

Code:
more dir1/xyz

1 2 3 4 5
11 22 33 44 55

more dir2/xyz
6 7 8 9 10
66 77 88 99 1010

more dir3/xyz
11 12 13 14 15

output should be

Code:
dir1 dir2 dir3
5 10 15
55 1010


Last edited by gina.lizar; 02-12-2014 at 03:27 PM..
# 2  
Old 02-12-2014
Here is an awk approach that might work:
Code:
awk '
        FNR == 1 {
                c += 1
        }
        {
                A[c,FNR] = $5
                m = m < FNR ? FNR : m
        }
        END {
                while ( ++j <= m )
                {
                        while ( ++i <= c )
                        {
                                printf "%s\t", A[i,j]
                        }
                        printf "\n"
                        i = 0
                }
        }
' dir[1-3]/xyz*

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-12-2014
Hi Yoda,
can be directory names generalized ? That was just an example, but the actual directory names do not have a pattern to use. Also This is to be done for all directories in the folder .

Edit..I used ./*/xyz*It seems to work fine, but I want the directory names as the column headers and not the actual column names..

Last edited by gina.lizar; 02-12-2014 at 03:41 PM..
# 4  
Old 02-12-2014
Try this:
Code:
awk '
        FNR == 1 {
                c += 1
                d = FILENAME
                sub ( /\/.*/, X, d )
                D[c] = d
        }
        {
                A[c,FNR] = $5
                m = m < FNR ? FNR : m
        }
        END {
                while ( ++k <= c )
                        printf "%s\t", D[k]
                printf "\n"

                while ( ++j <= m )
                {
                        while ( ++i <= c )
                        {
                                printf "%s\t", A[i,j]
                        }
                        printf "\n"
                        i = 0
                }
        }
' */xyz*

This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-12-2014
thank you, this works great !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Modifying/Removing Timestamps from a filename

So given filenames of varying lengths, I was wondering how I would remove or modify appended timestamps of the current date DD-MM-YY. So say: test_DD-MM-YY.txt coolbeans_DD-MM-YY.pdf And what I expect the output to be: test.txt coolbeans.pdf Thanks :) (2 Replies)
Discussion started by: sodaboyz
2 Replies

2. Shell Programming and Scripting

[Solved] Replace extension of filename stored in a variable

Hi there, I have a small question (most like a true beginners question :) ). In a script I have a filename stored in variable (vFile). Through the an input parameter this variable gets its value (for instance cookie.txt). Two new variables are created with the value of vFile, but with a... (2 Replies)
Discussion started by: rberkers
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Appending beginning of filename to end

Hi Guys, I have serveral directories like this: (2013) blablabla(blabla) - blabla (blabla) or (1997) blablabla(blabla) - blabla (blabla) and have to rename them to something like that: blablabla(blabla) - blabla (blabla) (2013) and blablabla(blabla) - blabla (blabla) (1997) Easy... (2 Replies)
Discussion started by: Nateshift
2 Replies

4. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

7. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

8. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies

9. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

10. UNIX for Dummies Questions & Answers

shortcut for tar cvf - [filename] | gzip > [filename].tar.gz

i'd like to have an alias (or something similar) where i can type a command like "archive" and a filename and have it tar and gzip the file, so... $ archive filename results in filename.tar.gz...do i have to write a script to do this? (4 Replies)
Discussion started by: bcamp1973
4 Replies
Login or Register to Ask a Question