number of fields in a text file as a variable - perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting number of fields in a text file as a variable - perl
# 1  
Old 05-25-2011
number of fields in a text file as a variable - perl

I am looking for perl code to get following o/p. If a line has more than 7 fields then value in field 7 onwards is BHA_GRP1, BHA_GRP2, BHA_GRP3, BHA_GRP4 etc.
Here is example of what I am trying to achieve.

INPUT File:
VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP1 BHA_GRP2 BHA_GRP3......

2 NIC6 cont 34 y n shal_orgrp shal
4 NIC5 signa 52 n y shal_orgrp1 new_shal test
3 TIC signa 8 n y shal_orgrp2

OUTPUT:
For line 2 -- $BHA_GRP1 = shal_orgrp, $BHA_GRP2= shal
line3 -- $BHA_GRP1= shal_orgrp1, $BHA_GRP2= new_shal, $BHA_GRP3=test
line4-- $BHA_GRP1= shal_orgrp2
Image
# 2  
Old 05-25-2011
Hi,

Test next 'Perl' script:
Code:
$ cat infile
VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP1 BHA_GRP2 BHA_GRP3......

2 NIC6 cont 34 y n shal_orgrp shal
4 NIC5 signa 52 n y shal_orgrp1 new_shal test
3 TIC signa 8 n y shal_orgrp2
$ cat script.pl
use strict;
use warnings;
use autodie;

@ARGV == 1 or die "Usage: perl $0 input-file\n";
open my $fh, "<", $ARGV[0];
my (@grp, @sha);

while ( <$fh> ) {
    chomp;
    if ( $. == 1 ) {
        @grp = split;
        @grp = splice @grp, 6;
        next;
    }
    next if /^\s*$/;
    @sha = split;
    @sha = splice @sha, 6;
    print +(join ", ", map { "\$" . ($grp[$_] || "???") . " = " . $sha[$_] } 0 .. $#sha ), "\n";
}

close $fh or warn "Error: Cannot close $ARGV[0]\n";
$ perl script.pl infile
$BHA_GRP1 = shal_orgrp, $BHA_GRP2 = shal
$BHA_GRP1 = shal_orgrp1, $BHA_GRP2 = new_shal, $BHA_GRP3...... = test
$BHA_GRP1 = shal_orgrp2

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add number of fields throughout file together?

So, I have three problems that cover this subject. First one asks me to find the number of fields in the file that contain the substring "he". I found the number of fields, but the problem I have is that they are displaying by each record. I want to add all of the records' fields together. With... (2 Replies)
Discussion started by: mc10
2 Replies

2. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

awk processing of variable number of fields data file

Hy! I need to post-process some data files which have variable (and periodic) number of fields. For example, I need to square (data -> data*data) the folowing data file: -5.34281E-28 -3.69822E-29 8.19128E-29 9.55444E-29 8.16494E-29 6.23125E-29 4.42106E-29 2.94592E-29 1.84841E-29 ... (5 Replies)
Discussion started by: radudownload
5 Replies

4. Shell Programming and Scripting

Perl: How to read text from file and process $variable in that data too.

In the hello.htm have the sentenses: Hello $name How are you? The perl script: $name = "David"; open(HEADER,"hello.htm"); while(<HEADER>) { $html .= $_; } close(HEADER); print "$html";I making something about template. But it can't process the $name variable. (4 Replies)
Discussion started by: natong
4 Replies

5. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

6. Shell Programming and Scripting

separate the file according to the number of fields

I have a file which is delimetered by ',' i need to filter out a file with respect to the number of fileds in each line. a,s,d,f,g,h,j,k,l 1,2,3,3,4,5,6,7,6 a,2,3 4,5,6,7 in this i neeed to filter out the lines with 8 column to another file and rest to another file. so ... (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

7. Shell Programming and Scripting

Perl:Read single value from text file and assign to variable

Hello All, A part of my very basic perl code requires me to read a single value from a text file. The file output is the following: Reading image ... done IMAGEREGION=0x0x0-256x162x256 VOXELDIMENSION=0.9375000000x1.2000000477x0.9375000000 VOXELNUMBER=10527001... (7 Replies)
Discussion started by: ncl
7 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. Shell Programming and Scripting

Check if a variable is a number - perl

Logic of code if ( $var is a number ) { Do something } else { Do something else } My question is: How do I check if a variable is a number. All the reg ex that I came up with to match this is failing. Please help. (3 Replies)
Discussion started by: garric
3 Replies

10. Shell Programming and Scripting

How to make a number in a text file a variable?

OK this one sounds like it should be a natural... it must be possible to send a number say 200 to a text file and later use it as a variable? yada yada > file.txt how would I retrieve that number to use as a variable? Is this possible? (5 Replies)
Discussion started by: nortypig
5 Replies
Login or Register to Ask a Question