Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Insert text line to specific location CSV

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 11-19-2012   -   Original Discussion by whyte_rhyno
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:


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

#appending the CSV line by line, not all at once

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


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))

    my @data;
    my @sorted;
    open FH, "<details.csv" || die ("Can't open details.csv"); 
    while(<FH>) {
        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
Old Unix and Linux 11-19-2012   -   Original Discussion by whyte_rhyno
in2nix4life's Unix or Linux Image
in2nix4life in2nix4life is offline
Registered User
Join Date: Oct 2007
Last Activity: 9 October 2016, 12:41 PM EDT
Location: East Coast
Posts: 621
Thanks: 1
Thanked 177 Times in 163 Posts


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; }

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

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 06:45 PM
insert data into specific lines of a CSV tlarkin Shell Programming and Scripting 4 08-26-2010 02: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 12:12 PM
Insert 2 lines in a file at a specific location potro Shell Programming and Scripting 2 05-08-2008 08:38 PM
Insert lines at specific location in file hcclnoodles Shell Programming and Scripting 17 03-28-2007 09:00 AM

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