awk print pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print pipe
# 1  
Old 09-27-2012
Question awk print pipe

Hey fellas,
I wrote an script which its output is like this:

Code:
a 1 T
a 1 T 
a 2 A 
b 5 G
b 5 G
b 5 G

I wanna print $1 $2 and the total number of $2 value as the third column and after that $3. Sth like this:
Code:
a 1 2 T
a 2 1 A
b 5 3 G


I know how to do it with a given input file but cannot do it using pipe.

It would be more cool if you can help to have the output like this: (to have zero value for those which are missed)

Code:
a 1 2 T
a 2 1 A 
- 3 0 - 
- 4 0 - 
b 5 3 G

But this last step is not that important. just for fun though! Smilie
Thanks for your help.
# 2  
Old 09-27-2012
I don't understand. How are you doing this with a regular file? What is it about the processing you're doing that makes it hard to do the same thing on a pipe? (Is there some reason why you need to perform a seek operation on the input? That is something you can't do on a pipe.)

Will you ever have input like:
Code:
a 1 F
a 1 T
b 1 T

with the same values for $2 but different values for $1 and/or $3? If so, when are they supposed to be combined and what values are supposed to be printed for $1 and $4?
# 3  
Old 09-27-2012
Try a for loop for the intermediate values, for example:
Code:
awk '
  p q !=$1 $2{
    if(p)print p,q,t,v
    t=1
    for(i=q+1; i<$2; i++)
      printf "- %s 0 -\n", i
    p=$1
    q=$2
    v=$3
    next
  }
  {
    t++
  } 
  END{
    print p,q,t,v
  }' infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-28-2012
Tnx Don Cragun for your consideration.
I am working with huge bam files which I need to parse and do some analysis on them. Since they are so huge and I'm not expert in programming I analyse them step by stem and using pipe function to go to next step.
The data that I have so far has 4 column which the first column is just two different values for all lines. what I wanna do is to report the second column and the number of its repeats in a separate column. and then the corresponding value on 4th column which is the same for similar values in column 2.

I managed to do it by using two scripts. I'm just wondering if its possible to do in one single script. So Then I can use the output I have so far as an input using pipe.
Thanks.

---------- Post updated at 05:29 PM ---------- Previous update was at 05:26 PM ----------

Thanks Scrutinizer
I haven't applied what you wrote yet. But it needs an input file right? Is it possible to modify it to be able to use this script for the output of another script as I mentioned?
The only thing I know to do such things like that is pipe function. If there is any other way let me know plz.
Thanks
# 5  
Old 09-28-2012
Leave off 'infile' and awk will read from standard input instead.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Print Date when Broken Pipe happens

Hi all, as i have multiple broken pipes on ssh sessions, i need to find out after how much time it happens, ssh root@testServer root@testServer's password: ssh:notty Last login: Thu Apr 6 06:41:16 2017 from 10.10.10.2 # but when broke pipe happen i don't have any idea after how much... (3 Replies)
Discussion started by: charli1
3 Replies

2. Shell Programming and Scripting

awk pipe to sort

In the below awk to add a sort by smallest to largest should it be added after the END? Thank you :). BEGIN { FS="*" } # Read search terms from file1 into 's' FNR==NR { s next } { # Check if $5 matches one of the search terms for(i in s) { if($5 ~ i) { ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Shell Programming and Scripting

BOTH pipe and file(s) into awk

Hello all, quick question: is it possible to pass input into AWK BOTH with a pipe AND a file at the same time, something like this: command .......|awk '.................' FILEIN > fileout All I read says either one or the other, not both, is it at all possible? And how would the... (2 Replies)
Discussion started by: gio001
2 Replies

5. Shell Programming and Scripting

help with sed or awk with less pipe

<tr><th align=right valign=top>Faulty_Part</th><td align=left valign=top>readhat version 6.0</td></tr> <tr><th align=right valign=top>Submit_Date</th><td align=left valign=top>2011-04-28 02:08:02</td></tr> .......(a long string) I want to get all the field between "left valign=top>" and "... (2 Replies)
Discussion started by: yanglei_fage
2 Replies

6. Programming

print all nonempty pipe

I am trying to write a shell script for Linux to print the names of all non empty pipes in the current directory? any help (3 Replies)
Discussion started by: nimesh
3 Replies

7. Shell Programming and Scripting

Pipe to awk to variable

Hi! If I'm trying something like: echo "hello world" | myvar=`awk -F "world" '{print $1}'` echo $myvar myvar is always empty :confused: I googled for houres now and don't understand why it isn't working... Trying it in normal bash. Can someone explain it to me so I can say "Of course!... (8 Replies)
Discussion started by: al0x
8 Replies

8. UNIX for Dummies Questions & Answers

grep pipe filename print issue

uname -a SunOS mypc 5.10 Generic_141414-07 sun4v sparc SUNW,SPARC-Enterprise-T2000 uname -a SunOS mypc 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220 find . -name "*.cer" -exec keytool -v -list -printcert -file {} \; | grep -i "Aug 03" Valid from: Mon Jan 29 00:00:00 GMT... (16 Replies)
Discussion started by: shifahim
16 Replies

9. Shell Programming and Scripting

Is it better to grep and pipe to awk, or to seach with awk itself

This may just be a lack of experience talking, but I always assumed that when possible it was better to use a commands built in abilities rather than to pipe to a bunch of commands. I wrote a (very simple) script a while back that was meant to pull out a certain error code, and report back what... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question