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

$ ruby -ne 'print "#{$_[0,6]}\n"' file
005056
ABCDEF

1 Like

Don't forget to use code tags.

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

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!

grep -o "^.\{6\}" infile |xargs -i grep {} textfile
1 Like

Prompt reply and superb!

Thanks!

ruby -ne 'BEGIN{s=[]};s<<$_[0,6];END{File.readlines("textfile").each{|l|s.each{|y| puts l if l.match(y) }  }}' infile
1 Like

When using variables in bash substitution :

~$ for W in 0050569868B7 ABCDEFGHTY; do echo ${W::6}; done
005056
ABCDEF
~$
1 Like