Reputation: 149
I am trying to export the byte offset of a specific character on every line of a piped input. For example, the output abc.txt:
....gugucaA....
.....guauAgggu..
.....ggguguAau..
should return:
11:A
10:A
12:A
I have tried using:
cat abc.txt | while read -r line; do grep -aob 'A'; done
As explained here https://unix.stackexchange.com/a/7561/327888.
However, this only offsets the output and retains the cumulative byte offsets of all 'A's.
ie.
11:A
21:A
33:A
Am I missing an option? Any help would be greatly appreciated!
Upvotes: 1
Views: 237
Reputation: 23667
If you just need the offset:
$ awk '{print index($0, "A")-1}' ip.txt
10
9
11
$ perl -lne 'print index($_, "A")' ip.txt
10
9
11
Upvotes: 3
Reputation: 7627
No need for multiple grep invocations. If your version of grep supports the P
option you could do:
grep -Po "[^A ]+(?=A)" inputfile | awk '{print length}'
output:
10
9
11
Upvotes: 1
Reputation: 88583
while read -r line; do grep -ob 'A' <<< "$line"; done < abc.txt
Output:
10:A 9:A 11:A
Upvotes: 3