Sponsored Content
Top Forums Shell Programming and Scripting Format output with blocks - awk Post 302968165 by greycells on Friday 4th of March 2016 04:21:48 PM
Old 03-04-2016
Format output with blocks - awk

Format output with blocks - Awk

Code:
 
ZONESET_A,DBP02_HBA0,DC01,20:00:00:25:b5:b9:a0:28,10,port-channel20,01-VB2
ZONESET_A,DBP02_HBA0,DC01,50:00:14:42:a0:72:74:00,10,fc4/30,O1-CORE-D
 
ZONESET_A,dc01vb,DC01,20:00:00:25:b5:b0:a0:13,10,fc1/24,01-VB1
ZONESET_A,dc01vb,DC01,50:00:14:42:a0:72:74:00,10,fc4/30,O1-CORE-D
 
ZONESET_B,DC01,DC01,20:00:00:25:b5:b6:b0:6c,11,port-channel11,NMDNSWACC64
ZONESET_B,DC01,DC01,50:06:01:67:46:e0:56:2e,11,,
ZONESET_B,DC01,DC01,20:00:00:25:b5:b6:b0:9c,11,port-channel11,NMDNSWACC64
 
ZONESET_A,dc01b,DC01,20:00:00:25:b5:b0:a0:13,10,,
ZONESET_A,dc01b,DC01,50:00:14:42:a0:72:74:00,10,,
 
ZONESET_A,dc01s,DC01,20:00:00:25:b5:b0:a0:13,10,,
ZONESET_A,dc01s,DC01,50:00:14:42:a0:72:74:00,10,,
ZONESET_A,dc01s,DC01,20:00:00:25:b5:b0:a0:13,10,,
ZONESET_A,dc01s,DC01,50:00:14:42:a0:72:74:00,10,,

Output Needed
Code:
 
ZONESET_A,DBP02_HBA0,DC01,20:00:00:25:b5:b9:a0:28,10,port-channel20,01-VB2
,,DC01,50:00:14:42:a0:72:74:00,10,fc4/30,O1-CORE-D
 
ZONESET_A,dc01vb,DC01,20:00:00:25:b5:b0:a0:13,10,fc1/24,01-VB1
,,DC01,50:00:14:42:a0:72:74:00,10,fc4/30,O1-CORE-D

ZONESET_B,DC01,DC01,20:00:00:25:b5:b6:b0:6c,11,port-channel11,NMDNSWACC64
,,DC01,50:06:01:67:46:e0:56:2e,11,,
,,DC01,20:00:00:25:b5:b6:b0:9c,11,port-channel11,NMDNSWACC64
 
ZONESET_A,dc01b,DC01,20:00:00:25:b5:b0:a0:13,10,,
,,DC01,50:00:14:42:a0:72:74:00,10,,
 
ZONESET_A,dc01s,DC01,20:00:00:25:b5:b0:a0:13,10,,
,,DC01,50:00:14:42:a0:72:74:00,10,,
,,DC01,20:00:00:25:b5:b0:a0:13,10,,
,,DC01,50:00:14:42:a0:72:74:00,10,,

where $1,$2 are same within a block replace all the lines except the first one in a block with "" .

Thanks !
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output in a particular format using AWK

Hi All, I am trying to check if if column 5 is greater than 90. If greater it will print the term in column 6, else if all are within limit, then it will output "Size is within limit". I can't seem to do that with the below code. The output should only be 1 statement of "Size is within the... (4 Replies)
Discussion started by: Raynon
4 Replies

2. Shell Programming and Scripting

[need help] output format from awk

hi all, i have a problem with my nawk command output below is the description : nawk $12 == "00008001" { cnt++;cs_cd } END {for(cd in cs_cd) print cd, cs_cd } 2007020814.TDR output : 133 123 desire output: 133,123,.... please advices thank you so much (6 Replies)
Discussion started by: bucci
6 Replies

3. Shell Programming and Scripting

Format Output with AWK command

Hi - I have a file with contents as below. 12.1 a.txt 12.1 b.txt 12.1 c.txt 13.2 a.txt 13.2 d.txt 14.3 f.txt 15.4 a.txt 15.4 b.txt 15.4 z.txt I need to print the contents like this. 12.1 a.txt <&nbsp><&nbsp><&nbsp>b.txt <&nbsp><&nbsp><&nbsp>c.txt (7 Replies)
Discussion started by: guruparan18
7 Replies

4. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

awk - format output

Input file1 zone: BAU_SERVER1 C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: BAU_SERVER2 C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: ... (4 Replies)
Discussion started by: greycells
4 Replies

6. Shell Programming and Scripting

awk to format an output

awk experts, I have in put file with time stamp followed by "," separated data. same patern continues. The output need time stamp in first columns and data total in 2nd columns. Input file T 9:15 d0,1,3,3 d1,2,1,1 d2,3,1,5 e1,1,1,1 T 9:30 d0,1,1,1 d1,2,3,2 d3,1,2,1... (10 Replies)
Discussion started by: arv_cds
10 Replies

7. UNIX for Dummies Questions & Answers

after awk-> format output

hi i have a awk command with several querys.... awk 'FS="|""; print $4, $5, $6...etc.... $4 gives me the date 20120304 $5 is timestamp 101023 I want to format these in 2012.03.04 or 2012/03/04 10:10:23 but have no idea, if this is possible with format-parameters in the awk... (2 Replies)
Discussion started by: Jazzmatazz
2 Replies

8. Shell Programming and Scripting

Format output using awk

Hello all , need help with this ... Input File DEV % POOL 0CB4 FBA 2211300 81792 4 IE RAID-5(3+1) R5_EFD100_1 - - 1805376 82 IF RAID-1 M2_FC300_1 - ... (4 Replies)
Discussion started by: greycells
4 Replies

9. Shell Programming and Scripting

Adjusting my awk output format

I've been diving into awk but still learning how to use it for text formatting. Below you can see my results are separated by a comma. Can somebody show me how to separate by TAB as well? An explanation would be appreciated as I did not comprehend the answer in the man pages and would like to gain... (1 Reply)
Discussion started by: sudo
1 Replies

10. Shell Programming and Scripting

Need to maintain in- and output format with awk

Hi All, I have a data file (myfile.txt) as below: - A H C - A HHH F - AAA HH I The importan point is that the width between the columns are not fixed and the column seperator is space. I wish to change the value of 4th column using awk only when $3 = HH. I can... (4 Replies)
Discussion started by: angshuman
4 Replies
A2P(1)							 Perl Programmers Reference Guide						    A2P(1)

NAME
a2p - Awk to Perl translator SYNOPSIS
a2p [options] [filename] DESCRIPTION
A2p takes an awk script specified on the command line (or from standard input) and produces a comparable perl script on the standard output. OPTIONS Options include: -D<number> sets debugging flags. -F<character> tells a2p that this awk script is always invoked with this -F switch. -n<fieldlist> specifies the names of the input fields if input does not have to be split into an array. If you were translating an awk script that processes the password file, you might say: a2p -7 -nlogin.password.uid.gid.gcos.shell.home Any delimiter can be used to separate the field names. -<number> causes a2p to assume that input will always have that many fields. -o tells a2p to use old awk behavior. The only current differences are: o Old awk always has a line loop, even if there are no line actions, whereas new awk does not. o In old awk, sprintf is extremely greedy about its arguments. For example, given the statement print sprintf(some_args), extra_args; old awk considers extra_args to be arguments to "sprintf"; new awk considers them arguments to "print". "Considerations" A2p cannot do as good a job translating as a human would, but it usually does pretty well. There are some areas where you may want to examine the perl script produced and tweak it some. Here are some of them, in no particular order. There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always integer anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always going to be integer, so it leaves it in. You may wish to remove it. Perl differentiates numeric comparison from string comparison. Awk has one operator for both that decides at run time which comparison to do. A2p does not try to do a complete job of awk emulation at this point. Instead it guesses which one you want. It's almost always right, but it can be spoofed. All such guesses are marked with the comment ""#???"". You should go through and check them. You might want to run at least once with the -w switch to perl, which will warn you if you use == where you should have used eq. Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced. If somehow you are relying on this mechanism to create null entries for a subsequent for...in, they won't be there in perl. If a2p makes a split line that assigns to a list of variables that looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n option mentioned above. This will let you name the fields throughout the script. If it splits to an array instead, the script is probably referring to the number of fields somewhere. The exit statement in awk doesn't necessarily exit; it goes to the END block if there is one. Awk scripts that do contortions within the END block to bypass the block under such circumstances can be simplified by removing the conditional in the END block and just exiting directly from the perl script. Perl has two kinds of array, numerically-indexed and associative. Perl associative arrays are called "hashes". Awk arrays are usually translated to hashes, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. Iteration over a hash is done using the keys() function, but iteration over an array is NOT. You might need to modify any loop that iterates over such an array. Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. You'll want to set $# explicitly if you use the default value of OFMT. Near the top of the line loop will be the split operation that is implicit in the awk script. There are times when you can move this down past some conditionals that test the entire record so that the split is not done as often. For aesthetic reasons you may wish to change index variables from being 1-based (awk style) to 0-based (Perl style). Be sure to change all operations the variable is involved in to match. Cute comments that say "# Here is a workaround because awk is dumb" are passed through unmodified. Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself. Scripts that refer to the special variables RSTART and RLENGTH can often be simplified by referring to the variables $`, $& and $', as long as they are within the scope of the pattern match that sets them. The produced perl script may have subroutines defined to deal with awk's semantics regarding getline and print. Since a2p usually picks correctness over efficiency. it is almost always possible to rewrite such code to be more efficient by discarding the semantic sugar. For efficiency, you may wish to remove the keyword from any return statement that is the last statement executed in a subroutine. A2p catches the most common case, but doesn't analyze embedded blocks for subtler cases. ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n-1]. A loop that tries to iterate over ARGV[0] won't find it. ENVIRONMENT
A2p uses no environment variables. AUTHOR
Larry Wall <larry@wall.org> FILES
SEE ALSO
perl The perl compiler/interpreter s2p sed to perl translator DIAGNOSTICS
BUGS
It would be possible to emulate awk's behavior in selecting string versus numeric operations at run time by inspection of the operands, but it would be gross and inefficient. Besides, a2p almost always guesses right. Storage for the awk syntax tree is currently static, and can run out. perl v5.16.3 2013-03-04 A2P(1)
All times are GMT -4. The time now is 08:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy