find positions of a letter in a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find positions of a letter in a text file
# 1  
Old 01-12-2011
find positions of a letter in a text file

Hi,

I would like to know how can I get all the positions of a letter, let say letter C in a text file.

sample input file:
hcck
pgog
hlhhc

desired output file:
2
3
13

Many thanks!
# 2  
Old 01-12-2011
Code:
awk -vFS= -vj=0 '{
 for(i=1;i<=NF;i++){
    j++;
    if($i~/[cC]/){
        print j 
    } 
    } 
}' file


Last edited by jim mcnamara; 01-12-2011 at 08:14 AM.. Reason: code tags please
# 3  
Old 01-12-2011
Thanks for the fast reply. Since I am a UNIX newbie, can you write the full command?

Thanks again.

---------- Post updated at 11:48 AM ---------- Previous update was at 11:46 AM ----------

ok, nevermind. It worked perfectly. Many thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting strings at various positions of text file

Hi Team - I hope everyone has been well! I export a file from one of our source systems that gives me more information than I need. The way the file outputs, I need to extract certain strings at different positions on the file and echo them to another file. I can do this in batch easily,... (2 Replies)
Discussion started by: SIMMS7400
2 Replies

2. Shell Programming and Scripting

Find flanking positions

I have a positions file with markers in col1 and position defined by chromosome and location in col2 and col3 m1 ch1 1 m2 ch1 5 m3 ch1 50 m4 ch2 567 m5 ch2 4567 m6 ch2 7766 m7 ch2 554433 m8 ch3 76 m9 ch3 456 m10 ch3 2315 Given a set of query marker, I would like to know what are the... (1 Reply)
Discussion started by: jianp83
1 Replies

3. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

4. Shell Programming and Scripting

Getting the non-homogenous letter row from a text file

I do have a large tab delimited file with the following format CCCCCGCCCCCCCCCCcCCCCCCCCCCCCCCCC 23 65 3 4 AAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAA 24 6 89 90 TGTTTTTTTTTTTTGGtTTTTTTTTTTTTTTTT 2 4 8 90 TTTT-TTTTTTTTTTTtTTTTTTTTTTTTTTTT 1 34 89 50 GGGGGGGGGGGGGGGGTGGGGGGGGGGGGGGGG 87 6 78 66... (8 Replies)
Discussion started by: Lucky Ali
8 Replies

5. Shell Programming and Scripting

Extract text between two character positions

Greetings. I need to extract text between two character positions, e.g: all text between character 4921 and 6534. The text blocks are FASTA-format sequence of whole chromosomes, so basically a million A, T, G, C, combinations. E.g: >Chr_1 ACCTGTTCAACTCTCAGGACTCTCAGGTCAACTCTCAG... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

6. Shell Programming and Scripting

Insert text with Sed (in various positions)

Hello. I'm trying to insert text in various positions and I could only do that using pipes for each position. Example: cat file | sed -e 's#\(.\{5\}\)\(.*\)#\1:\2#g' | sed -e 's#\(.\{26\}\)\(.*\)#\1:\2#g' Insert ":" at position 5 and 26. it can be done in the same sentence, without using... (4 Replies)
Discussion started by: </kida>
4 Replies

7. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

8. Shell Programming and Scripting

Replace 9-16 positions of a text file.

Hi i am having text file like this 40000201040005200213072009000000700000050744820906904421 40069300240005200713072009000000067400098543630000920442 i want to replace 9-16 positions of my txt file...by 1234567...in a single line command i.e 0400052....should be replaced by... (2 Replies)
Discussion started by: suryanarayana
2 Replies

9. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question