Visit Our UNIX and Linux User Community

How to print first 'n' columns?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print first 'n' columns?
# 1  
Old 02-12-2010
How to print first 'n' columns?

Hello All,

I have a file of 1000 columns. I need to print only the first 100 columns in to a new file. I have tried with the following code

HTML Code:
awk -F  '{ for(i=0; i<=100; i++) {printf $i}' inputfile > outputfile
But this is not working, I need the output file also of the same format of the input, but with out other columns.

Can anyone help me in this regard?

Expecting your reply and thanks in advance.

# 2  
Old 02-12-2010
delimiter=' '
cut -d "$delimiter" -f 1-100 "$file"

# 3  
Old 02-12-2010
Try something like
cut -d'*' -f1-100 inputfile > outputfile

where * = your field separator.
# 4  
Old 02-12-2010
Thank you very much "cfajohnson", the following code is working perfect.

delimiter=' '
cut -d "$delimiter" -f 1-100 "$file"

---------- Post updated at 11:22 AM ---------- Previous update was at 11:21 AM ----------

Thank you "Vi-Curious", it working fine.
# 5  
Old 02-12-2010
Originally Posted by Fredrick
Hello All,

HTML Code:
awk -F  '{ for(i=0; i<=100; i++) {printf $i}' inputfile > outputfile
The following is the right awk statement in your way of doing things,
awk -F' '  '{ for(i=1; i<=2; i++) {print $i} }' inputfile > outputfile

  • -F without option !
  • missing brace...
  • "print $0" is meant to print whole line.
Everything else is fine.

Previous Thread | Next Thread
Test Your Knowledge in Computers #637
Difficulty: Easy
As of January 2017, Apple's App Store had over 2.2 million apps for the iPhone.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Help with print if two columns is somehow similiar

Input File: GO:0009437,GO:0006355,GO:0006351 GO:0009437 GO:0006777,GO:0032324 GO:0035433,GO:0015992,GO:0071422 GO:0009082,GO:0009097,GO:0006566 GO:0009082,GO:0006351 GO:0000160,GO:0045893,GO:0006351 GO:0006071,GO:0045892,GO:0006351 GO:0009244 GO:0009244 GO:0046417,GO:0009094,GO:0006571... (12 Replies)
Discussion started by: perl_beginner
12 Replies

3. UNIX for Dummies Questions & Answers

Print Matches to New Columns

Hi all, I have a problem that I'm struggling to resolve. I have two files that look like this: File 1 654654654 3 987987987 2 321321321 1 File 2 14NS0064 654654654 14NS0054 654654654 14NS0032 654654654 14NS0090 987987987 14NS0093 987987987 14NS0056 321321321 As you may notice,... (2 Replies)
Discussion started by: winkleman
2 Replies

4. Shell Programming and Scripting

Print output as columns

Normal grep is not working to get the output. Sample Input: newjob: abc command name: a=b+c newjob: bbc command name: c=r+v newjob:ddc newjob:kkc command name: c=l+g newjob:mdc newjob:ldc newjob:kjc command name: u=dl+g newjob:lkdc newjob:lksdc command name: o=udl+g (6 Replies)
Discussion started by: onesuri
6 Replies

5. Shell Programming and Scripting

print columns with spaces

Hi All I've a file which is similar to the one given below column1 coulmn2 column3 column4 A B C D X Y F G H I would like to get it in this format A|B|C|D ||X|Y F||G|H Is... (4 Replies)
Discussion started by: Celvin VK
4 Replies

6. Shell Programming and Scripting

Print columns from each row

I have awk command to print column 8 awk '/select/ {print $8}' which will print column 8 But I need to print 3, 5 and 8 column in a row and each column should be de-limited by "\t" Hope anyone help me quickly. (2 Replies)
Discussion started by: elamurugu
2 Replies

7. Shell Programming and Scripting

using awk to print some columns of a file

Hi, i have a file with content 00:01:20.613 'integer32' 459254 00:01:34.158 459556 00:01:36.626 'integer32' 459255 and i want to print only output as below 00:01:20.613 459254 00:01:34.158 459556 00:01:36.626 459255 i dont want the word 'integer32' which is the second column. i... (2 Replies)
Discussion started by: dealerso
2 Replies

8. UNIX for Advanced & Expert Users

Print rows into columns

Hi, I required to print all rows into columns(some comma separated or else) till a pattern match found. Using Solaris UNIX scripting my input is like abc def ghi jkl mno END pqr stu vwx yz 123 END ... ... (7 Replies)
Discussion started by: vikash.rastogi
7 Replies

9. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies

10. Shell Programming and Scripting

want to print output if u have rows and columns

U have a file File 1 0.3 0.2 0.4 0.3 0.8 0.11 0.22 0.4 0.23 0.45 0.56 0.78 0.98 0.11 0.32 0.2 0.4 0.45 0.54 0.2 0.33 0.44 0.21 0.22 0.98 0.8 0.2 0.34 0.54 0.98 0.12 0.1 0.22 0.32 0.34 0.89 0.22 File 2 rows columns 3 1 2 3 4 2 5 ... (8 Replies)
Discussion started by: cdfd123
8 Replies

Featured Tech Videos