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

Regards
Xavier John Lionel
xavierjohndrxAsked:
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.

jmcgOwnerCommented:
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 = (
"Lastname",
"Firstname",
"Department",
);

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;
		next;
		}
			
	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.]
0
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
#!/bin/bash
#Displaying only third column
if test -f inputfile.csv
then
	OUTPUT_FILE=./Output_''`/bin/date "+%d_%b_%Y_%H_%M_%S_%N"`''".csv"
	if test -f "$OUTPUT_FILE"
	then
		/bin/sleep 1
		OUTPUT_FILE=./Output_''`/bin/date "+%d_%b_%Y_%H_%M_%S_%N"`''
	fi
	if test 0 -eq $#
	then
		echo "Displaying all columns from input file"
		/bin/awk -F"," '{
			printf( "%s\n", $0);
		}' inputfile.csv|\
		/usr/bin/tee -a "$OUTPUT_FILE"
	else
		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 {
			RequiredColumns=ENVIRON["RequiredColumns"];
		}
		{
			split( RequiredColumns, CurrentColumn, " ")
			for( EachCol=1; EachCol <=NF; EachCol++)
			{
				if ( 0 != length($EachCol))
				{
					if (EachCol == CurrentColumn[ EachCol])
					{
						if ( 1 == EachCol)
						{
							printf( "%s", $EachCol);
						}
						else
						{
							printf( ",%s", $EachCol);
						}
					}
				}
			}
			printf( "\n");
		}' inputfile.csv |\
		/usr/bin/tee -a "$OUTPUT_FILE"
	fi
	echo "Same ouput written in following file:"
	echo "$OUTPUT_FILE"
else
	echo "~/inputfile.csv No such file"
fi

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
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
1,2,3,4,5,6
a,b,c,d,e,f
A,B,C,D,E,F
Same ouput written in following file:
./Output_08_May_2017_11_50_37_388676800.csv

Open in new window

Executing required parameters:
$ ./28719841_Query.sh 1 2 3
Displaying required columns 1 2 3 from input file
1,2,3
a,b,c
A,B,C
Same ouput written in following file:
./Output_08_May_2017_11_50_50_780519100.csv

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
1,2,3,4,5
a,b,c,d,e
A,B,C,D,E
Same ouput written in following file:
./Output_08_May_2017_11_51_03_240323600.csv

Open in new window

0

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.
0
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.