Command to parse a word character by character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to parse a word character by character
# 1  
Old 09-02-2009
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.74 ----> should be 6.74 only [ i mean no change required]
5,73 ----> should be 5.73 [change required has mentioned above]
777,9 ----> should be 777.9 [change required has mentioned above]
2000 -----> should be 2000 only [ i mean no change required]


Thanks in advance,
Giri
# 2  
Old 09-02-2009
Code:
echo "6,76" | tr ',' '.'

# 3  
Old 09-02-2009
Thanks a lot Anchal,it was perfect code to my requirment.

Thanks,
Giri
# 4  
Old 09-02-2009

It can be done much faster without an external command:

Code:
var=12,34
while :
do
  case $var in
    *,*) var=${var%%,*}.${var#*,} ;;
    *) break ;;
  esac
done
echo "$var"

# 5  
Old 09-03-2009
Quote:
Originally Posted by cfajohnson

It can be done much faster without an external command:

Code:
var=12,34
while :
do
  case $var in
    *,*) var=${var%%,*}.${var#*,} ;;
    *) break ;;
  esac
done
echo "$var"

Question.
Code:
*,*) var=${var%%,*}.${var#*,} ;;

Why %% and #* ?
Please explain this line.
# 6  
Old 09-03-2009
Quote:
Originally Posted by cola
Question.
Code:
*,*) var=${var%%,*}.${var#*,} ;;

Why %% and #* ?
Please explain this line.

It's basic parameter expansion that remove portions of the variable's contents; see the relevant section in your shell's man page.
# 7  
Old 09-03-2009
Quote:
Originally Posted by cola
Question.
Code:
*,*) var=${var%%,*}.${var#*,} ;;

Why %% and #* ?
Please explain this line.

those are shell's string operations.
%% deletes the longest part whenever pattern matches at the end.
# deletes the shortest part whenever pattern matched at the beginning.

this is just a short explanation.
agree with that you need to look at the man pages for details.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove word before a character

Hi, I have a file which looks like this id integer, name string, create_dt date, I want to remove all words that are present before the character , My output should be id, name, create_dt, Thanks wah (2 Replies)
Discussion started by: wahi80
2 Replies

2. Shell Programming and Scripting

Position of character in word

How can I represent the position of 1 (considering only the 1s after the colon) in the word from field5 and above; counting from right to left. Input: 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... (6 Replies)
Discussion started by: aydj
6 Replies

3. Shell Programming and Scripting

How can we get the character before and after a word in a string?

Hi friends, I am working in a korn shell. i want to know the command which gives me the previous one character and next one character of a given keyword in a string? for exmaple: input string: /bin/dir/folder1/.proc_name^folderone Keyword: proc_name output : .^ ... (10 Replies)
Discussion started by: neelmani
10 Replies

4. Shell Programming and Scripting

How to chose certain character in a word?

Hi Expert, I would like to know on how to export only the first 6 character of below 0050569868B7 ABCDEFGHTY to 005056 ABCDEF Thank you. Reggy (7 Replies)
Discussion started by: regmaster
7 Replies

5. Shell Programming and Scripting

parse special character in the line

Hi all, I have a file with some module names as below. Font::AFM Data::Grove ---> libxml-perl Net::LDAP ---> perl-ldap DBI XML .... ... .... and so on ... The file has some lines with the character " -->" . Now how can I cut only the last column of the line wherever "-->" is... (4 Replies)
Discussion started by: vijaya2006
4 Replies

6. Shell Programming and Scripting

Delete character from a word

Friends, I'm looking for a command that delete the first tho caractere in a word. Here is an exp : I want to replace "20091001" by "091001" or "replace" by "place" Thx, (13 Replies)
Discussion started by: newpromo
13 Replies

7. UNIX for Dummies Questions & Answers

How to delete all character before certain word

Hi, For example, i have a string "123 456 789 abc 111 222 333" and I would like to delete all the characters before abc so that it becomes "abc 111 222 333" how can i do that in unix? using sed? note: I actually don't know how many words/charachters before "abc", so the "cut"... (9 Replies)
Discussion started by: phandy
9 Replies

8. Shell Programming and Scripting

parse a file for a special character

hello, How to parse a file to see if a specific line is commented by '#' character? filename: file1 cat file1 ... # /usr/bin/whatever ... thank you (9 Replies)
Discussion started by: melanie_pfefer
9 Replies

9. UNIX for Dummies Questions & Answers

to search for a particular character in a word

Hi I would like to accept in a string from user like username/pwd@dbname suppose the user does not input @ then i should throw an error that @ symbol missing . How to achieve this Thanks in advance Suresh (6 Replies)
Discussion started by: ssuresh1999
6 Replies

10. UNIX for Dummies Questions & Answers

Grep for X character on a word

How can I grep for a certain letter that only shows on the 3rd letter or character. ex: ASGHDY SHTYRD SDTYRD IGIKGD I only want the TY part of the 3rd character so output would only be SHTYRD and DDTYRD - I only want the TY on the 3rd character. THANKS (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question