Visit Our UNIX and Linux User Community

Perl regexp to extract first and second column

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regexp to extract first and second column
# 1  
Old 07-29-2013
Perl regexp to extract first and second column


I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file:
perl -p -e "s/\s*(\w+).*/$1/"
perl -p -e "s/\s*.+\s(.+)\s*/$1\n/"

whereas the text file's data looks like:
Error: terminated 2233
Warning: reboot 3434
Warning: filledup 4322

I am not getting the expected output, but blank. Kindly correct me in the code where I am doing wrong.
# 2  
Old 07-29-2013
bash-3.00$ perl -p -e 's/(.*):\s(.*)\s\w+/$1 $2/' a
Error terminated
Warning reboot
Warning filledup

# 3  
Old 07-29-2013
perl -lane 'print $F[0],$F[1]' inputfile

# 4  
Old 07-29-2013
Example using awk
awk -F":| " '{print $1,$3}' file

Previous Thread | Next Thread
Test Your Knowledge in Computers #462
Difficulty: Medium
NTPv4 introduced a 256-bit date format: 128 bits for the second and 128 bits for the fractional-second.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - Extract first column from file

Hi, I want to extract first column from a file and redirect the output to another file in perl. I am able to do this from command line by executing below commands. perl -anle 'print $F' Input.dat > Output.dat perl -ne '@F = split("\t", $_); print "$F\n";' Input.dat > Output.dat perl -anE... (7 Replies)
Discussion started by: Neethu
7 Replies

2. Shell Programming and Scripting

Perl regexp help

Hi, I have file like below: 1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|~1213~~~~~ 1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| |4|abc|~rwarw~~asa~~~123~312313 1|1212|34353|5fdf 6575||dfgdg sfsdf |afasfs||4|aasfdbc|~564564~~~~ 1|1212|34353|5fdf 6575||dfgdg sfsdf... (1 Reply)
Discussion started by: sol_nov
1 Replies

3. Shell Programming and Scripting

Extract first column from second line in perl

Hello Gurus I have a source file which has the first line as header and the rest are the records I need to extract the first column from the second line to extract a value I/P ... (7 Replies)
Discussion started by: Pratik4891
7 Replies

4. UNIX for Dummies Questions & Answers

How to extract one column from csv file in perl?

Hi everyone, i am new to perl programming, i have a problem in extracting single column from csv file. the column is the 20th column, please help me.. at present i use this code #!C:/perl/bin use warnings; use strict; my $file1 = $ARGV; open FILE1, "<$file1" or die "Can't... (13 Replies)
Discussion started by: kvth
13 Replies

5. Shell Programming and Scripting

Perl script to extract second column from a xls

Can Anyone tell me how to extract the second column of a xls sheet And compare the content of each row of the column with a .h file. xls sheet is having only one spreadsheet. (2 Replies)
Discussion started by: suvenduperl
2 Replies

6. Shell Programming and Scripting

extract string until regexp from backside

Hi, I searched in the forums, but I didn't find a good solution. My problem is: I have a string like "TEST.ABC201005.MONTHLY.D101010203". I just want to have the string until the D100430, so that the string should look like: "TEST.ABC201005.MONTHLY.D" The last characters after the D can be... (8 Replies)
Discussion started by: elifchen
8 Replies

7. Shell Programming and Scripting

extract values from column with Perl

Hi everybody I have some problems with PERL programming. I have a file with two columns, both with numeric values. I have to extract the values > 50 from the 2nd columns and sum them among them. The I have to sum the respective values in the first column on the same line and, at the end, I... (6 Replies)
Discussion started by: m_elena
6 Replies

8. Shell Programming and Scripting

perl regexp

What is the easiest way to get full address of *.jpg images from html file using perl? example: (1 Reply)
Discussion started by: mirusnet
1 Replies

9. Shell Programming and Scripting

Extract words before and after a pattern/regexp

Couldn't find much help on the kind of question I've here: There is this text file with text as: Line one has a bingo Line two does not have a bingo but it has a tango Bingo is on line three Line four has both tango and bingo Now I would want to search for the pattern "bingo" in this file... (3 Replies)
Discussion started by: manthasirisha
3 Replies

10. Shell Programming and Scripting

Need Help with Perl REGEXP

I need help with a Perl regular expression. The following string blows up my program: <david(greg jim)> If I type this string, there is no problem: <david(greg_jim)> or type david(gregjim) or type <david greg jim> the CGI program does not complain. For some reason that I do not understand the... (1 Reply)
Discussion started by: mh53j_fe
1 Replies

Featured Tech Videos