Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Insert text line to specific location CSV

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-19-2012
whyte_rhyno whyte_rhyno is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 13 December 2012, 5:54 PM EST
Posts: 22
Thanks: 7
Thanked 0 Times in 0 Posts
Insert text line to specific location CSV

In Perl. ***edited question below***

Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file:


Code:
age,name,location

Is it possible to INSERT data into the CSV into the correct age order. For example, if I had the following data:


Code:
#appending the CSV line by line, not all at once
>>10,Jim,London
>>8,Jon,Paris
>>21,Jen,Rome
>>17,Bob,Washington

It would go in the CSV file in age order as follows (youngest to oldest):


Code:
8,Jon,Paris
10,Jim,London
17,Bob,Washington
21,Jen,Rome

I have a problem in that I don't have access to the useful Text::CSV library, so any solutions that do not involve an import would be a massive bonus (it doesn't matter if it's sloppy/inefficient as it's purely for educational purposes)

Thanks for your time.

---------- Post updated at 07:19 PM ---------- Previous update was at 04:17 PM ----------

Ok, so I've given up hope that it's possible to do what I want. Instead, I'm just going to sort the details on the fly; I'm still having some problems though. I've pieced together this sorting algorithm but it's only printing out memory locations (E.g: ARRAY(0x6a33e10))


Code:
    my @data;
    my @sorted;
    open FH, "<details.csv" || die ("Can't open details.csv"); 
    
    while(<FH>) {
        chomp;
        push @data, [split ',', $_]; #split the line
        @sorted = sort { $b->[0] <=> $a->[0] } @data; #sort based on age
        print "$sorted[0]\t$sorted[1]\t$sorted[2]\n";
    }
    
    close FH;

Sponsored Links
    #2  
Old Unix and Linux 11-19-2012
in2nix4life's Unix or Linux Image
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 20 March 2016, 12:01 PM EDT
Location: East Coast
Posts: 621
Thanks: 1
Thanked 177 Times in 163 Posts

Code:
#!/usr/bin/perl
#

use strict;
use diagnostics;

my @data;
my @sorted_data;
my $csvfile = "details.csv";

open(FH, "<$csvfile") or die "Unable to open $csvfile.\n";
@data = <FH>;
@sorted_data = sort { (split ',', $a)[0] <=> (split ',', $b)[0] } @data;
foreach(@sorted_data) { print; }
close(FH);
exit(0);

The Following User Says Thank You to in2nix4life For This Useful Post:
whyte_rhyno (11-19-2012)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
unix command : how to insert text at the cursor location via command line? xib.be UNIX for Advanced & Expert Users 0 12-22-2010 05:45 PM
insert data into specific lines of a CSV tlarkin Shell Programming and Scripting 4 08-26-2010 01:57 PM
Insert a text from a specific row into a specific column using SED or AWK Issemael Shell Programming and Scripting 5 07-14-2009 11:12 AM
Insert 2 lines in a file at a specific location potro Shell Programming and Scripting 2 05-08-2008 07:38 PM
Insert lines at specific location in file hcclnoodles Shell Programming and Scripting 17 03-28-2007 08:00 AM



All times are GMT -4. The time now is 04:33 AM.