LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-27-2014, 08:47 PM   #1
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Rep: Reputation: Disabled
How to Remove Column in Perl


Hi. I'm new with Perl.

What is the command to remove column in Perl? I know we can use awk in unix but I need to create the script in Perl.

Example. File1:

aaa 1 aa11
bbb 2 bb22
ccc 3 cc33
ddd 4 dd44

I want the output of File2 to be:

aaa 1
bbb 2
ccc 3
ddd 4

I did search but could not find the answer for my type of question.
Thank you in advance!
 
Old 11-28-2014, 02:18 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
I am pretty sure one of the 446000+ entries found by google to 'remove column in Perl' would work.
 
1 members found this post helpful.
Old 11-28-2014, 10:48 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,961

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by newbi2014 View Post
Hi. I'm new with Perl.
What is the command to remove column in Perl? I know we can use awk in unix but I need to create the script in Perl.

Example. File1:

aaa 1 aa11
bbb 2 bb22
ccc 3 cc33
ddd 4 dd44

I want the output of File2 to be:

aaa 1
bbb 2
ccc 3
ddd 4

I did search but could not find the answer for my type of question. Thank you in advance!
We will be happy to help you, but as grail pointed out, there are MANY thousands of Perl guides you can easily find, so I seriously doubt you couldn't find ANYTHING with a Google search. Also, be aware that Perl was developed SPECIFICALLY to parse text, so what you're trying to do is trivial. As a hint, you can use the 'split' function in Perl to get the different columns into variables, then print those out. There is abundant documentation on how to use split, and how to open files for reading/writing.

Post what YOU have written and done on your own, and we can help..but we will NOT write your programs for you.
 
1 members found this post helpful.
Old 11-30-2014, 02:39 AM   #4
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hi all,

I am not asking for the script just the command name. I tried the 'spilt' command. It prints out the columns that I want but it says :

Use of uninitialized value in concatenation (.) or string at ff line 16, <IN> line 4.
Use of uninitialized value in concatenation (.) or string at ff line 16, <IN> line 4.

Use of uninitialized value in concatenation (.) or string at ff line 16, <IN> line 5.
Use of uninitialized value in concatenation (.) or string at ff line 16, <IN> line 5.

Use of uninitialized value in concatenation (.) or string at ff line 16, <IN> line 6.
Use of uninitialized value in concatenation (.) or string at ff line 16, <IN> line 6.


Below are my codes for 2 files. The input and output.

Input:

WD0001 1022 COI single copy
WD0002 930 rpb2 single copy
WD0003 200 IS5 multiple copy


Output:
#!/usr/bin/perl

use warnings;
use strict;

# open the input file
open(IN, "ee") or die;

# go through the file line by line
while (my $line = <IN>) {

chomp $line;

# split the line on tabs
my @columns = split(/\t/, $line);

# print out the first and third columns
print "$columns[0]\t$columns[2]\n";
}

# close the filehandle
close IN;

exit;

 
Old 11-30-2014, 06:39 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Firstly, please us[code][/code] tags around code or data to make it more readable.

As for your error, it is advising that you have un-initialised variables and the best reason I would see for this is that your input is not tab separated, hence
the split is returning the entire line as opposoed to separate columns.
 
1 members found this post helpful.
Old 12-02-2014, 12:34 AM   #6
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks. I have managed to print the columns that I want.

Is there any ways (just the command name) to REMOVE the unwanted column rather than displaying the columns that I want?

I want to remove the last column so that I can create hash using column 1 and 2.

Thank you.
 
Old 12-02-2014, 04:49 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
I would say there is no column in perl, just in your app (if you implement something like that), therefore you need to implement the handling of it (especially removing it)
 
1 members found this post helpful.
Old 12-02-2014, 09:54 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,961

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by newbi2014 View Post
Hi,
Thanks. I have managed to print the columns that I want.

Is there any ways (just the command name) to REMOVE the unwanted column rather than displaying the columns that I want? I want to remove the last column so that I can create hash using column 1 and 2.
Certainly. You can use the Data:umper function (most flexible, and easier to use if you have changing data later), or you can just run the file through sed and remove what you don't want.

Since you provided the script and showed effort of your own, a quick sample of the Data:umper usage:
Code:
use Data::Dumper;

#make a list of the fields you want
my @fieldswanted = qw/Column1 Column3/;

my @fields = split /\t/, <DATA>;  # Assumes tab-separated, reads data into the <DATA> structure
while (<DATA>) {
	# Read each line from the input file and assign each field to its matching header name
	my %row;
	@row{@fields} = split /\t/;
	
	# map each wanted field in the hash for the matching data on this row
	my @wanted_data = map {$row{$_}} @fieldswanted;
	print join("\t", @wanted_data), "\n";
}
 
1 members found this post helpful.
Old 12-03-2014, 12:50 AM   #9
newbi2014
LQ Newbie
 
Registered: Nov 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hi all,

I've figured out how to delete columns.
I just add this command under my while. It named my first column as $1 and second column as $2.

Code:
$line =~ s/(\S*)\s*(\S*).*/$1 $2/g;
Thanks!
 
Old 12-03-2014, 03:17 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
actually your original split command would work also using \s* instead of \t. But I guess only
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove lines according a column? fantasy1215 Programming 2 06-12-2014 09:04 AM
[ask awk] remove certain row in a column dhodho Programming 18 06-08-2010 10:36 AM
remove duplicate entries from first column?? kadvar Programming 2 05-12-2010 07:22 PM
big brother remove column BFCsaus Red Hat 4 03-23-2010 06:11 PM
how to remove 72 column limit in gfortran mkrems Programming 1 06-13-2008 07:47 AM


All times are GMT -5. The time now is 10:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration