Easy edit problem: interchange columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Easy edit problem: interchange columns
# 1  
Old 10-25-2010
Easy edit problem: interchange columns

hi;

my file1.txt:
Code:
cell137 1
cell337 1
cell355 1
cell355 3
cell360 1
cell360 2
cell360 3

my file2.txt:
Code:
...
cell137 1 20.64.1.97
cell137 2 20.64.1.97
cell137 3 20.64.1.97
...
cell337 1 20.64.1.113
cell337 2 20.64.1.113
cell337 3 20.64.1.113
...
cell355 1 20.64.1.145
cell355 2 20.64.1.145
cell355 3 20.64.1.145
...
cell360 1 20.64.1.161
cell360 2 20.64.1.161
cell360 3 20.64.1.161
...

and my code:
Code:
nawk 'NR==FNR{a[$1 FS $2]++;next} a[$1 FS $2] {$1=$2;print}' /gc_sw/file1.txt /gc_sw/file2.txt

my code searches file2.txt according to the values in file1.txt. then it takes the matched rows and get output as:
Code:
1 1 20.64.1.97
1 1 20.64.1.113
1 1 20.64.1.145
3 3 20.64.1.145
1 1 20.64.1.161
2 2 20.64.1.161
3 3 20.64.1.161

but i want output as:
Code:
20.64.1.97 1
20.64.1.113 1
20.64.1.145 1
20.64.1.145 3
20.64.1.161 1
20.64.1.161 2
20.64.1.161 3

how can i modify my code? SmilieSmilie

thanks Smilie
# 2  
Old 10-25-2010
Try this:
Code:
nawk 'NR==FNR{A[$1,$2]=$3;next} A[$1,$2]{print A[$1,$2],$2}' /gc_sw/file2.txt /gc_sw/file1.txt

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-25-2010
perfecto Scruti! thanks very much Smilie
# 4  
Old 10-25-2010
alternative
Code:
while read -r l ; do sed -n "/$l/ s/.* \(.*\) \(.*\)/\2 \1/p" file2.txt; done< file1.txt
20.64.1.97 1
20.64.1.113 1
20.64.1.145 1
20.64.1.145 3
20.64.1.161 1
20.64.1.161 2
20.64.1.161 3

This User Gave Thanks to ygemici For This Post:
# 5  
Old 10-25-2010
Quote:
Originally Posted by gc_sw
and my code:
Code:
nawk 'NR==FNR{a[$1 FS $2]++;next} a[$1 FS $2] {$1=$2;print}' /gc_sw/file1.txt /gc_sw/file2.txt

Update from your own code.

Code:
nawk 'NR==FNR{a[$1 FS $2]++;next} a[$1 FS $2] {print $NF,$2}' /gc_sw/file1.txt /gc_sw/file2.txt

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To transpose columns + edit in a txt file

Hi, I have a txt file that looks like log2FoldChange Ontology_term 8.50624450251828 GO:0003700,GO:0003707,GO:0005634,GO:0006355,GO:0043401,GO:0003700,GO:0005634,GO:0006355,GO:0008270,GO:0043565 7.03936870356684 GO:0005515,GO:0008080 6.49606183738682 6.49525073909629 GO:0005515... (4 Replies)
Discussion started by: alisrpp
4 Replies

2. Shell Programming and Scripting

Compute Difference and Edit second, third columns

Hi Friends, My input file is like this chr1 100 200 chr1 300 330 chr1 2000 2000 chr1 5000 5000 chr2 7790 7890 chr2 8000 8000 If the difference of third and second columns is zero, then subtract 500 from second column and add 500 to the third column. So, my output would be chr1... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

3. Shell Programming and Scripting

Help with this easy problem

hi people. i need assist in this quite easy problem. i have a text as: cell112-1/2/3/4/5/6 4 cell156-1/2/3/4 7 cell197-1/2/3 6 cell215-1/2/3/4/5/6 9 cell235-1/2/3 5 cell354-1/2/3/4/5 8 cell355-1 4 ... cell446-1/2/3/4/5/6 5 the script should check second coloumn in each line and ... (21 Replies)
Discussion started by: gc_sw
21 Replies

4. Shell Programming and Scripting

edit columns in unix shell script

i have a file which if you will open as xls will yield something like this: Column1 Column2 Column3 ABC CDE EFG GHI IJK KLM MNO OPQ QRS what i want to need is to have this kind of results: Column1 ABC CDE EFG GHI IJK ... (10 Replies)
Discussion started by: kingpeejay
10 Replies

5. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

6. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

7. Shell Programming and Scripting

How to interchange the lines

Hi, I want to know that, how to inter change the lines. In the following file two line are there first one is conatin PD next line PY. But i want to first line PY and next line PD. My file structure is like this: TI Electronic Publishing AU Robert PD... (5 Replies)
Discussion started by: shankarao
5 Replies

8. UNIX for Dummies Questions & Answers

bad substitution problem (easy question)

hi, what i want to do is to convert all the txt file under my directory to the properties file using the native2ascii command. however, when i run my script, i got bad substitution error. what's wrong with my script ? pls help. thanks #!/bin/sh curDIR=`pwd` oldExt='txt'... (10 Replies)
Discussion started by: champion
10 Replies
Login or Register to Ask a Question