Sponsored Content
Top Forums Shell Programming and Scripting Copying and pasting columns from different files Post 302412058 by Franklin52 on Sunday 11th of April 2010 11:28:33 AM
Old 04-11-2010
Quote:
Originally Posted by Arlamos
thanks to both of you.

I don't quite understand the AWK syntax so I tried the perl line and aside from a couple of details it does exactly what I need.

I have only few followup questions.

the perl command is including an extra line with the filename at the bottom of the output from each file:

Code:
500.423065 3.526756 3.dat
503.648834 82.129471 3.dat
506.528046 21.388363 3.dat
510.004608 12.926329 3.dat
  3.dat
21.214518 273.515930 4.dat
33.278835 282.828949 4.dat
29.159582 270.977661 4.dat

how do I get rid of the "extra" 3.dat at the bottom?

How can I specify a range of input files to the perl line, so that instead of
Code:
perl -lane '$.>2 && print "$F[3] $F[4] $ARGV"; close ARGV if eof' 1.dat
perl -lane '$.>2 && print "$F[3] $F[4] $ARGV"; close ARGV if eof' 2.dat

i can perhaps do something like
Code:
perl -lane '$.>2 && print "$F[3] $F[4] $ARGV"; close ARGV if eof' 0.dat .. 82.dat

lastly, I'd like to get rid of the ".dat" file handle from the output if that is at all possible without renaming all the files.

thanks again guys, your knowledge and willingness to help has already saved me hours.
With a little modification of alister's code:

Code:
awk 'FNR==1{sub(".dat","",FILENAME)}FNR>2{print $4, $5, FILENAME}' {0..82}.dat

Explanation of the code:

Code:
FNR==1{sub(".dat","",FILENAME)}

Remove the .dat part of the filename on the 1st line of a file

Code:
awk 'FNR>2

If the line number is greater then 2

Code:
print $4, $5, FILENAME}

print the desired fields

Last edited by Franklin52; 04-11-2010 at 12:55 PM.. Reason: Correcting code
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

copying selected records from two columns to another file

Hey guys I have got a tab-separated file and I want to copy only selected records from two columns at a time satisfying specified condition, and create a new file. My tab separated file is like this ID score ID score ID Score ID score ID score 1_11 0.80 2_23 0.74 2.36 0.78 2_34 0.75 A_34... (9 Replies)
Discussion started by: jacks
9 Replies

3. Shell Programming and Scripting

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 (5 Replies)
Discussion started by: ergy1983
5 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Copying and Pasting columns from one text file to another

I have a tab delimited text file that I want to cut columns 3,4,5 from. Then I want to paste these columns into a space delimited text file between columns 2 and 3. I still want to keep the space delimited format in the final text file. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 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. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy