How to chose certain character in a word?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to chose certain character in a word?
# 1  
Old 09-19-2010
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
# 2  
Old 09-19-2010
Code:
$ ruby -ne 'print "#{$_[0,6]}\n"' file
005056
ABCDEF

This User Gave Thanks to kurumi For This Post:
# 3  
Old 09-19-2010
Don't forget to use code tags.

Code:
grep -o "^.\{6\}" infile
awk '{print substr($0,0,6)}' infile

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 09-19-2010
You guys are genius!

Many Thanks!

---------- Post updated at 06:48 PM ---------- Previous update was at 06:38 PM ----------

One more question, how do I pass the answer that I get from above and compare to existing file and match string , then produce output.

e.g
005056

grep 005056 textfile

But it has to be imported from above answer?

Thanks!
# 5  
Old 09-19-2010
Code:
grep -o "^.\{6\}" infile |xargs -i grep {} textfile

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 09-20-2010
Prompt reply and superb!

Thanks!
# 7  
Old 09-20-2010
Code:
ruby -ne 'BEGIN{s=[]};s<<$_[0,6];END{File.readlines("textfile").each{|l|s.each{|y| puts l if l.match(y) }  }}' infile

This User Gave Thanks to kurumi For This Post:
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

Chose list of sub directories in a bash script file

Hi, I have a command in my bash script, searchDirectoryName.sh: DIR_NAME=$(find . -type d) echo "${DIR_NAME}" . ./Dir1 ./Dir1/1.0.2.1 ./Dir2 ./Dir2/1.1 ./Dir3 ./Dir3/2.2.1 How can I select only following directory names with second subdirectoies and without first ./ in the... (3 Replies)
Discussion started by: hce
3 Replies

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

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

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

9. Shell Programming and Scripting

Chose option Utility

Hi, I am learning unix shell scripting. I want to create a utiltiy. I will explain about that. When I run my utility, it will display only the directories of a particular directory like the below output. For example My current directory is /home/balamv and it has 3 directories called Andrew,... (9 Replies)
Discussion started by: balamv
9 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