Randomly inserting extra columns into csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Randomly inserting extra columns into csv file
# 8  
Old 09-19-2013
Perhaps perl is they way to go if your on AIX or Solaris:

Code:
#!/usr/bin/perl
use Time::Local 'timelocal';
use POSIX;

my $year, $month, $day;
my $sd, $ed;
$sd=1167573600;
$ed=1577800800;

while (my $ln = <STDIN>) {
  if($ln =~ /([^;]+;){4,}/) {
    my @values = split(/\;/, $ln);
    $values[3] = $values[3] .
       ";" . strftime("%Y%m%d", localtime(rand()*($d-$sd)+$sd)) .
       ";" . strftime("%Y%m%d", localtime(rand()*($d-$sd)+$sd)) .
       ";" . strftime("%Y%m%d", localtime(rand()*($d-$sd)+$sd));
    $values[5]= $values[5] . ";" . (rand()>0.5)?"Y":"N";
    print $values[0];
    for($i=1 ; $i < scalar(@values); $i++ ) {
        printf ";$values[$i]"
    }
  } else {
      print $ln;
  }
}

This User Gave Thanks to Chubler_XL For This Post:
# 9  
Old 09-20-2013
This solution worked. Thanks a lot.

Smilie
Quote:
Originally Posted by Chubler_XL
Perhaps perl is they way to go if your on AIX or Solaris:

Code:
#!/usr/bin/perl
use Time::Local 'timelocal';
use POSIX;

my $year, $month, $day;
my $sd, $ed;
$sd=1167573600;
$ed=1577800800;

while (my $ln = <STDIN>) {
  if($ln =~ /([^;]+;){4,}/) {
    my @values = split(/\;/, $ln);
    $values[3] = $values[3] .
       ";" . strftime("%Y%m%d", localtime(rand()*($d-$sd)+$sd)) .
       ";" . strftime("%Y%m%d", localtime(rand()*($d-$sd)+$sd)) .
       ";" . strftime("%Y%m%d", localtime(rand()*($d-$sd)+$sd));
    $values[5]= $values[5] . ";" . (rand()>0.5)?"Y":"N";
    print $values[0];
    for($i=1 ; $i < scalar(@values); $i++ ) {
        printf ";$values[$i]"
    }
  } else {
      print $ln;
  }
}

---------- Post updated at 11:23 PM ---------- Previous update was at 11:22 PM ----------

This solution worked. Thanks a lot. great help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to Insert three extra columns in csv file

Hello Experts, I got a requirement i have a input file which am getting from different source,Now i want to add extra 3 columns to this file like BASE,ACTUAL and DATE. Input File Looks like QUAL CHGE TYP LAW COM1 COM2 A 1 X SED HO ASE B 3 Z CDE SE ... (5 Replies)
Discussion started by: ahmed.vaghar
5 Replies

2. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

3. Shell Programming and Scripting

Inserting blank columns in already present CSV file

Hi, i have a csv file which have headers and values of it like below : headers --> CI Ref SerialNumber LastScanDate values --> VMware-42,VMware-42,Tue, 20 May 2014 11:03:44 +0000 i want to have a above csv in below format : headers --> CI Name CI Description CI Ref... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

4. Shell Programming and Scripting

Inserting new fields to a csv file

hi I have a csv file with few rows > cat job_stat 1,jobname1,somthing,somthing 2,jobname2,somthing,somthing 3,jobname3,somthing,somthing 4,jobname4,somthing,somthing I want to add few columns after the 2nd column and then append rest of the columns after the 3rd newly added... (3 Replies)
Discussion started by: midhun19
3 Replies

5. UNIX for Dummies Questions & Answers

To Add extra commas to a CSV file.

Hi All, I got this requirement to process a complex CSV file. Eg File. Line 1: Name:,XYz Line 2: Age:,15 Line 3: Grade:,7 Line 4: Line 5: English, Maths, Science,Spanish Line 6:10,11,13,14 As you can see the maximum column is 4 . The file i need to make is Line 1: Name:,XYz,,... (12 Replies)
Discussion started by: chillblue
12 Replies

6. UNIX for Dummies Questions & Answers

To Add extra commas to a CSV file using 2 files...

Hi , Based on my previous requirement the code works fine for comma as delimiter. Now my Req is widened up a bit .. There will be two set of files .. one with comma as delimiter and other with semi-colon ; as delimiter. Second Sample file. With Double Quotes (Semi-Colon... (1 Reply)
Discussion started by: chillblue
1 Replies

7. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

8. Shell Programming and Scripting

Ascii Mode appending extra records to csv file

I am relatively new to this forum and Unix scripting. ksh script: part 1 :will call a PL\SQL program will create 3 CSV file at the unix directory. part 2 : will sftp the files to the EFT server. Once the EFT server receives these file , it will transfer them to a shared windows folders. ... (3 Replies)
Discussion started by: developerpa
3 Replies

9. Shell Programming and Scripting

Inserting 2 columns from a file to another with nawk

Hello all, I have these 2 files File1 123 100 456 200 789 300 File2 |1|2|3||4|5||6| |1|2|3||4|5||6| |1|2|3||4|5||6| I need an output like : |1|2|3|123|4|5|100|6| |1|2|3|456|4|5|200|6| |1|2|3|789|4|5|300|6| (3 Replies)
Discussion started by: rany1
3 Replies

10. Shell Programming and Scripting

use awk to read randomly located columns in an excel file

Hi, I have an excel file that have a random count of columns/fields and what im trying to do is to only retrieve all the rows under 2 specific field headers. I can use the usually command for awk which is awk 'print{ $1 $2}' > output.txt, but the location of the 2 specific field headers is... (9 Replies)
Discussion started by: mdap
9 Replies
Login or Register to Ask a Question