Operating System - HP-UX
1825766 Members
2341 Online
109687 Solutions
New Discussion

Search the character by position

 
SOLVED
Go to solution
Muktha
Frequent Advisor

Search the character by position

Hi,
I want to get the nth character in a file.
Could you please help me to do so.
Regards
Muktha
9 REPLIES 9
Hemmetter
Esteemed Contributor
Solution

Re: Search the character by position

Hi Mukta,

try:

$ dd if=$FILE bs=1 skip=$(( $N -1 )) count=1

rgds
HGH

Re: Search the character by position

Hi,

cut -c n file

might work

Can you be more specific - is the file a text file? Do you want the nth character on each line, or just the nth charcter on a specific line? If you file is data do you just want the nth byte in the file?

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Muktha
Frequent Advisor

Re: Search the character by position

Hi,

Thanks for the response.
Its a text file.
'cut -c n filename' gives the nth character of each line.I want the nth character of the file , not each line.
Please help me to do so.

Regards
Muktha
Hein van den Heuvel
Honored Contributor

Re: Search the character by position

What is a character? utf-8? 16-bit? plain?
What is the first character? 0? 1?
Does newline count as a character?

two possible solutions in perl:

Seek to target and destroy...

$ perl -le 'seek STDIN,(shift)-1,0; print substr <>,0,1' 27 < x

Stomp through file and count:

$ perl -le '$offset=shift; while (<>) { if ($offset < length) { print substr $_,$offset-1,1; last } else { $offset -= length}}' 27 < x

Enjoy!
Hein.





Muktha
Frequent Advisor

Re: Search the character by position

Hi,

Can you give me another solution without using the perl command .

Regards
Muktha
Muktha
Frequent Advisor

Re: Search the character by position

Hi Hemmetter,

Thanks for the reply.
It works fine.

Regards
Muktha
Muktha
Frequent Advisor

Re: Search the character by position

Hi,
Thanks to all for the response.
Is there any way to grep the nth character?

Regards
Muktha
Dennis Handly
Acclaimed Contributor

Re: Search the character by position

>Is there any way to grep the nth character?

Well you can use HGH'g dd(1) fragment then compare the char with what you want.

Or you could use xd(1) then grep:
$xd -tc -N1 -j$(( N - 1)) file
0000000 s
0000001
Muktha
Frequent Advisor

Re: Search the character by position

I got the solution for my query.
Thanks to all.