Sponsored Content
Top Forums Shell Programming and Scripting Awk: printing column using for loop Post 303011327 by genome on Tuesday 16th of January 2018 05:52:30 PM
Old 01-16-2018
Awk: printing column using for loop

Hello:

I've input data:

Input data
Code:
--- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1
--- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1
--- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1

I edit/make first few columns before numbers (6th column) and want to print data as they are. Numbers start from 6th colimn (zero-one)

Code:
  awk '{out=""; for(i=6;i<=NF;i++){out=$out" "$i}; print 3,3":"$3":"$4":"$5,$3,$4,$5,$out}' pot.txt

wrong output:

Code:
3 3:60069:C:T 60069 C T --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60079:A:G 60079 A G --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1
3 3:60157:G:A 60157 G A --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60189:A:G 60189 A G --- 3:60189:A:G 60189 A G 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60197:G:A 60197 G A --- rs115479960:60197:G:A 60197 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60201:T:C 60201 T C --- 3:60201:T:C 60201 T C 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60202:C:G 60202 C G --- rs28729284:60202:C:G 60202 C G 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60322:G:A 60322 G A --- 3:60322:G:A 60322 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60337:G:A 60337 G A --- rs116791090:60337:G:A 60337 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60342:A:<CN0> 60342 A <CN0> --- 3:60342:A:<CN0>:0 60342 A <CN0> 1 0 0 1 0 0 1 0 0 1 0 0 1

Correct output needed:
Code:
3 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1
3 3:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60189:A:G 60189 A G 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60197:G:A 60197 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60201:T:C 60201 T C 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60202:C:G 60202 C G 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60322:G:A 60322 G A 1 0 0 1 0 0 1 0 0 1
3 3:60337:G:A 60337 G A 1 0 0 1 0 0 1 0 0 1 0 0 1
3 3:60342:A:<CN0> 60342 A <CN0> 1 0 0 1 0 0 1 0 0 1 0 0 1

Even if I start from i=7, I get input column repeated in the output.

Where's my code going wrong?
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Silly question on printing for loop in AWK

Hi, One silly question. I would like to add statement like below and append to a file. I used the below code; however, it does not work. Can anyone please tell me what mistakes I have made? awk ' { for (i=1;i<=563;i++) print i }'>>output.txt Thanks. -Jason (1 Reply)
Discussion started by: ahjiefreak
1 Replies

2. Shell Programming and Scripting

printing with awk through while loop

ive input file contains to clums a and b spreated by pipe a | b 123|456 323|455 and xyz contains other info about a and b now i want to print as follows: a | b | "info from xyz" but "info from xyz" might be more than 1 line and i want to keep the format to 3 cloums. how to do it?... (3 Replies)
Discussion started by: windows
3 Replies

3. Shell Programming and Scripting

Awk not printing the last combined column

nawk -F "|" 'FNR==NR {a=$2 OFS $3 OFS $4 OFS $5 OFS $6;next}\ {if ($5 in a)print $1,"test",$5,a, $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2-$3-$4 ; \ else print $1,"Database",$5 OFS OFS OFS OFS OFS OFS $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2-$3-$4 }' OFS="|" \ file1 file2 > file3 This... (5 Replies)
Discussion started by: pinnacle
5 Replies

4. Shell Programming and Scripting

Printing 1st column to lower case using awk

I want to print the 1st field in a comma seperated file to lower case and the rest the case they are. I tried this nawk -F"," '{print tolower($0)}' OFS="," file this converts whole line in to lower case i just want the first column to be converted. The below doesnt work because in... (11 Replies)
Discussion started by: pinnacle
11 Replies

5. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

6. Shell Programming and Scripting

Awk while-loop printing extra character

Hi, I'm using a while-loop in an awk script. If it matches a regular expression, it prints a line. Unfortunately, each line that is printed in this loop is followed by an extra character, "1". While-statement extracted from my script: getline temp; while (temp ~ /.* x .*/) print temp... (3 Replies)
Discussion started by: redbluefish
3 Replies

7. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

8. Shell Programming and Scripting

Printing another column using awk and input data

Hi, I have data of the following type, chr1 234 678 39 852 638 abcd 7895 chr1 526 326 33 887 965 kilj 5849 Now, I would like to have something like this chr1 234 678 39 852 638 abcd 7895 <a href="http://unix.com/thread=chr1:234-678">Link</a> chr1 526 326 33 887 965 kilj 5849 <a... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

9. Shell Programming and Scripting

awk: printing newline with last column

I was trying to simplify this from what I'm actually doing, but I started getting even more confused so I gave up. Here is the content of my input file: Academic year,Term,Course name,Period,Last name,Nickname 2012-2013,First Semester,English 12,7th Period,Davis,Lucille When I do this: ... (3 Replies)
Discussion started by: nextyoyoma
3 Replies

10. Shell Programming and Scripting

Column printing in awk

Experts, i have a following file containing data in following manner. 1 2480434.4 885618.6 0.00 1948.00 40.00 1952.00 ... (6 Replies)
Discussion started by: Amit.saini333
6 Replies
Devel::Refcount(3pm)					User Contributed Perl Documentation				      Devel::Refcount(3pm)

NAME
"Devel::Refcount" - obtain the REFCNT value of a referent SYNOPSIS
use Devel::Refcount qw( refcount ); my $anon = []; print "Anon ARRAY $anon has " . refcount($anon) . " reference "; my $otherref = $anon; print "Anon ARRAY $anon now has " . refcount($anon) . " references "; DESCRIPTION
This module provides a single function which obtains the reference count of the object being pointed to by the passed reference value. FUNCTIONS
$count = refcount($ref) Returns the reference count of the object being pointed to by $ref. COMPARISON WITH SvREFCNT This function differs from "Devel::Peek::SvREFCNT" in that SvREFCNT() gives the reference count of the SV object itself that it is passed, whereas refcount() gives the count of the object being pointed to. This allows it to give the count of any referent (i.e. ARRAY, HASH, CODE, GLOB and Regexp types) as well. Consider the following example program: use Devel::Peek qw( SvREFCNT ); use Devel::Refcount qw( refcount ); sub printcount { my $name = shift; printf "%30s has SvREFCNT=%d, refcount=%d ", $name, SvREFCNT($_[0]), refcount($_[0]); } my $var = []; printcount 'Initially, $var', $var; my $othervar = $var; printcount 'Before CODE ref, $var', $var; printcount '$othervar', $othervar; my $code = sub { undef $var }; printcount 'After CODE ref, $var', $var; printcount '$othervar', $othervar; This produces the output Initially, $var has SvREFCNT=1, refcount=1 Before CODE ref, $var has SvREFCNT=1, refcount=2 $othervar has SvREFCNT=1, refcount=2 After CODE ref, $var has SvREFCNT=2, refcount=2 $othervar has SvREFCNT=1, refcount=2 Here, we see that SvREFCNT() counts the number of references to the SV object passed in as the scalar value - the $var or $othervar respectively, whereas refcount() counts the number of reference values that point to the referent object - the anonymous ARRAY in this case. Before the CODE reference is constructed, both $var and $othervar have SvREFCNT() of 1, as they exist only in the current lexical pad. The anonymous ARRAY has a refcount() of 2, because both $var and $othervar store a reference to it. After the CODE reference is constructed, the $var variable now has an SvREFCNT() of 2, because it also appears in the lexical pad for the new anonymous CODE block. PURE-PERL FALLBACK An XS implementation of this function is provided, and is used by default. If the XS library cannot be loaded, a fallback implementation in pure perl using the "B" module is used instead. This will behave identically, but is much slower. Rate pp xs pp 225985/s -- -66% xs 669570/s 196% -- SEE ALSO
o Test::Refcount - assert reference counts on objects AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2011-11-15 Devel::Refcount(3pm)
All times are GMT -4. The time now is 12:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy