awk return number of entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk return number of entries
# 1  
Old 03-06-2013
awk return number of entries

I have a string with the following information and want to return the number of entries enclosed by <> in awk



Code:
<stdin>: N = 441    <0.369000018/0.569000006>    <0.369000018/0.569000006>    <0/1>

# 2  
Old 03-06-2013
Try:
Code:
awk '{print gsub(/<[^>]*>/,x)}'

# 3  
Old 03-06-2013
The variable is in a bash script

Code:
x="<stdin>: N = 441    <0.369000018/0.569000006>    <0.369000018/0.569000006>    <0/1>"

And need to pass it to awk somehow, possibly using -v

Code:
s="<stdin>: N = 441    <0.369000018/0.569000006>    <0.369000018/0.569000006>    <0/1>"
awk -v x="$s" '{print gsub(/<[^>]*>/,x)}'

The command stalls giving me 0 all the time when I press return

Last edited by kristinu; 03-06-2013 at 01:27 PM..
# 4  
Old 03-06-2013
The awk program stalls because it is expecting input. Use echo instead:
Code:
echo "$s" | awk '{print gsub(/<[^>]*>/,x)}'

---------- Post updated at 11:46 ---------- Previous update was at 11:33 ----------

OR use a BEGIN block:
Code:
awk -v x="$s" 'BEGIN{print gsub(/<[^>]*>/,X,x)}'

# 5  
Old 03-06-2013
Thanks a lot. It would be good to explain how the number the calculated with that command as I am not getting it. It works though.

I tried the following, incrementing i which also works

Code:
echo "$s" | awk '{print gsub(/<[^>]*>/,++i)}'


Last edited by kristinu; 03-06-2013 at 01:55 PM..
# 6  
Old 03-06-2013
Regexp /<[^>]*>/ means:

/ -- Begin pattern

<[^>]*> -- < followed by zero or more occurrence * of any character [^>] other than > followed by >

/ -- End pattern

The gsub function returns the number of substitutions made which the code prints.
These 2 Users Gave Thanks to Yoda For This Post:
# 7  
Old 03-06-2013
Got it, so in principle I can replace x with "" as it is just a replacement. Is that correct?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to return only field with a number in it

What is an awk command to print only fields with a number in it?? Input file....... S,S,S,S,S,S,S,S,S 001S,S,S,S,S,S,S,S,S 00219S,23S,24S,43S,47S,S,S,S,S 00319S,10S,23S,41S,43S,47S,S,S,S 00423S,41S,43S,46S,47S,S,S,S,S 00510S,23S,24S,43S,46S,S,S,S,S 00610S,23S,43S,46S,47S,S,S,S,S... (2 Replies)
Discussion started by: garethsays
2 Replies

2. Shell Programming and Scripting

awk - Print column number that return value comes from

I have the following awk script that I am using to find the max value in the file and print results. awk 'BEGIN {MAX=-1E100} {for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x; C1 = $1}} END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), MAX}' ABC* Input (ABC*) ... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

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

4. Shell Programming and Scripting

How do I use sed to return only the serial number here?

I'd appreciate the help and explaining "which each switch/command does. Thanks in advance. 1742@3min# ./fmtopo|grep serial hc://:product-id=SUNW,Sun-Blade-2500:server-id=c3admin:serial=130B58E3146/motherboard=0/cpu=0 (2 Replies)
Discussion started by: LittleLebowski
2 Replies

5. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

6. Shell Programming and Scripting

awk script to return the middle line number

I need an awk script that returns the 1st field of the line in the middle of a file. For example, if a file I have has 6 lines, I want it to return the 1st field on line number 3. If a file has 7 lines, I want the script to return the 1st field on line number 4. File1: 3 214 4 219 5 226 2... (8 Replies)
Discussion started by: jontjioe
8 Replies

7. Shell Programming and Scripting

Want to trap script error and return line number of failure

Hey all UNIX nerds- I've built a shell script which runs pretty well- only I want it to have much better error trapping. (Like the kind I could apply to every shell script I write). I'm not a UNIX genius, and could really use a bit of help. The original script goes something like this: 1... (3 Replies)
Discussion started by: stevekerver
3 Replies

8. Shell Programming and Scripting

Return Number of Substitutions made by SED?

Hi guys, Is there any way this can be done, or return whether any substitutions have been made? thanks for any input. skinnygav (using Bash shell) (2 Replies)
Discussion started by: skinnygav
2 Replies

9. Shell Programming and Scripting

Diff Command to return the number of lines inserted,deleted and changed.

Hello, I have to compare two files file1 and file2, retrieve the number of lines modified (added, deleted or modified) in file2. Output must be like: File2: Added Deleted Changed Total ------ ------- -------- ----- 2 1 1 4 Somebody... (2 Replies)
Discussion started by: nmattam
2 Replies

10. Shell Programming and Scripting

return number of rows selected

Hi all, i am doing a perl script to read from a db. I am able to retrieve the rows, but i am unable to return the number of rows selected. i tried $selectedrows = $sth->numrows; i got the error msg: Can't locate object method "numrows" via package "DBI::st" i changed it to $selectedrows =... (7 Replies)
Discussion started by: new2ss
7 Replies
Login or Register to Ask a Question