Insert text space

I have the following texts in a .txt file,

22/02 23/02 24/02 25/02 26/02
Bike speed (kmph) 004 004 004 004 004

22/02 23/02 24/02 25/02 26/02
Bike speed (kmph) 004 007 007 007 011

I am trying to export it to .csv by doing

cat nic.txt > nic.csv

22/02 23/02 are date and this has to match the kmph speed in second line as follows in .csv file, i.e i need to use 13 space after "Date"

Date        22/02 23/02 24/02 25/02 26/02
Wind speed (kmph) 004 007 007 007 011

how could we do it?

  1. You want to format it neatly or you want a csv?
  2. Here one solution for getting a csv:
sed "s/^\([0-9]\)/Date \1/; s/Bike speed (kmph)/Bike_speed_(kmph)/; s/ /,/g; s/Bike_speed_(kmph)/Bike speed (kmph)/" input
1 Like

thanks balaji,

I want to format it neatly.

Date and bike speed should in one column, 22/02 23/02 24/02 25/02 26/02 should match 004 004 004 004 004 in respective columns

---------- Post updated at 02:26 AM ---------- Previous update was at 02:22 AM ----------

Date 22/02 23/02 24/02 25/02 26/02
Bike speed (kmph) 4 11 11 11 13

---------- Post updated at 02:34 AM ---------- Previous update was at 02:26 AM ----------

sorry tried pasting aligned text, but by default the above edit removed the space

got it

added two spaces after Date, now while opening .csv file in libreoffice, it shows in matching format,

sed "s/^\([0-9]\)/Date \1/; s/Bike speed (kmph)/Bike_speed_(kmph)/; s/Bike_speed_(kmph)/Bike speed (kmph)/" *.txt > nic.csv

thanks again balajesuri (sorry i mispelt your name in earlier post)

how align left by default all the words ? the speed numbers are getting aligned to right while opening the .csv file.

Please use code tags. Your spaces will be retained. Wrap your data samples and codes in code tags. Check this video tutorial: Forum Video Tutorial: How to Use Code Tags

---------- Post updated at 13:55 ---------- Previous update was at 13:38 ----------
Something like this?

$ perl -ne 's/^([0-9])/Date $1/; s/Bike speed \(kmph\)/Bike_speed_kmph/; @x = split /\s+/;
for ($i=0; $i<=$#x; $i++) {printf "%15s", $x[$i]}; print "\n";' inputfile
           Date          22/02          23/02          24/02          25/02          26/02
Bike_speed_kmph            004            004            004            004            004

           Date          22/02          23/02          24/02          25/02          26/02
Bike_speed_kmph            004            007            007            007            011

awesome.

i would appreciate if you have time to brief this code balajesuri, can you please?

@nicolethomson: The crux of the code is printf "%15s" which actually does the beautification work. printf of perl is similar to C's printf.

1 Like

thanks a ton,
along with it, i am going to try few more like creating workbook's.

Let me get back to you shortly.

you made life easier.

---------- Post updated at 08:34 AM ---------- Previous update was at 07:05 AM ----------

#!/usr/bin/perl -w

use strict;
use Spreadsheet::WriteExcel;

# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new("districts.xls");

# Add some worksheets
my $zu = $workbook->addworksheet("zunheboto");
my $ba = $workbook->addworksheet("bangalore");
my $hy  = $workbook->addworksheet("hyderabad");
my $er  = $workbook->addworksheet("kurnool");

# Add a caption to each worksheet
#foreach my $worksheet (@{$workbook->{worksheets}}) {
#   $worksheet->write(0, 0, "Milege");
#}

# zunheboto.txt, bangalore.txt hyderabad.txt kurnool.txt are in this folder, wanted extract info from *.txt and based on filename it ahs to create the workbook and append the data based on the date. getting confused here

my $virudhungr = open ("|sed "s/^\([0-9]\)/Date              \1/; s/Bike speed (kmph)/Bike_speed_(kmph)/; s/Bike_speed_(kmph)/Bike speed (kmph)/" hyderabad.txt");

# Write some data
#echo $virudhungr;
$zu->write($virudhungr);
#$ba->write($hy);
#$hy->write ($ba);
#$er->write ($er);

# Set the active worksheet
#$er->activate();

---------- Post updated at 08:36 AM ---------- Previous update was at 08:34 AM ----------

this will be every five days once i will update. so thought of cron-jobing it.

---------- Post updated 2012-02-22 at 03:19 AM ---------- Previous update was 2012-02-21 at 08:36 AM ----------

not able to do it, how to proceed?

---------- Post updated at 05:49 AM ---------- Previous update was at 03:19 AM ----------

i am sorry franklin52, already one of the senior member warned me, but i missed it out, pls accept my apologies

---------- Post updated at 08:35 AM ---------- Previous update was at 05:49 AM ----------

will those codes work?

i am trying it but, doesnt have much luck, pls help

balaji suri, can you pls help me