I have to create a txt file from an Excel sheet , but the txt file should have only selected columns information

I need to convert an excel file to txt or csv file, with only one few selected column information in the txt file. I dont want all the column information in the txt or csv file.

Can you please suggest something as I am new to scripting!! (Better by using Perl or Shell scripting)

Thanks in advance!!

Xavier John Lionel
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you wanted to do this within Excel, using automation, you could simply hide the columns you don't want to export, copy to a new sheet and export the sheet as .csv.

What I generally do, though, is export the entire sheet from Excel as .csv and use a Perl script to modify the data as needed.

### this is just an example fragment,excerpted and  modified from working code 
### but not independently tested

my @OutputFieldList = (

use strict;
use warnings;
no warnings qw(uninitialized); # relies on uninitialized values being equal to ''
use Carp;
use Data::Dumper;

use Text::CSV;

printf "%s\n", join ",", @OutputFieldList;

my $csv1 = Text::CSV->new( { binary => 1, eol => $/ } );
my $row;
my @Label;

open my $io1, "<", $ARGV[0] or croak "Failed to open $ARGV[0]: $!";

while( $row = $csv1->getline( $io1 ) ) {
	my @field = @$row;
	if ( $. == 1 ) {
		@Label = @field;
		$labelcount = scalar @Label;
	my %rowhash = ();
	@rowhash{@Label} = @field;
	#### print STDERR Data::Dumper->Dump( [ \%rowhash ], [ qw( *rowhash ) ] ); #### DEBUG

       printf "%s\n", join ",", map { qq{"$_"} } @rowhash{@OutputFieldList};

Open in new window


This may be way overkill for what you're trying to do, but using the %rowhash this way allows fairly easy-to-understand manipulation of the various fields of the spreadsheet, all sorts of processing beyond simply dropping unwanted fields.            
[The Text::CSV module also provides a getline_hr method that returns rows as hashes, but I found I had to do some of the handling myself to get around some irregularities the way my sources were producing what they considered CSV-format files.]
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
To use shell scripting
1. You can install cygwin (https://www.geocities.ws/murugesandinesh/cygwin_installation_steps.htmlcygwin installation steps)
2. or you can use any Linux oriented operating system( Windows or Linux[all sub types => Redhat Enterprise Linux/CentOS/OpenSUSE/UNIX/...] or AIX or HP-UX or SunOS)

When saving the file using excel.exe mention
Save as type: CSV(Comma delimited)
Assume that the input file name is inputfile.csv

Sample script I have written at Windows bash.exe
$ /bin/cat ./28719841_Query.sh
#Displaying only third column
if test -f inputfile.csv
	OUTPUT_FILE=./Output_''`/bin/date "+%d_%b_%Y_%H_%M_%S_%N"`''".csv"
	if test -f "$OUTPUT_FILE"
		/bin/sleep 1
		OUTPUT_FILE=./Output_''`/bin/date "+%d_%b_%Y_%H_%M_%S_%N"`''
	if test 0 -eq $#
		echo "Displaying all columns from input file"
		/bin/awk -F"," '{
			printf( "%s\n", $0);
		}' inputfile.csv|\
		/usr/bin/tee -a "$OUTPUT_FILE"
		echo "Displaying required columns $@ from input file"
		export RequiredColumns=''`echo "$@" | /usr/bin/tr " " "\n" | /usr/bin/uniq | /usr/bin/tr "\n" " " | /bin/sed "s/ $//;"`''
		/bin/awk -F"," 'BEGIN {
			split( RequiredColumns, CurrentColumn, " ")
			for( EachCol=1; EachCol <=NF; EachCol++)
				if ( 0 != length($EachCol))
					if (EachCol == CurrentColumn[ EachCol])
						if ( 1 == EachCol)
							printf( "%s", $EachCol);
							printf( ",%s", $EachCol);
			printf( "\n");
		}' inputfile.csv |\
		/usr/bin/tee -a "$OUTPUT_FILE"
	echo "Same ouput written in following file:"
	echo "$OUTPUT_FILE"
	echo "~/inputfile.csv No such file"

Open in new window

Providing execute permission for current user:
$ /bin/chmod u+x ./28719841_Query.sh

Open in new window

Executing without input file
$ ./28719841_Query.sh
~/inputfile.csv No such file

Open in new window

I am creating sample input file ~/inputfile.csv
$ echo "1,2,3,4,5,6
A,B,C,D,E,F" > ~/inputfile.csv

Open in new window

Executing without any parameter
$ ./28719841_Query.sh
Displaying all columns from input file
Same ouput written in following file:

Open in new window

Executing required parameters:
$ ./28719841_Query.sh 1 2 3
Displaying required columns 1 2 3 from input file
Same ouput written in following file:

Open in new window

Executing repeated parameters:
$ ./28719841_Query.sh 1 2 2 3 3 4 4 5 5
Displaying required columns 1 2 2 3 3 4 4 5 5 from input file
Same ouput written in following file:

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
>> Better by using Perl or Shell scripting
1. Provided related answer and tested.
2. Question being inactive.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.