Identify the position of character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify the position of character
# 1  
Old 12-10-2010
Java Identify the position of character

Hi,

Can some one guide me to identify the position of a character using index in UNIX.

I have a record like "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" . I need to identify the value which comes after _src:_ (_ denotes space).

I am able to identify the no. of characters till _src:_ as below:

s1="17/11/2010 15:16:39;reject;10.44.48.65;daemon alert src: 10.44.48.112; dst: 172.21.52.88"

s2=" src: "

start_val=`awk -v a="$s1" -v b="$s2" 'BEGIN{print index(a,b)+6}'`

But am not able to identify the character position of ";" which comes after _src:_ .

If I can identify that character position then I can pick the value using substr.

Can some one help me to identify the position.

Also If any one is having better approach please guide me.


Many Thanks in advance.
MSK
# 2  
Old 12-10-2010
Are u looking to extract an IP address comes after _src:_ ?? If its is so then

Code:
echo "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" | sed 's/.*; src: (.*); /$1/g'

output

10.44.48.112

and if u r having this data stored in file then

Code:
sed 's/.*; src: (.*); /$1/g' filename

R0H0N
# 3  
Old 12-10-2010
Thanks for your reply R0H0N.

But it is returning the value as my input.
# 4  
Old 12-10-2010
Make the changes in sed command for finding an exact string as per your requirement. Or, delimit the special characters in sed command.
R0H0N
# 5  
Old 12-10-2010
Hi R0H0N,

I executed the below command
echo "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" | sed 's/.*; src: (.*); /$1/g'

but it is returning 17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88

But am expecting the 10.44.48.112.

Many Thanks
# 6  
Old 12-10-2010
Quote:
Originally Posted by suneel.mekala
Hi R0H0N,

I executed the below command
echo "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" | sed 's/.*; src: (.*); /$1/g'

but it is returning 17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88

But am expecting the 10.44.48.112.

Many Thanks
Code:
sed -n 's/.*; src: \(.*\); /$1/g'

or

sed -n 's/.*; src: \(.*\); /\1/g'

R0H0N
# 7  
Old 12-10-2010
try this
Code:
echo "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" | sed 's/.*\(src: \)\(.*\);..*/\2/' -

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

Identify the First Column Position in Second Column and add the position value

Identify the First Column Position in Second Column and add the position value in 3rd column. Sample data: a|c b|d c|a d|b e|e f|g g|f |h |i Expected Output: a|c|1 b|d|2 c|a|3 d|b|4 (6 Replies)
Discussion started by: BrahmaNaiduA
6 Replies

3. Shell Programming and Scripting

Regex to identify word in second position on a line

I am interested in finding a regex to find a word in second position on a line. The word in question is या I tried the following PERL EXPRESSION but it did not work: ] या or ^\W या But both gave Null results I am giving below a Sample file: देना या सौंपना=delegate तह जमना या... (8 Replies)
Discussion started by: gimley
8 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

Get character position within variable

Hi all let's say i have a file named 1234_v2_abcd i need to find the position of the character located after _v, in above case this would be character number 2 the count of the characters before _v can change, but i always have a number after _v can anybody help :) (4 Replies)
Discussion started by: gigagigosu
4 Replies

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

7. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

8. Shell Programming and Scripting

How to identify the occurence of a pattern between a unique character?

hi, is it possible to find the number of occurences of a pattern between two paranthesis. for e.g i have a file as below. >>{ >>hi >>GoodMorning >>how are you? >>} >>is it good, >>tell me yes, if it is good In the above file, its clear the occurence of word "Good"... (17 Replies)
Discussion started by: divak
17 Replies

9. UNIX for Dummies Questions & Answers

How to split a value according to character position

Hello all, I have a script which picks up a series of large numbers, each of which are actually amalgamations of a series of other numbers. Unfortunately there are no separator characters so I can't use awk -F. I am looking for a way of splitting them into variables according to their... (4 Replies)
Discussion started by: michaeltravisuk
4 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