creating a file using the fist column and printing second column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers creating a file using the fist column and printing second column
# 1  
Old 12-15-2009
creating a file using the fist column and printing second column

Hello all. I have a problem that I need help solving.

I would like to convert the following file:

Code:
human     pool1_12      10e-02 45 67
human     pool1_1899   10e-01 45 29
human     pool1_1829   10e-01 43 26
horse       pool1_343    10e-20 65 191
horse       pool1_454    10e-09 44 43
horse       pool1_2233   10e-01 40 22

to these two files:

human.txt

which contains
Code:
pool1_12
pool1_1899
pool1_1829

and file:

horse.txt

which contains
Code:
pool1_343
pool1_454
pool1_2233

Anyone have an idea how to accomplish this?

Last edited by Scott; 12-15-2009 at 06:30 PM.. Reason: Please use code tags
# 2  
Old 12-15-2009
Code:
awk '
 /horse/ { print $2 > "horse.txt" }
 /human/ { print $2 > "human.txt" }
' input_file

$ cat horse.txt 
pool1_343
pool1_454
pool1_2233
$ cat human.txt 
pool1_12
pool1_1899
pool1_1829

# 3  
Old 12-16-2009
hi,

for a more generic script try :-

Code:
fileLoc="" // initialize this
uniqNames=$(cat $fileLoc | awk '{print $1}' | uniq)
for i in $uniqNames
do
        grep $i $fileLoc | awk '{print $2}' >> $i.txt
done

Cheers
# 4  
Old 12-16-2009
Another one:
Code:
awk '{print $2 > $1 ".txt"}' file

# 5  
Old 12-16-2009
Hi akshay61286,

How do I execute the script that you put together? Where do I plug in the filename?

Thank you,

viralnerd
# 6  
Old 12-17-2009
hi ,

assuming that your file's absolute path is :- /home/dummy/viralnerd.txt
replace the first line as follows :-
fileLoc="/home/dummy/viralnerd.txt"

cheers

ps - remove //initialize this
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. UNIX for Dummies Questions & Answers

Creating a two column list of date pairs form a single column list

Hi all, looking for some help here. I'm what you'd call a dirty programmer. my shell scripts might be ugly, but they (usually) function... Say I have a single column text file with a list of dates (yyyymmdd) that represent the elevation of a point on that date (I work with land subsidence, so... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

3. UNIX for Dummies Questions & Answers

Locate the column names with their values in the file and the printing the same in the other file

I have text file in Linux with two rows : first row conmtain the column nam and the second row contain its value .I nned to fetch few columns first and then redirect the data of those colum in the another file. Any ideas?? (1 Reply)
Discussion started by: Anamica
1 Replies

4. Shell Programming and Scripting

Perl- creating a matrix from a 3 column file

Dear all, I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the... (2 Replies)
Discussion started by: gabrysfe
2 Replies

5. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

6. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

9. Shell Programming and Scripting

Creating/ammending Name Column in existing .txt file

With the help of this forum, I have a script with the following output: chr7 27104483 27105154 chr7 27106872 27110789 chr7 27111956 27112830 chr7 27114388 27125180 chr7 27126966 27131260 chr7 27135440 27137796 which was created by the following script: awk '1 == NR || $NF >= 1000 {... (6 Replies)
Discussion started by: awknerd
6 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