Position of character in word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Position of character in word
# 1  
Old 03-13-2014
Position of character in word

How can I represent the position of
Code:
1

(considering only the 1s after the colon) in the word from field5 and above; counting from right to left.

Input:
Code:
TT-124 06-03-14 08-02-10 FAS   CAT1:10
TT-125-1 05-03-14 10-06-08 CAS   CAT2:1010 FAT1:10000
TT-125-3 07-03-14 11-02-06 FAS   FAT1:1101
SS-120-1 05-03-14 09-04-07 FAS   CAT3:100000
AA-121-0 06-03-14 08-03-06 CAS   FAT2:11 CAT1:101100

Output:

Code:
TT-124 06-03-14 08-02-10 FAS   CAT1:1
TT-125-1 05-03-14 10-06-08 CAS   CAT2:3 CAT2:1 FAT1:4
TT-125-3 07-03-14 11-02-06 FAS   FAT1:0 FAT1:2 FAT1:3
SS-120-1 05-03-14 09-04-07 FAS   CAT3:5
AA-121-0 06-03-14 08-03-06 CAS   FAT2:0 FAT2:1 CAT1:2 CAT1:3 CAT1:5

# 2  
Old 03-13-2014
Code:
awk '{for(i = 5; i <= NF; i++) {split($i, a, ":"); $i = ""; split(a[2], b, "");
  for(j = 1; j <= length(b); j++) {if(b[j] == 1)
    {$i = ($i == "") ? (a[1] ":" length(b) - j) : ($i FS a[1] ":" length(b) - j)}}};
  print $0}' file


Last edited by SriniShoo; 03-13-2014 at 06:34 AM.. Reason: format
This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 03-13-2014
Given VAR holds the pattern you want to be considered, try sth like:
Code:
 for ((i=${#VAR}-1;i>=0;i--)); do [ 1 -eq "${VAR:$i:1}" ] && echo $((${#VAR}-i-1));  done

# 4  
Old 03-13-2014
You can do this within ksh/bash if you wish:-
Code:
while read col1 col2 col3 col4 col5
do
   test5="${col5%%1*}"                  # Truncate string after first occurrence of 1
   position="${#test5}"                 # Get the length from zero to full length of $col5
   ((position=$position+1))             # Add one to get the true position of search
   if [ $position -le ${#col5} ]        # Check we actually found the character.
   then
      print "Found character 1 at position $position."
   else
      print "Did not find character 1 in string $col5."
   fi
done < filename

Maybe someone would care to neaten this, but for a large file, it saves spawning multiple awk commands if you are reading it through a shell script.
# 5  
Old 03-18-2014
Quote:
Originally Posted by SriniShoo
Code:
awk '{for(i = 5; i <= NF; i++) {split($i, a, ":"); $i = ""; split(a[2], b, "");
  for(j = 1; j <= length(b); j++) {if(b[j] == 1)
    {$i = ($i == "") ? (a[1] ":" length(b) - j) : ($i FS a[1] ":" length(b) - j)}}};
  print $0}' file

Works in cygwin but not in Solaris 10 using nawk or /usr/xpg4/bin/awk
# 6  
Old 03-20-2014
try "gawk"
# 7  
Old 03-20-2014
You could also try this:

Code:
nawk '{
 for(i = 5; i <= NF; i++) {
  split($i, a, ":"); $i = "";
  for(j=b=length(a[2]);j;j--)
     if(substr(a[2],j,1)+0) $i=$i ($i?FS:"") a[1] ":" b - j}
}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 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

position of character in a line

i want to find the position of a character in a line , the first position, last, 5th occurence position , i ve tried grep -n , and expr index but they dont fit the bill. Please let me know if there is any other alternative (2 Replies)
Discussion started by: phpsnook
2 Replies

5. 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

6. Shell Programming and Scripting

Command to parse a word character by character

Hi All, Can anyone help me please, I have a word like below. 6,76 I want to read this word and check if it has "," (comma) and if yes then i want to replace it with "." (dot). That means i want to be changed to 6.76 If the word doesnot contain "," then its should not be changed. Eg. ... (6 Replies)
Discussion started by: girish.raos
6 Replies

7. Shell Programming and Scripting

URG!! Last position of a character

Hi. If I have files with names like abcd.20080625.1234.abc.XYZ abcd.20080625.1234.abc.XYZW how can I get the XYZ or XYZW part? is there something like an Index() function but in reverse order? I was thinking that if I can get the position of the last dot... (7 Replies)
Discussion started by: mrodrig
7 Replies

8. Shell Programming and Scripting

How to check a word position in a file ?

Hello everybody, I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1") Thank you for your help ! (7 Replies)
Discussion started by: tibo
7 Replies

9. Shell Programming and Scripting

Change Position of word character

Hi, I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field. 5874957|901125| 95874960|650614| 95874966|870308| 901125 to be changed as 25-11-1990 for eg Can someone help please ?? :) ... (6 Replies)
Discussion started by: cedrichiu
6 Replies

10. UNIX for Dummies Questions & Answers

Character position

Hi , I am required to view the fixed postion file very often . I am looking for the utility like this if the file has a one or multile line abcdefghijklmnopqr Utility should make my file look like this 12345678910111213141516-------------------------- abcdefghijk l m n o p q r ... (4 Replies)
Discussion started by: akrathi
4 Replies
Login or Register to Ask a Question