Can i read a file character by character | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Can i read a file character by character

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-20-2007
karnan karnan is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 24 September 2012, 8:23 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Can i read a file character by character

How to read character by character using awk
Sponsored Links
    #2  
Old 11-21-2007
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 111 Times in 98 Posts
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

Sponsored Links
    #3  
Old 11-21-2007
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
 
Join Date: Jul 2001
Last Activity: 16 June 2011, 4:50 PM EDT
Location: Portland, OR, USA
Posts: 1,626
Thanks: 2
Thanked 15 Times in 13 Posts
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

    #4  
Old 05-16-2008
srikanthg srikanthg is offline
Registered User
 
Join Date: May 2008
Last Activity: 22 February 2012, 11:31 AM EST
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #5  
Old 05-16-2008
chella chella is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 30 March 2009, 6:42 AM EDT
Posts: 75
Thanks: 0
Thanked 2 Times in 2 Posts
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
Sponsored Links
    #6  
Old 05-19-2008
srikanthg srikanthg is offline
Registered User
 
Join Date: May 2008
Last Activity: 22 February 2012, 11:31 AM EST
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #7  
Old 05-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
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.

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read the text file and print the content character by character.. samupnl Shell Programming and Scripting 1 06-10-2010 03:03 AM
How to read character by character in a file kartheek Shell Programming and Scripting 1 11-11-2009 07:31 AM
Can I read a file character by character? murtaza Shell Programming and Scripting 4 04-27-2009 05:51 AM
read in a file character by character - replace any unknown ASCII characters with spa raghav525 Shell Programming and Scripting 1 04-20-2009 01:52 PM
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 03:18 PM



All times are GMT -4. The time now is 04:11 AM.