Fetching 1st Column and Last n Columns


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Fetching 1st Column and Last n Columns
# 1  
Old 10-05-2016
Fetching 1st Column and Last n Columns

I am trying to fetch 1st column and last 10 columns.The code I am using is working fine but after using the code then output file becomes space delimited not tab delimited.

Code:
 
 awk 'BEGIN {OFS="\t"}{printf("%s\t",$1)}{for(i=NF-9; i<=NF; i++) {printf("%s\t",$i)};printf "\n" } ' inputfile

Then I have to use below code to make the file Tab Delimited.

Code:
 
 awk '{ for(i=1;i<=NF;i++){if(i==NF){printf("%s\n",$NF);}else {printf("%s\t",$i)}}}'

Please help


Please let me know what is wrong in the code which is fetching 1st column and last 10 columns/
# 2  
Old 10-05-2016
Hello Nina2910,

Thank you for showing your efforts in forum, it will be more good if you could show us the sample Input_file and sample expected output too, could you please try following and do let me know if this helps you, haven't tested it though.
Code:
awk 'BEGIN {OFS="\t"}{for(i=NF-9; i<=NF; i++){Q=Q?Q OFS $i:$i};print $1,Q;Q=""}' Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-05-2016
I'd be very surprised if the output were space delimited as you explicitly print the <TAB> char...
Post (or better: attach) your input and output files.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-05-2016
@Rudi

Input File

Code:
 
NAME    1       2       3       4       5       6       7       8       9       10      11      12      13      14      15      16
A       10      20      30      40      50      60      70      80      90      100     110     120     130     140     150     160
B       10      20      30      40      50      60      70      80      90      100     110     120     130     140     150     160
C       10      20      30      40      50      60      70      80      90      100     110     120     130     140     150     160
D       10      20      30      40      50      60      70      80      90      100     110     120     130     140     150     160
E       10      20      30      40      50      60      70      80      90      100     110     120     130     140     150     160

checked the input file for TAB delimited
Code:
 
 awk -F "\t" 'NF != 17' Inputfile.txt

Then use the below code to get 1st and last 10 columns
Code:
 
 awk 'BEGIN {FS="\t"}{printf("%s\t",$1)}{for(i=NF-9; i<=NF; i++) {printf("%s\t",$i)} printf "\n"} ' Inputfile.txt >> Inputfile1.txt

Inputfile1.txt

Code:
 
NAME    7       8       9       10      11      12      13      14      15      16
A       70      80      90      100     110     120     130     140     150     160
B       70      80      90      100     110     120     130     140     150     160
C       70      80      90      100     110     120     130     140     150     160
D       70      80      90      100     110     120     130     140     150     160
E       70      80      90      100     110     120     130     140     150     160

check to see if above is Tab Delimited but it is not

Code:
awk -F "\t" 'NF != 11' Inputfile1.txt

@Ravinder Thank you so much. Your code is working fine but I am not sure what is wrong with mine.
# 5  
Old 10-05-2016
Code:
awk 'BEGIN {FS="\t"}{printf("%s\t",$1)}{for(i=NF-9; i<=NF; i++) {printf("%s\t",$i)} printf "\n"} ' file | od -tx1
0000000 4e 41 4d 45 09 37 09 38 09 39 09 31 30 09 31 31
0000020 09 31 32 09 31 33 09 31 34 09 31 35 09 31 36 09
0000040 0a 41 09 37 30 09 38 30 09 39 30 09 31 30 30 09
0000060 31 31 30 09 31 32 30 09 31 33 30 09 31 34 30 09
0000100 31 35 30 09 31 36 30 09 0a 42 09 37 30 09 38 30
0000120 09 39 30 09 31 30 30 09 31 31 30 09 31 32 30 09
0000140 31 33 30 09 31 34 30 09 31 35 30 09 31 36 30 09
0000160 0a 43 09 37 30 09 38 30 09 39 30 09 31 30 30 09
0000200 31 31 30 09 31 32 30 09 31 33 30 09 31 34 30 09
0000220 31 35 30 09 31 36 30 09 0a 44 09 37 30 09 38 30
0000240 09 39 30 09 31 30 30 09 31 31 30 09 31 32 30 09
0000260 31 33 30 09 31 34 30 09 31 35 30 09 31 36 30 09
0000300 0a 45 09 37 30 09 38 30 09 39 30 09 31 30 30 09
0000320 31 31 30 09 31 32 30 09 31 33 30 09 31 34 30 09
0000340 31 35 30 09 31 36 30 09 0a

proves it IS <TAB> delimited. What you can see is that you have one <TAB> too many at the end-of-line.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-05-2016
Look into expand/unexpand to go between tabs/spaces. And if the number of columns will stay constant, you can just do cat <file> | cut -f1,7-16 (-d is defaulting to tabs).

R
This User Gave Thanks to rohmanovich For This Post:
# 7  
Old 10-05-2016
If we go back to your original code:
Code:
awk 'BEGIN {OFS="\t"}{printf("%s\t",$1)}{for(i=NF-9; i<=NF; i++) {printf("%s\t",$i)};printf "\n" } ' inputfile

I think you will find that the output is tab delimited, but your test is failing because you have 12 fields on each output line (with the last field being empty) instead of 11 fields. Two trivial changes to your code will fix the problem:
Code:
awk 'BEGIN {OFS="\t"}{printf("%s",$1)}{for(i=NF-9; i<=NF; i++) {printf("\t%s",$i)};printf "\n" } ' inputfile

or, since nothing in your awk script uses OFS:
Code:
awk '{printf("%s,$1)}{for(i=NF-9; i<=NF; i++) {printf("\t%s",$i)};print ""}' inputfile

Of course, there is also the brute force:
Code:
awk 'BEGIN{OFS="\t"}{print $1,$(NF-9),$(NF-8),$(NF-7),$(NF-6),$(NF-5),$(NF-4),$(NF-3),$(NF-2),$(NF-1),$NF}' inputfile

or:
Code:
awk '{print $1,$(NF-9),$(NF-8),$(NF-7),$(NF-6),$(NF-5),$(NF-4),$(NF-3),$(NF-2),$(NF-1),$NF}' OFS='\t' inputfile


Last edited by Don Cragun; 10-05-2016 at 05:37 PM.. Reason: Add another alternative.
This User Gave Thanks to Don Cragun 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

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

4. UNIX for Dummies Questions & Answers

Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal

I have the code for the below things.. File1 has the content as below 8859 0 subscriberCreate 18 0 subscriberPaymentMethodChange 1650 0 subscriberProfileUpdate 7668 0 subscriberStatusChange 13 4020100 subscriberProfileUpdate 1 4020129 subscriberStatusChange 2 4020307 subscriberCreate 8831... (5 Replies)
Discussion started by: Mahen
5 Replies

5. Shell Programming and Scripting

Fetching values in CSV file based on column name

input.csv: Field1,Field2,Field3,Field4,Field4 abc ,123 ,xyz ,000 ,pqr mno ,123 ,dfr ,111 ,bbb output: Field2,Field4 123 ,000 123 ,111 how to fetch the values of Field4 where Field2='123' I don't want to fetch the values based on column position. Instead want to... (10 Replies)
Discussion started by: bharathbangalor
10 Replies

6. Shell Programming and Scripting

Fetching columns from .csv file except last column

Hi, i have below list of files so i just want the name of the files in one parameter and not the timestamp. i want only GIDW_Dy_Tm_Seg_Sls_legacy_PL_0_0_ in variable of all files. GIDW_Dy_Tm_Seg_Sls_legacy_PL_0_0_20131001101800.csv GIDW_Dly_Sls_legacy_RO_0_0_20131001172001.csv ... (9 Replies)
Discussion started by: renuk
9 Replies

7. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

8. Shell Programming and Scripting

Help In fetching the 2nd Peak value of a column

Hi, We have a requirement of calculating the AVG, Peak and 2nd Peak (Say Peak2) of a column in a text file. We are able to get Avg and Peak but we are finding difficulty in getting the 2nd Peak. I tried using the AWK but in vainL. Please find the below Script i have written for fetching AVG and... (3 Replies)
Discussion started by: mahesh Madpathi
3 Replies

9. UNIX for Dummies Questions & Answers

two files.say a and b.both have long columns.i wanna match the column fron 1st file w

ex: a file has : 122323 123456456 125656879 678989965t635 234323432 b has : this is finance no. this is phone no this is extn ajkdgag idjsidj i want the o/p as: 122323 his is finance no. 123456456 this is phone no 123456456 ... (4 Replies)
Discussion started by: TRUPTI
4 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question