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
# 8  
Old 03-02-2012
Code:
awk -F"\|"  '{sub(".*"$1 FS,x);NF--}1' OFS="|" yourfile

this will also work if you want to cut from $2 instead of $1 :

Code:
awk -F"\|"  '{sub(".*"$2 FS,x);NF--}1' OFS="|" yourfile

---------- Post updated at 02:51 PM ---------- Previous update was at 02:47 PM ----------

Code:
$ cat f1
106089|machomaddy|ZHealth|Y
106089|machomaddy|ZNetworks|N
106089|machomaddy|ZNetworks|N
$ awk -F"\|"  '{sub(".*"$1 FS,x);NF--}1' OFS="|" f1
machomaddy|ZHealth
machomaddy|ZNetworks
machomaddy|ZNetworks
$ awk -F"\|"  '{sub(".*"$2 FS,x);NF--}1' OFS="|" f1
ZHealth
ZNetworks
ZNetworks

# 9  
Old 03-02-2012
Another awk solution:
Code:
~/unix.com$ awk '{OFS=""; S=""; i=start-1; while(i++<end){S=S OFS $i;OFS=FS}}$0=S' start=3 end=99 FS='|' file

Set start and end at the end of the line to match your requirements
This User Gave Thanks to tukuyomi For This Post:
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