Sponsored Content
Top Forums Shell Programming and Scripting Merge CSV files and create a column with the filename from the original file Post 302521044 by Klashxx on Tuesday 10th of May 2011 05:19:14 AM
Old 05-10-2011
A Perl:
Code:
#!/usr/bin/perl
my $result_file=shift;
my $pattern=shift;
my (@file_list) = glob $pattern;

open( OUT, ">$result_file") or die "Can't open '$result_file':$!";
foreach my $in_file (@file_list) { 
   open(IN, "$in_file"); 
   while (<IN>) {
      chomp;
      print OUT $_ . ',' . $in_file . "\n" ;
      }
   close (IN);
   }

close (OUT);

Usage:
Code:
ex.pl result.txt 'file*csv'

This User Gave Thanks to Klashxx For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

merge two two txt files into one file based on one column

Hi, I have file1.txt and file2.txt and would like to create file3.txt based on one column in UNIX Eg: file1.txt 17328756,0000786623.pdf,0000786623 20115537,0000793892.pdf,0000793892 file2.txt 12521_74_4.zip,0000786623.pdf 12521_15_5.zip,0000793892.pdf Desired Output ... (5 Replies)
Discussion started by: techmoris
5 Replies

2. Shell Programming and Scripting

Merging files to create CSV file

Hi, I have different files of the same type, as: Time: 100 snr: 88 perf: 10 other: 222 Each of these files are created periodically. What I need to do is to merge all of them into one but having the following form: (2 Replies)
Discussion started by: Ravendark
2 Replies

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

4. Shell Programming and Scripting

How to create a CSV File by reading fields from separate files

SHELL SCRIPT Hi, I have 3 separate files within a folder. Every File contains data in a single column like File1 contains data mayank sushant dheeraj File2 contains DSA_AT MG_AT FLAT_09 File3 contains data 123123 232323 (2 Replies)
Discussion started by: mayanksargoch
2 Replies

5. Shell Programming and Scripting

create new column for filename

Hi, I created a list with 2 columns. Each line is from a different file. I am getting these with a loop in Perl. I would like to add a 3rd column with the name of the file that the line is coming from. I usually use pr to print the filename but this is not working here ... I was wondering if... (5 Replies)
Discussion started by: danieladna
5 Replies

6. UNIX for Dummies Questions & Answers

How to create a .csv file from 2 different .txt files?

Hi, I need to create a .csv file from information that i have in two different tab delimited .txt file. I just want to select some of the columns of each .txt file and paste them into a .cvs file. My files look like: File 1 transcript_id Seq. Description Seq. Length ... (2 Replies)
Discussion started by: alisrpp
2 Replies

7. Shell Programming and Scripting

Merge different files into the original file

Hello Below is my requirement I have 3 files A1.txt , A2.txt and A3.txt . A2 is dynamically generating file I want the merge of A1,A2 and A3 in A2.txt Could you please help? (3 Replies)
Discussion started by: Pratik4891
3 Replies

8. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

9. UNIX for Dummies Questions & Answers

Merge two csv files using column name

Hi all, I have two separate csv files(comma delimited) file 1 and file 2. File 1 contains PAN,NAME,Salary AAAAA5467D,Raj,50000 AAFAC5467D,Ram,60000 BDCFA5677D,Kumar,90000 File 2 contains PAN,NAME,Dept,Salary ASDFG6756T,Karthik,ABC,450000 QWERT8765Y,JAX,CDR,780000... (5 Replies)
Discussion started by: Nivas
5 Replies

10. Shell Programming and Scripting

I am trying to merge all csv files from source path into 1 file

I am trying to merge all csv files from source path into one single csv file in target. but getting error message: hadoop fs -cat /user/hive/warehouse/stage.db/PK_CLOUD_CHARGE/TCH-charge_*.csv > /user/hive/warehouse/stage.db/PK_CLOUD_CHARGE/final/TCH_pb_charge.csv getting error message:... (0 Replies)
Discussion started by: cplusplus1
0 Replies
NetSDS::Util::File(3pm) 				User Contributed Perl Documentation				   NetSDS::Util::File(3pm)

NAME
NetSDS::Util::File - file related utilities SYNOPSIS
use NetSDS::Util::File qw(file_read); my $passwd = file_read('/etc/passwd'); file_move('/etc/passwd', '/tmp/find_this'); DESCRIPTION
"NetSDS::Util::File" module contains some routines for files and directories processing tasks like creating, reading, writing, copying and moving files and catalogs. This module of cource uses such well known things like File::Spec, File::Path, File::Copy and others. EXPORTED FUNCTIONS
is_handle($var) - check if argument is a file handle Paramters: some variable Returns: 1 if it's file handle or undef otherwise if (is_handle($var)) { reset_handle($fh); } reset_handle($fh) - reset file handle Paramters: file handle Returns: nothing This function tries to set filehandle to begin of file and set binmode on it. my $fh = file_open('/etc/passwd'); ... do something with file ... reset_handle($fh); # We can read it from the beginning file_open($file) - open file Paramters: file name or file handle Returns: file handle This function provides unified API for opening files. my $f = file_open('/etc/passwd'); file_read($file) - read file to scalar Paramters: file name or file handle Returns: scalar content of file This function provides ability to read file content to scalar variable. my $data = file_read('/etc/passwd'); print "Passwords file: $data "; file_write($file, $data) - write scalar data to file Paramters: file name or open file handle Returns: length of written data or undef in case of error my $data = 'This should be file'; file_write('/tmp/file.dat', $data); file_copy($in_file, $out_file) - copy file Paramters: input file name, output file name Returns: This function copy file to new location. file_move($in_file, $out_file) - move file Paramters: input file name, output file name Returns: 1 or undef This function moves old file to new location. file_temp($dir) - create temporary file Creates new temp file and return its handle dir_create($dir) - create directory with parents Paramters: directory name Returns: directory name or undef # Will create all parent catalogs if necessary dir_create('/var/log/NetSDS/xxx'); dir_delete($dir) - remove directory recursive Paramters: directory name Returns: dir name or undef if error print "We need no libs!"; dir_delete('/usr/lib'); dir_read($dir, $ext) - read files list from catalog Paramters: directory name, extension of files to read Returns: list of files in catalog my @logs = @{ dir_read('/var/log/httpd', 'log') }; print "Logs are: " . join (', ', @logs); dir_read_recursive($dir, $ext, [$res]) - read all files list recursive Paramters: $start catalog, $extension Returns: list of files with extension from parameters my $tpls = dir_read_recursive('/etc/NetSDS', 'tmpl'); foreach my $tpl (@$tpls) { pritn "Template: $tpl "; } exec_external($prog, [$param1, ... $paramN]) - execute external program Paramters: pragram name, arguments list (see perldoc -f system) Returns: 1 if ok, undef otherwise This function calls system() with given parameters and returns 1 if everything happened correctly (program executed and returned correct result). if (exec_external('/bin/rm', '-rf', '/')) { print "Hey! We removed the world!"; } EXAMPLES
None yet BUGS
Unknown yet SEE ALSO
IO::Handle, IO::Scalar, IO::File, File::Spec, File::Copy, File::Path, system() TODO
1. Implement more detailed error handling AUTHOR
Valentyn Solomko <pere@pere.org.ua> Michael Bochkaryov <misha@rattler.kiev.ua> perl v5.12.4 2011-08-27 NetSDS::Util::File(3pm)
All times are GMT -4. The time now is 03:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy