The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 12:18 PM
bash while read how to remove \n character papasj Shell Programming and Scripting 2 02-24-2008 08:01 AM
Read First Character of Each Line in File azelinsk Shell Programming and Scripting 7 07-11-2007 05:15 PM
Can I read a file character by character? murtaza Shell Programming and Scripting 3 03-29-2007 08:19 AM
need to read 3° character from a text file piltrafa UNIX for Dummies Questions & Answers 15 07-26-2005 07:19 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007
Registered User
 

Join Date: Nov 2007
Posts: 5
Can i read a file character by character

How to read character by character using awk
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-20-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,221
Either use fold -1 then pipe into awk, or loop through each character in awk, e.g....
Code:
$ cat file1
abc
$ fold -1 file1 | awk '{print $0}'
a
b
c
$ awk '{for(i=1;i<=length;i++) print substr($0, i, 1)}' file1
a
b
c
Reply With Quote
  #3 (permalink)  
Old 11-20-2007
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
What shell? Bash has the `read` builtin where you can do:
Code:
while read -n1 char; do
 #do something with the byte in $char
done <input.file
Reply With Quote
  #4 (permalink)  
Old 05-15-2008
Registered User
 

Join Date: May 2008
Posts: 2
what is -n1 heere in this syntax

could u explain what is "-n1" here in this syntax

Quote:
Originally Posted by LivinFree View Post
What shell? Bash has the `read` builtin where you can do:
Code:
while read -n1 char; do
 #do something with the byte in $char
done <input.file
Reply With Quote
  #5 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Oct 2007
Posts: 72
Quote:
read -n nchars
Read returns after reading nchars characters rather than
waiting for a complete line of input.
So -n1 means reading character by character.

Regards,
Chella
Reply With Quote
  #6 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: May 2008
Posts: 2
can u explain it little bit clear

what exactly the "number 1" do in the syntax

if i give 2 what will happen .... does it read 2 chars at a time

can u explain it little bit clear

regards
srikanth
Reply With Quote
  #7 (permalink)  
Old 05-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
Bash has a manual page and a built-in help system.

Code:
bash$ help read
...
If -n is supplied with a non-zero NCHARS argument, read returns after NCHARS
characters have been read.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0