If it is a trivial count, then commands like wc can give you this, however I think the easiest way using awk (if you are really doing some processing of the data in awk) is to have a count incremented each time you read a record, then in the END section, display the count.
Would this work for you?:-
Obviously adjust it to your preferences. The above will print a line number for every record read AND the final record count, so this might not be what you want. I would only go with awk if you are doing some other processing. wc is far simpler.
I have a variable, var="some1|some2|some3"
I want to know how many "|" are in $var.
When I say echo $var | grep -c '|'
I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
In particular I'm trying to check if a line contains an IP address and then do something with it.
The file contains some lines that have no numbers, some blank lines, and some lines with only IP addresses.
#!/bin/bash
touch file2.txt
cat file1.txt | \
while read line
do
if *.*.*.* ];... (4 Replies)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)
Hi,
Can you tell me how to escape a variable number of slash characters in sed "/" ?
In the script the code looks like this:
cat $file_to_update | sed s/^$param/$param=$tab2*\#\*/1
And the $tab2 value is a path so it will have a number of "/" charracters.
# cat db.cfg | sed... (4 Replies)
hi,
i have a file having datas like that
./a.txt
12344
12345
12346
12347
.....
.....
...
i want to save this datas to another file like that
./b.txt
12344 12345 12346 12347 ... ... ...
i think awk can make this but how? :) waiting for ur help. (3 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
logs:
"/home/abc/public_html/index.php"
"/home/abc/public_html/index.php"
"/home/xyz/public_html/index.php"
"/home/xyz/public_html/index.php"
"/home/xyz/public_html/index.php"
how to use "cut" or "awk" or "sed" to get the following result:
abc
abc
xyz
xyz
xyz (8 Replies)
Hi,
I have a snippet like below.
Based on variable i, i wish to print 1,2,3,4,5th columns to Sample files.
For each loop, one column from contetn and results will be pused to sample files. But i have a problem here
i=1
while ; do
`awk -F"\t" '{print $($i)}' $content > Sample_${i}_original`;... (4 Replies)
Hello, there!
I am trying to pass an awk variable into a shell command in order to collect the result into an awk variable; in Bash it does work, as in:
v='2'; date -d "now + $v weeks"
But in awk it does not, as in:
v="2"
"date -d 'now + v weeks'" | getline newdate
close ("date -d 'now... (3 Replies)
Discussion started by: fbird3
3 Replies
LEARN ABOUT DEBIAN
io::capture::stdout
IO::Capture::Stdout(3pm) User Contributed Perl Documentation IO::Capture::Stdout(3pm)NAME
IO::Capture::Stdout - Capture any output sent to STDOUT
SYNOPSIS
# Generic example (Just to give the overall view)
use IO::Capture::Stdout;
$capture = IO::Capture::Stdout->new();
$capture->start(); # STDOUT Output captured
print STDOUT "Test Line One
";
print STDOUT "Test Line Two
";
print STDOUT "Test Line Three
";
$capture->stop(); # STDOUT output sent to wherever it was before 'start'
# In 'scalar context' returns next line
$line = $capture->read;
print "$line"; # prints "Test Line One"
$line = $capture->read;
print "$line"; # prints "Test Line Two"
# move line pointer to line 1
$capture->line_pointer(1);
$line = $capture->read;
print "$line"; # prints "Test Line One"
# Find out current line number
$current_line_position = $capture->line_pointer;
# In 'List Context' return an array(list)
@all_lines = $capture->read;
# More useful example 1 - "Using in module tests"
# Note: If you don't want to make users install
# the IO::Capture module just for your tests,
# you can just install in the t/lib directory
# of your module and use the lib pragma in
# your tests.
use lib "t/lib";
use IO::Capture::Stdout;
use Test::More;
my $capture = IO::Capture::Stdout->new;
$capture->start
# execute with a bad parameter to make sure get
# an error.
ok( ! $test("Bad Parameter") );
$capture->stop();
DESCRIPTION
The module "IO::Capture::Stdout", is derived from the abstract class "IO::Capture". See IO::Capture. The purpose of the module (as the
name suggests) is to capture any output sent to "STDOUT". After the capture is stopped, the STDOUT filehandle will be reset to the previ-
ous location. E.g., If previously redirected to a file, when "IO::Capture->stop" is called, output will start going into that file again.
Note: This module won't work with the perl function, system(), or any other operation
involving a fork(). If you want to capture the output from a system command,
it is faster to use open() or back-ticks.
my $output = `/usr/sbin/ls -l 2>&1`;
METHODS
new
o Creates a new capture object.
o An object can be reused as needed, so will only need to do one of these.
o Be aware, any data previously captured will be discarded if a new capture session is started.
start
o Start capturing data into the "IO::Capture" Object.
o Can not be called on an object that is already capturing.
o Can not be called while STDOUT tied to an object.
o "undef" will be returned on an error.
stop
o Stop capturing data and point STDOUT back to it's previous output location I.e., untie STDOUT
read
o In Scalar Context
o Lines are read from the buffer at the position of the "line_pointer", and the pointer is incremented by one.
$next_line = $capture->read;
o In List Context
o The array is returned. The "line_pointer" is not affected.
@buffer = $capture->read;
o Data lines are returned exactly as they were captured. You may want to use "chomp" on them if you don't want the end of line charac-
ter(s)
while (my $line = $capture->read) {
chomp $line;
$cat_line = join '', $cat_line, $line;
}
line_pointer
o Reads or sets the "line_pointer".
my $current_line = $capture->line_pointer;
$capture->line_pointer(1);
SUB-CLASSING
Adding Features
If you would like to sub-class this module to add a feature (method) or two, here is a couple of easy steps. Also see IO::Capture::Over-
view.
1 Give your package a name
package MyPackage;
2 Use this "IO::Capture::Stdout" as your base class like this:
package MyPackage;
use base qw/IO::Capture::Stdout/;
3 Add your new method like this
package MyPackage;
use base qw/IO::Capture::Stdout/;
sub grep {
my $self = shift;
for $line (
}
See Also
IO::Capture::Overview
IO::Capture
IO::Capture::Stderr
AUTHORS
Mark Reynolds reynolds@sgi.com
Jon Morgan jmorgan@sgi.com
COPYRIGHT
Copyright (c) 2003, Mark Reynolds. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under
the same terms as Perl itself.
perl v5.8.8 2007-07-30 IO::Capture::Stdout(3pm)