how to differentiate columns of a file in perl with no specific delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to differentiate columns of a file in perl with no specific delimiter
# 1  
Old 07-12-2008
how to differentiate columns of a file in perl with no specific delimiter

Hi everybody,
This time I am having one issue in perl.
I have to create comma separated file using the following type of information. The problem is the columns do not have any specific delimiter. So while using split I am getting different value. Some where it is space(S) and some where it is tab(s). in some cases I do not have values under some specific column and these are also filled with tab(s) and /or space(s).
Revsn: Date Who SID Description of Modification
09-Jul-01 Amiya 01-04Jul01 add setdescriptor shortcuts
02-Aug-99 RathA chk include 1st line
remove per VandJ
06-Jan-98 DasP data obsolete...
02-Jul-97 Gupts add DDMM for completeness

I want my output as
09-Jul-01, Amiya,01-04Jul01, add setdescriptor shortcuts
02-Aug-99,RathA,----, chk include 1st line remove VandJ (If line has only description, add to prev line)
06-Jan-98, DasP,----,data obsolete...
02-Jul-97, Gupts,----, add DDMM for completeness
Thanks in advance....
# 2  
Old 07-12-2008
Try this:
Code:
awk '
NR>1 && /^[0-9/{printf("\n%s",$0);next}
{printf(" %s",$0)}
END{print ""}
' file

Regards
# 3  
Old 07-12-2008
I am getting syntax erro while executing this command as below
awk: newline in character class near line 1
awk: syntax error near line 2
awk: bailing out near line 2
# 4  
Old 07-12-2008
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 5  
Old 07-12-2008
Try this:

Code:
perl -ne '
	if (/^[0-9]{2}-[[:alpha:]]{3}-[0-9]{2}/) {
		if (defined($prev)) { printf($prev . "\n"); }
		($f1, $f2, $f3, undef, $f5) = m/([0-9]{2}-[[:alpha:]]{3}-[0-9]{2})[[:blank:]]+([[:alnum:]]+)[[:blank:]]+(([0-9]{2}-)?[0-9]{2}-?[[:alpha:]]{3}-?[0-9]{2}[[:blank:]])?(.*)/;
		if ($f3 == "") { $f3="----" }
		$prev = join(",",$f1,$f2,$f3,$f5);
	} else {
		$prev .= " " . chomp($_);
	}
	END { printf($prev . "\n"); }
' inputfile > outputfile

# 6  
Old 07-12-2008
in order to help with a reliable solution, you need to define what this field can contain:

01-04Jul01

is it always that same format or can it be different besides just not existing? Does it always correspond with the date at the beginning of the line?
# 7  
Old 07-12-2008
Code:
awk '{p=c; c=$0}; !/^[0-9]+/{print p,c;next}1' file

perl:
Code:
while (<>) {
    chomp; 
    $p = $c; $c = $_;
    if (!/^[0-9]+/) { print "$p $c\n" ; next ; }
    print $_ . "\n";
}


Last edited by ghostdog74; 07-13-2008 at 12:12 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delimiter appending in a data file if we receive a less columns than expected

Required No.of field = 12 Let say you got a “~” delimited input file and this file has 6 input fields and now I want to add 12-5=7 number of “~” into this input file in order to make it 12 fields datafile can have n number of records ex., a~b~c~d~12~r a~b~c~d~12~r a~b~c~d~12~r... (19 Replies)
Discussion started by: LJJ
19 Replies

2. Shell Programming and Scripting

Perl script to accept specific columns from excel

Hi All, I have below perl script which writes xml from .xls file. Now i want to add below two conditions in this script : 1. to check if the the input .xls file has ony two columns , if more tahn two columns then script should pop up an error. 2. If there are two columns , then first column... (4 Replies)
Discussion started by: omkar.jadhav
4 Replies

3. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

4. Shell Programming and Scripting

Transpose whole file and specific columns

Hi, I have a file like this a b c d e f g h i j k l Case1: I want to transpose the whole file Output1 a d g j b e h k c f i l Case2 Transpose a specific column - Say 3rd (6 Replies)
Discussion started by: jacobs.smith
6 Replies

5. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

6. Shell Programming and Scripting

Selecting Specific Columns and Insert the delimiter TAB

Hi, I am writing a Perl Script for the below : I have a data file that consists of the header information which is 231 Lines and the footer information as 4 lines. The total number of line including the header and footer 1.2 Million with Pipe Delimited file. For example: Header Information:... (4 Replies)
Discussion started by: filter
4 Replies

7. Shell Programming and Scripting

Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this. My question: I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it... (3 Replies)
Discussion started by: mehdib
3 Replies

8. UNIX for Dummies Questions & Answers

Displaying specific columns in a file

Hi, I'm just wondering how you display a specific set of columns of a specified file in Unix. For example, if you had an AddressBook file that stores the Names, Phone numbers, and Addresses of people the user entered in the following format (the numbers are just to give an idea of what column... (1 Reply)
Discussion started by: logorob
1 Replies

9. Shell Programming and Scripting

replace space with delimiter in whole file -perl

Hi I have a file which have say about 100,000 records.. the records in it look like Some kind of text 1234567891 abcd February 14, 2008 03:58:54 AM lmnop This is how it looks.. if u notice there is a 2byte space between each column.. and im planning to replace that with '|' .. ... (11 Replies)
Discussion started by: meghana
11 Replies

10. Shell Programming and Scripting

Deleting specific columns from a file

Hi Friends, I want to delete specific columns from a file. Say my file content is as follows: "1","a","ww1",1234" "2","b","wwr3","2222" "3","c","erre","3333" Now i want to delete the column 2 and 4 from this file. That is I want the file content to be: "1","ww1" "2","wwr3"... (11 Replies)
Discussion started by: premar
11 Replies
Login or Register to Ask a Question