How to extract the certain position's character in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract the certain position's character in a string
# 1  
Old 06-30-2012
How to extract the certain position's character in a string

Suppose there are two files:
Code:
A, format is like:
line1   12
line2   33
line3   6
...

B, format is like:
Code:
>header
taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac
ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa
ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc
ctaccctaacccctaacccctaacccctaacccctaacccttaaccctaa
ccctaaccctaccctaaccctaaccctaacccctaacccctaaccc

There are only two lines in B files, one starting with ">" and the rest string is one line and they may be very long.

The problem is how to extract the exact character in B's string according to the 2nd columns' information in A, and output the characters in A's order?
I notice the awk substr function might be of help, but I didn't know how to handle with two files.

Thanks!

Last edited by Scrutinizer; 06-30-2012 at 05:51 AM.. Reason: code tags
# 2  
Old 06-30-2012
Code:
awk 'FNR==NR{a[FNR]=$2;next} FNR>1{for(i=1;a[i];i++) print substr($0,a[i],1)}' fileA fileB

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract all first numeric character from a string

Hello, I have a file of strings a below:- 4358RYFHD9845 28/COC/UYF984 9834URD 98HJDU I need to extract all the first numeric character of every sting as follows:- 4358 28 9834 thanks to suggest ASAP Regards, Jasi Use code tags, thanks. (7 Replies)
Discussion started by: jassi10781
7 Replies

2. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

3. Shell Programming and Scripting

URGENT!!!move character x of input string to some position

i have this prob I have some records in a file1 like this 1001 sajal singh tampa 1002 .... so on i have a pattern file which is like this 1,4 4,13 14,15 i have to read the first pair 1,4 and extract that from the first record so the pattern is 1001 now i have to scramble 1001... (1 Reply)
Discussion started by: spankincubus
1 Replies

4. Shell Programming and Scripting

Extract a character specifying position

hi , i am having a file Full_ARTMAS_20110510152425.xml in my local directory. i wanted to extract the character at the 35143546 th position at line 1 of this file.Can any body help me how to do it??? regards Anjali (2 Replies)
Discussion started by: angel12345
2 Replies

5. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

6. Shell Programming and Scripting

Extract character from string

ps -eaf | grep “oracleTRLV (LOCAL=NO)” | while read ora_proc do echo $ora_proc done I would like to modify the above shell so that if character 13 and 14 equal "12" to do something. Sorry I'm new to shell:( (14 Replies)
Discussion started by: NicoMan
14 Replies

7. UNIX for Dummies Questions & Answers

Depending upon character at particular position, string needs to be appended

Depending upon character at particular position in a line, string needs to be appended followed by empty spaces till 256th column position at the end of the each line in the file. example: 123p4 J8769 123q4 R2345 123r4 S2345 What I Require is, it has to check at 7th column character and... (10 Replies)
Discussion started by: vaka
10 Replies

8. Shell Programming and Scripting

Extract the last character of a string

How can I extract the last character of a string (withou knowing how many characters are in that string ! ) (8 Replies)
Discussion started by: annelisa
8 Replies

9. Shell Programming and Scripting

How to add character in specific position of a string?

Hi All, I would like to use sed to add "-" between the following string: Value: 20060830 Result: 2006-08-30 Pls advice. Thx a lot Victor (5 Replies)
Discussion started by: victorlung
5 Replies

10. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies
Login or Register to Ask a Question