Insert text space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text space
# 1  
Old 02-21-2012
Insert text space

I have the following texts in a .txt file,
Code:
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
Code:
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"

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

how could we do it?

Last edited by Franklin52; 02-21-2012 at 03:17 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-21-2012
1. You want to format it neatly or you want a csv?
2. Here one solution for getting a csv:
Code:
sed "s/^\([0-9]\)/Date \1/; s/Bike speed (kmph)/Bike_speed_(kmph)/; s/ /,/g; s/Bike_speed_(kmph)/Bike speed (kmph)/" input

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-21-2012
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.

Last edited by nicolethomson; 02-21-2012 at 03:26 AM.. Reason: tried copying excel contents
# 4  
Old 02-21-2012
Please use code tags. Your spaces will be retained. Wrap your data samples and codes in code tags. Check this video tutorial: https://www.unix.com/how-post-unix-li...code-tags.html

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

Code:
$ 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

# 5  
Old 02-21-2012
awesome.

i would appreciate if you have time to brief this code balajesuri, can you please?
# 6  
Old 02-21-2012
@nicolethomson: The crux of the code is printf "%15s" which actually does the beautification work. printf of perl is similar to C's printf.
This User Gave Thanks to balajesuri For This Post:
# 7  
Old 02-22-2012
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 ----------

Code:
#!/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

Last edited by Franklin52; 02-22-2012 at 05:46 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert space in a word using sed

Hi all, I have a file that looks like this- ----------------------------- . . ATOM 8 O2' U A 5 135.452 109.687 7.148 1.00 48.99 A16S ATOM 9 C1' U A 5 135.282 111.512 5.641 1.00 48.99 A16S ATOM 10 N1 U A 5 134.647 112.595 ... (2 Replies)
Discussion started by: asmi_g
2 Replies

2. Shell Programming and Scripting

How to insert a space and assign the value to another variable?

Hello, I need to insert a space between 2 strings. I used many techniques and all of them worked but when I assign the value to another variable then the inserted space vanishes, strange! Please advise. # dat=`date |awk '{print $2,$3}'` # echo $dat Nov 3 The above is perfectly fine. Now... (4 Replies)
Discussion started by: prvnrk
4 Replies

3. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Shell Programming and Scripting

how to insert space in alphanumeric string

Hi everyone, I want help to insert space between digits and letters in a alphanumeric string. INPUT TRY234TER PHY1TYR EXPECTED OUTPUT TRY 234 TER PHY 1 TYR The lines always begin with the letters and the alphabets will be a three letter combination before and after the number. The... (2 Replies)
Discussion started by: kaav06
2 Replies

5. UNIX for Advanced & Expert Users

Insert 1 space using the command sed

Hi I want to use sed to insert one space after the 10'th character in every line. The lines are on this format: 2012-01-1012:30:55|7323456|65432 2011-02-0313:11:06|1223|3456 ...... ...... Does anyone know sed well enough to acomplish this? If there is any other way around this... (7 Replies)
Discussion started by: ic12
7 Replies

6. UNIX for Dummies Questions & Answers

insert white space between numbers

Hello all, I have a file with several lines like this: (1,1) (4,10) (8,23) (17, 4) (6,8) etc. and I need this: ( 1 , 1 ) ( 4 , 10 ) ( 8 , 23 ) ( 17 , 4 ) ( 6 , 8 ) How do I insert a space between the left parenthesis and the first number, between the first number and the comma,... (2 Replies)
Discussion started by: MDeBiasse
2 Replies

7. Shell Programming and Scripting

Insert space between characters using sed

Input: Youcaneasilydothisbyhighlightingyourcode. Putting space after three characters. You can eas ily dot his byh igh lig hti ngy our cod e. How can i do this using sed? (10 Replies)
Discussion started by: cola
10 Replies

8. Shell Programming and Scripting

Insert space between two words

Hi, I need to insert space between words on my output in UNIX other than the single space given by the space bar on my keyboard, e.g when are you going. (There should be 4 spaces between each of these words) rather than when are you going Can anyone help me with... (3 Replies)
Discussion started by: divroro12
3 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies
Login or Register to Ask a Question