The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can I read a file character by character? murtaza Shell Programming and Scripting 4 04-27-2009 05:51 AM
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 03:18 PM
Can i read a file character by character karnan Shell Programming and Scripting 6 05-19-2008 02:22 AM
Read First Character of Each Line in File azelinsk Shell Programming and Scripting 7 07-11-2007 08:15 PM
Edit and insert character in a text file negixx Shell Programming and Scripting 3 06-10-2005 01:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-21-2005
piltrafa's Avatar
piltrafa piltrafa is offline
Registered User
  
 

Join Date: Oct 2001
Location: Buenos Aires, Argentina
Posts: 77
need to read 3° character from a text file

Hi,

I need a script to read the n° character from a text file.
eg: if the text file contains the line "123456" ,I nedd a command to display the number 4, as an example.

I tried with awk and printf but it seems only works with words separated with spaces, but in this case I have only one word "123456" and the characters are not separated.


Thanks in advance for your kindly help.
  #2 (permalink)  
Old 07-21-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
  #3 (permalink)  
Old 07-22-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
Phew,

vgersh99 can you throw some light on that.
It bounced over my head.
  #4 (permalink)  
Old 07-22-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by vgersh99
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
Code:
^        - from the beginning of the line
.\{3\}  - any 3 characters
\(.\)    - capture one character [the 4-th chars from the beginning]
.*       - any number of characters

\1       - print the first 'capture'
  #5 (permalink)  
Old 07-25-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'

Then won't this work.

echo 12345 | sed 's/^\(...\)\(.\).*/\1\2/'
  #6 (permalink)  
Old 07-25-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by vibhor_agarwali
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
4

Quote:
Originally Posted by vibhor_agarwali
Then won't this work.
echo 12345 | sed 's/^\(...\)\(.\).*/\1\2/'
1234

I don't know - depends on the the definition of 'works'
  #7 (permalink)  
Old 07-25-2005
Unbeliever Unbeliever is offline
Registered User
  
 

Join Date: Jul 2005
Location: England
Posts: 183
sed and awk wont work if the character you've got isn't on the first line of the file.

If you have GNU text utiliities installed you can do it using the versions tail and head in the distribution.

head -c 4 yourfile.txt | tail -c 1

If you wanted the 4th charcter for example. However this counts carriage returns and spaces as characters. If you want to ignore spaces and/or carraige returns you can do it but its a little more complicated.

It would be easy if you have access to perl:

cat youfile.txt | perl -e 'read(STDIN,$data,4); print substr($data,-1);'

again gives your the 4th character.

Sean
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0