Problem in extraction when space is a field delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in extraction when space is a field delimiter
# 8  
Old 08-10-2010
Quote:
Originally Posted by AshwaniSharma09
I simply created a artificial ".txt" file with same content what I have given in my 1st post. your script is working very fine on it. But when I convert ".xls" file into ".txt "or ".csv" file, its not working. Again its taking values from other columns. This is what i did :

mv 10042.xls 10042.csv
OR
mv 10042.xls 10042.txt

Also, above command is for one file. I need it in a way so that it can be run on all files.
Giving the file another name shouldn't work, the file must be save as a csv file with excel.

After processing the file you can save the file back in excel format with excel.
# 9  
Old 08-10-2010
@Franklin52 :
Sorry for being such a pain here but I really don't know how to do that for more than 1000 files.
I am using Fedora 8.
# 10  
Old 08-10-2010
Maybe you can use perl to do the job.

I hope someone with knowledge of perl can put you in right direction.

Regards
# 11  
Old 08-16-2010
Guys, I am still in a real fix. All I want is to extract column from excel files as it is.
If there are blanks in the column, they should be in the output.Smilie
# 12  
Old 08-16-2010
If you want to read excel sheet and print the value of cell, you can try below perl solution (read_excel.pl).

input file - myxl.xls
Code:
field 1	field 2	field 3
test1	1	6
test2	2	
test3	3	7
test4	4	
test5	5	8

Code:
#!/usr/bin/perl

use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;

my $sourcename = shift @ARGV;
my $source_excel = new Spreadsheet::ParseExcel;

my $source_book = $source_excel->Parse($sourcename) or die "Could not open source Excel file $sourcename: $!";
my $storage_book;

foreach my $source_sheet_number (0 .. $source_book->{SheetCount}-1)
{
 my $source_sheet = $source_book->{Worksheet}[$source_sheet_number];

 print "--------- SHEET:", $source_sheet->{Name}, "\n";

 next unless defined $source_sheet->{MaxRow};
 next unless $source_sheet->{MinRow} <= $source_sheet->{MaxRow};
 next unless defined $source_sheet->{MaxCol};
 next unless $source_sheet->{MinCol} <= $source_sheet->{MaxCol};

 foreach my $row_index (1 .. $source_sheet->{MaxRow})
 {
  foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol})
  {
   if ($col_index == 0 or $col_index == 2 ) {
   my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
   if ($source_cell)
   {
    #print "( $row_index , $col_index ) =>", $source_cell->Value, "\n";
    print $source_cell->Value, "\t";
    } # end of source_cell check
   }
  } # foreach col_index
  print "\n";
 } # foreach row_index
} # foreach source_sheet_number

invocation
Code:
perl read_excel.pl myxl.xls

output
Code:
--------- SHEET:Sheet1
test1   6
test2
test3   7
test4
test5   8

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

2. Shell Programming and Scripting

Need to use delimiter as : and space in awk

Hi , Please suggest me how do I use : (colon and one space) as a delimiter in awk Best regards, Vishal (2 Replies)
Discussion started by: Vishal_dba
2 Replies

3. Shell Programming and Scripting

Space as a delimiter

not sure if i'm doing this right i'm new tho this but i'm trying to use a space as a delimiter with the cut command my code is size=$( du -k -S -s /home/cmik | cut -d' ' -f1 ) i've also tried -f2 and switching the -d and -f around if that does anything (3 Replies)
Discussion started by: Cmik
3 Replies

4. Shell Programming and Scripting

Help needed XML Field Extraction

I had an immediate work to sort out the error code and error message which are associated within the log. But here im facing an problem to extract 3 different fields from the XML log can some one please help. I tried using different script including awk & nawk, but not getting the desired output. ... (18 Replies)
Discussion started by: raghunsi
18 Replies

5. Shell Programming and Scripting

deplace field delimiter

hi here my problem: i have 2 file: 1.tmp 111 222 555 2.tmp 1*TEST1**111*LA 2*TEST2**112*LA 3*TEST3**222*LA 4*TEST4**333*LA 5*TEST5**555*LA (5 Replies)
Discussion started by: saw7
5 Replies

6. Shell Programming and Scripting

Add field delimiter for the last field

I have a file with three fields and field delimiter '|' like: abc|12:13:45|123 xyz|12:87:32| qwe|54:21:09 In the file the 1st line has proper data -> abc|12:13:45|123 ,the 2nd line doesnt has data for the 3rd field which is okay , the 3rd line doesnt has data for the 3rd field as well the... (5 Replies)
Discussion started by: mehimadri
5 Replies

7. UNIX for Dummies Questions & Answers

Delimiter: Tab or Space?

Hello, Is there a direct command to check if the delimiter in your file is a tab or a space? And how can they be converted from one to another. Thanks, G (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

8. UNIX for Dummies Questions & Answers

Problem Using Cut With A Space Delimiter

I am trying to extract 'postmaster' from the following string: PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN using the following command: cat /usr/share/assp/assp.cfg | grep ^PenaltyError:= | cut -d '@' -f1 | cut -f8 but it returns: PenaltyError:=554 5.7.1 Error,... (10 Replies)
Discussion started by: cleanden
10 Replies

9. Shell Programming and Scripting

delimiter appears in field

The typical line of the input file is as follows, 123|abcde|"xyz|mn"|ghelosa|3455hello| The delimiter is |. I need to change it to another delimiter, say ~. For the above line, the output should be: 123~abcde~xyz|mn~ghelosa~3455hello~ The challenge is when | appears in a field, it... (2 Replies)
Discussion started by: derekxu
2 Replies

10. Shell Programming and Scripting

Extraction of string from Stringlist using delimiter

Hi Experts, I need to extract some set of strings one be one using delimiter. Example: shellscript.sh|unix.sh|script_file.sh i need to extract this shellscript.sh,unix.sh,script_file.sh separately. I tried but couldn't get. Please help me.. Thanks & Regards :), Kanda (3 Replies)
Discussion started by: spkandy
3 Replies
Login or Register to Ask a Question