print the number of fields in each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print the number of fields in each line
# 1  
Old 05-24-2011
print the number of fields in each line

I am looking for equivalent of following awk command in perl

[root@swa-rndpoc tmp]# awk '{ print NF ":" $0 } ' junk1
8:VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP DESCR
8:2 NIC6 cont 34 y n shal_orgrp /shal
8:4 NIC5 signa 52 n y shal_orgrp /new_shal
8:3 TIC signa 8 n y shal_orgrp /new_shal

---------- Post updated at 09:21 PM ---------- Previous update was at 07:21 PM ----------

This is the input file
[code]
#VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP DESCR

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


I want output to print number of words in each line except first line.

O/P:
8
8
8
# 2  
Old 05-25-2011
Here is one way:
Code:
#!/usr/bin/perl

use strict;
use warnings;

die "No file argument" unless @ARGV > 0;

my $infile = shift @ARGV;

open (FILE, $infile) or die $!;

while (my $line = <FILE>) {
    next if $. == 1;
    my $fieldcount = $line =~ tr/ //;
    print $fieldcount+1, "\n" if $fieldcount > 0;
}

close FILE;

# 3  
Old 05-25-2011
Thanks fpmurphy !!

This is counter question to earlier

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

---------- Post updated at 01:10 PM ---------- Previous update was at 12:15 AM ----------

hi fpmurphy

Your script fails on following file.

TRK_GRP LD_TYPE CAT LME_TK STCLDF TSX RT_GRP1 Test_GRP2 Test_GRP3 Test_GRP4
2 LD6 STPD 34 y n test_delgrp test junk0 junk1
6 LD5 CCT 52 n y test_delgrp new_test junk4 junk5
4 LD6 CCT 8 n y test_delgrp new_test junk6 junk7
# 4  
Old 05-25-2011
Hi,

I am a new student in the field of UNIX and Linux. I need some basic knowledge to start with.

Could anybody help me?

Rgds,
Student/2011
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

5. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

6. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies

7. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

8. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

9. Shell Programming and Scripting

print last 2 fields of a line

Hi everyone, happy new year! Can someone teach me how I can print the last 2 fields of every line in a file (lines don't have uniform number of fields), for example: input file: aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll mmm nnn ooo output: ccc ddd fff ggg iii jjj nnn ooo ... (2 Replies)
Discussion started by: Deanne
2 Replies

10. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies
Login or Register to Ask a Question