awk '{print $ from file1.txt}'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk '{print $ from file1.txt}'
# 1  
Old 10-17-2011
awk '{print $ from file1.txt}'

Hi All,

I have a file1.txt where the index of the columns are placed. I want to get the columns from file2.txt corresponding to these index numbers.
I was usually using

Code:
awk '{print $5, $6, $2, $3, ...}' file2.txt > output.txt

However, this list is very long. So, i want to read the index from the file1.txt. could you please help me to do that. Thanks.


file1.txt (index for column)

Code:
5,6,2,3,8,9,...

file2.txt (large taxt file)

Code:
D F R T K L K T W Z P ...
F K L W W O T T Z K W ...
.
.

output.txt

Code:
K L  F R T W
W O K L  T Z
...

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-17-2011 at 01:15 PM.. Reason: code tags, please!
# 2  
Old 10-17-2011
Code:
nawk 'FNR==NR {idxN=split($0,idxA,",");next} {for(i=1;i<=idxN;i++) printf("%s%c", $idxA[i], (i==idxN)?ORS:FS)}' file1 file2

And please, start using code tags!

Last edited by vgersh99; 10-17-2011 at 01:28 PM..
# 3  
Old 10-17-2011
Is there any other way to do this without using nawk command. Because it gives me "nawk: command is not found"?
# 4  
Old 10-17-2011
Quote:
Originally Posted by senayasma
Is there any other way to do this without using nawk command. Because it gives me "nawk: command is not found"?
Here is another way, using unix script:
Code:
#!/usr/bin/ksh
set -A mNbr $(sed 's/^/0,/;s/,/ /g' File1)
typeset -i mI
typeset -i mJ
while read mLine; do
  mOut=''
  set -A mLetter $(echo '0 '${mLine})
  mI=1
  while [[ ${mI} -lt ${#mNbr[*]} ]]; do
    mJ=${mNbr[$mI]}
    mOut=${mOut}${mLetter[$mJ]}' '
    mI=${mI}+1
  done
  echo ${mOut}
done < File2

# 5  
Old 10-17-2011
Quote:
Originally Posted by senayasma
Is there any other way to do this without using nawk command. Because it gives me "nawk: command is not found"?
use either awk or gawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with... (6 Replies)
Discussion started by: msmehaffey
6 Replies

5. Shell Programming and Scripting

Print the new fail compared with file1

file1: A pass B fail E pass file2: B pass A fail E pass if from file2 introduced new failure then, print failure It should print: (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Shell Programming and Scripting

Delete file2.txt from file1.txt using scripting

Hi, I`m a total newbie, well my requirement is that i have 2 files I want to identify which countries i do not currently have in db.. how can i use the grep or another command to find this file .. i want to match all-countries.txt with countries-in-db.txt so the output is equal to... (11 Replies)
Discussion started by: beanbaby
11 Replies

7. Shell Programming and Scripting

awk to print value from txt file to csv

Hi, I want to print two columns from a .txt file to a .csv file using awk. data in text file: Application -------------------------------------------------- ----------- OS Related Issues 1 EMEA Solutions ... (8 Replies)
Discussion started by: prashu_g
8 Replies

8. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

9. UNIX for Advanced & Expert Users

print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies

10. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies
Login or Register to Ask a Question