How to Print from nth field to mth fields using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Print from nth field to mth fields using awk
# 1  
Old 03-02-2012
How to Print from nth field to mth fields using awk

Hi,

Is there any short method to print from a particular field till another filed using awk?

Example File:
Code:
File1
====
1|2|acv|vbc|......|100|342
2|3|afg|nhj|.......|100|346

Expected output:
Code:
File2
====
acv|vbc|.....|100
afg|nhj|.....|100

In the above example, let say I have 101 fields, I have print line only from 3 field to 100 field.

Is
Code:
awk '{print $3FS$4FS$5FS....$100}'

is the only method or is it possible to just mention the starting field and the ending field and get the line printed?

Last edited by Franklin52; 03-02-2012 at 10:44 AM.. Reason: fixed code tag
# 2  
Old 03-02-2012
Try like...
Code:
cut -d'|' -f 2-9 test.txt

# 3  
Old 03-02-2012
Try:
Code:
awk -F\| '{sub($1"\\|"$2"\\|",x)}1' infile

# 4  
Old 03-02-2012
bmk,
Thanks. But I was luking for some awk solution Smilie

Scruntizer,
Could yoy please give a explanation of the code? I tried it but it wasn't wokring in the way I was hoping for.

I test like:
Code:
106089|machomaddy|ZHealth|Y
106089|machomaddy|ZNetworks|N
106089|machomaddy|ZNetworks|N

Expected output:
Code:
machomaddy|ZHealth|Y
machomaddy|ZNetworks|N
machomaddy|ZNetworks|N

But the code was producing
Code:
ZHealth|Y
ZNetworks|N
ZNetworks|N

# 5  
Old 03-02-2012
Try like..
Code:
awk -F'|' -v f=2 -v t=4 '{ for (i=f; i<=t;i++) printf("%s%s", $i,(i==t) ? "\n" : OFS) }' test.txt

This User Gave Thanks to bmk For This Post:
# 6  
Old 03-02-2012
Well my suggestion was for cutting the first two fields, as per your example. Only the first column:
Code:
awk -F\| '{sub($1"\\|",x)}1' infile

# 7  
Old 03-02-2012
Quote:
Originally Posted by machomaddy
bmk,
Thanks. But I was luking for some awk solution Smilie

Scruntizer,
Could yoy please give a explanation of the code? I tried it but it wasn't wokring in the way I was hoping for.

I test like:
Code:
106089|machomaddy|ZHealth|Y
106089|machomaddy|ZNetworks|N
106089|machomaddy|ZNetworks|N

Expected output:
Code:
machomaddy|ZHealth|Y
machomaddy|ZNetworks|N
machomaddy|ZNetworks|N

But the code was producing
Code:
ZHealth|Y
ZNetworks|N
ZNetworks|N

an additional try @Scrutinizer's solution
Code:
# awk '{sub("^[^|]*\\|","")}1' infile
machomaddy|ZHealth|Y
machomaddy|ZNetworks|N
machomaddy|ZNetworks|N

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

2. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

How to remove mth and nth column from a file?

Hi, i need to remove mth and nth column from a csv file. here m and n is not a specific number. it is a variable ex. m=2 n=5 now i need to remove the 2nd and 5th line.. Please help how to do that. Thanks!!! (18 Replies)
Discussion started by: zaq1xsw2
18 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

Get the nth word of mth line in a file

Hi.. May be a simple question but I just began to write unix scripts a week ago, for sorting some huge amount of experiment data, so I got no common sense about unix scripting and really need your helps... The situation is, I want to read the nth word of mth line in a file, and then store it... (3 Replies)
Discussion started by: freezelty
3 Replies

7. UNIX for Dummies Questions & Answers

Print Nth to last field

Hey, I'm sure this is answered somewhere but my Googling has turned up nothing. I have a file with data in the following format: <desription of event> at <time and date>The desription of the event is variable length and hence when the list is displayed it is hard to easily see the date (and... (8 Replies)
Discussion started by: RECrerar
8 Replies

8. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

9. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

10. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies
Login or Register to Ask a Question