How to find character position in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find character position in file?
# 1  
Old 07-11-2011
Tools 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
# 2  
Old 07-11-2011
Code:
string="123X568"
echo $string |awk -v s=X '{print index($1,s)}'

# 3  
Old 07-11-2011
I get the follwoing error:

awk: syntax error near line 1
awk: bailing out near line 1

---------- Post updated at 09:11 AM ---------- Previous update was at 09:11 AM ----------

I am using BASH
# 4  
Old 07-11-2011
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 07-11-2011
Or try..
Code:
echo ${string%X*} | wc -c

One more..
Code:
 echo "$string" | awk '{sub(/X.*/,"");print length($0)+1}'


Last edited by michaelrozar17; 07-11-2011 at 04:18 AM..
This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 07-11-2011
Another one:
Code:
echo 123x567 | awk -Fx '{print length($1)+1}'

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 07-11-2011
Code:
echo "123X568" | awk 'NF+=0' FS= OFS='\n' | awk '/X/{print NR}'


Last edited by Franklin52; 07-11-2011 at 01:36 PM.. Reason: Please use code tags
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

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

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

5. Shell Programming and Scripting

Remove " character from 25th position in file

Hi All, I want to remove " character if present in position 25th in CSV file. Each field in CSV file is separated by , and enclosed in "" (enclosed using double quotes) For example "1","2","3",........."123 Tom " is Good boy","26",........"45" Where "1" first position character, ...... 25th... (8 Replies)
Discussion started by: lancesunny
8 Replies

6. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

7. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: 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... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

8. Shell Programming and Scripting

How to remove a semi-repeating character in position 1 of a file

My file is in a good column format but several lines in the file begin with a zero. I'm in KSH and looking for a command to remove this zero and keep the text next to it. I don't want any of the zeros in the other columns removed. Below is a snip from the file... all I need to do is remove that... (2 Replies)
Discussion started by: right_coaster
2 Replies

9. UNIX for Dummies Questions & Answers

Sort file by character position

Hi! I need to sort file by certain column (column position from-to). I tried sort command, but it works wrong. Example: 0001 5214521 0148 6712145 I need to sort this 2 lines by values from position 9 to 12 (bold) Output: 0148 6712145 0001 5214521 Is there any way how to do this? (2 Replies)
Discussion started by: nadinnne
2 Replies

10. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies
Login or Register to Ask a Question