问题
I have lots of files that look like this:
136
155
223
783
344
455
230
.
.
.
And another file like this (the dictionary):
rs6427315 230
rs1171564 455
rs1609666 344
rs728917 155
rs728918 223
rs11264495 783
rs11805559 136
. .
. .
. .
And I want a new file that looks like this, that is reading the characters from my first file and substituting them with the match in column 1 from the other file:
rs11805559
rs728917
rs728918
rs11264495
rs1609666
rs1609666
rs1171564
rs6427315
Thanks in advance
回答1:
This can be a way:
$ awk 'NR==FNR {a[$2]=$1; next} $1=a[$1]' file2 file1
rs11805559
rs728917
rs728918
rs11264495
rs1609666
rs1171564
rs6427315
Explanation
Firstly it loops through the second file and stores the array a[number]=rxxxx
. Then it loops through the first file and replaces the field with its value in the array.
回答2:
Another way: Create a script-file and use it. (This -- saving a script file -- is good for if you have many input.txt)
sed -r 's|^(.*)\t(.*)|s/\2/\1/|' dictionary.txt > dictionary.sed
sed -f dictionary.sed input.txt
or if you don't need the script-file
sed -r 's|^(.*)\t(.*)|s/\2/\1/|' dictionary.txt | sed -f - input.txt
回答3:
How about this:
for number in $(cat file1)
do
grep " $number$" file2 | { read a b; echo "$a"; }
done
I'm not sure you meant that you have files with the names 136
, 155
, etc. or if you have a file with the contents which looks like what you've posted above. I assumed now the latter because your post suggested later that it is just one file. Feel free to correct my assumption, then I will adjust my answer.
来源:https://stackoverflow.com/questions/21912184/translating-character-from-a-list-to-my-file