Find position of character in multiple strings in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find position of character in multiple strings in a file
# 1  
Old 07-01-2012
Find position of character in multiple strings in a file

Greetings.

I have a file with information like this:
Code:
AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?
AMNDHRKEEU?AMNDHREOEU?
AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU?

What I need to extract is the position, in every line, of every occurrence of '?'

A desired output would be something like:
Code:
1:12, 24, 36, 48
2:11, 22
3:9, 18, 28, 38

I'm using the bash shell
# 2  
Old 07-01-2012
One possible approach: you can use AWK with the field separator set to ?. Then, for each record, inspect the number of fields and their lengths to determine where the ? characters occur.

Analogously, you can do the same thing in the shell using IFS, read, the set builtin, a for-loop, and $#.

Regards,
Alister
# 3  
Old 07-01-2012
Code:
awk '{printf("%d:",NR);len=0;for(i=1;i!=NF;++i){len+=length($i)+1;printf("%d ",len)}printf("\n")}' FS="?" infile

or
Code:
awk '{printf("%d:",NR)}{for(i=1;i<=NF;++i)if($i == "?")printf("%d ",i)}{printf("\n")}' FS="" infile

This User Gave Thanks to complex.invoke For This Post:
# 4  
Old 07-01-2012
Thanks, huaihaizi3. The first one worked perfectly!
# 5  
Old 07-02-2012
Quote:
Originally Posted by huaihaizi3
Code:
awk '{printf("%d:",NR);len=0;for(i=1;i!=NF;++i){len+=length($i)+1;printf("%d ",len)}printf("\n")}' FS="?" infile

Just in case, be aware that should a blank line occurr in the data, the initial value of i will be greater than NF and the highlighted expression will never be true, an infinite loop (or at least a loop that runs until a field size limit is triggered by $i). Using < should fix it.

Regards,
Alister

Last edited by alister; 07-02-2012 at 03:37 AM..
This User Gave Thanks to alister For This Post:
# 6  
Old 07-02-2012
Code:
a="AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?"
$ echo $a | fold -w 1 | grep -n "?"

# 7  
Old 07-02-2012
Also to get rid of the spurious space at the end or to be able to specify an output field separator, we could use something like this:
Code:
awk -F? '{p=0; for(i=1;i<NF;i++)s=(i==1?NR ":":s OFS)(p+=length($i)+1)} p{print s}' OFS=", " infile


Last edited by Scrutinizer; 07-02-2012 at 04:57 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to find multiple strings on different lines in file?

Hello, I have spent considerable amount of time breaking my head on this and reached out here. here is the back ground. OS - Solaris 10 There are two strings '<Orin>sop' and '<Dup>two' which I wanted to look for in a file without the quotes on different lines and ONLY if both strings are... (5 Replies)
Discussion started by: keithTait309875
5 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Find position of character with awk

Hi Guys! Could anyone help me with?.. I have a line which says BCVGF%6$#900 .....How can we know which position is for % or say $ by command or script?There is any way to get a prompt by any script? Thanks a lot (6 Replies)
Discussion started by: Indra2011
6 Replies

3. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

4. Shell Programming and Scripting

How to find character position in file?

how to find character positionin file? i.e string = "123X568" i want to find the position of character "X". Thanks (6 Replies)
Discussion started by: LiorAmitai
6 Replies

5. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

6. Shell Programming and Scripting

Cut multiple data based on character position

How to extract multiple data based on character position. I need to fetch from 7-9 and 22-26 and there is no delimiter for 22-26 since it is part of the column. The file may have more than 1000 character long.I managed to pull any one but not both for example test data 12345 zxc vbnmlk... (1 Reply)
Discussion started by: zooby
1 Replies

7. Shell Programming and Scripting

Search for multiple strings in specific position

Hi, I need to search for some strings in specific positions in a file. If the strings: "foo1", "foo2" or "foo3" is on position 266 or position 288 in a file i want the whole line printed. Any idea how to do it? (5 Replies)
Discussion started by: HugoH
5 Replies

8. Shell Programming and Scripting

Finding character mismatch position in two strings

Hello, I would like to find an efficient way to compare a pair of strings that differ at one position, and return the difference and position. For example: String1 123456789 String2 123454789 returning something - position 6, 6/4 Thanks in advance, Mike (5 Replies)
Discussion started by: etherite
5 Replies

9. Linux

To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows: bmgcc bmgccftp bsmsftp bulkftp cctuneftp crbtftp crmpos cso gujhr I am doing manual grep for each of the string to find the line count. The command i am using right now is: grep mark... (3 Replies)
Discussion started by: salaathi
3 Replies

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question