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
Can i read a file character by character karnan Shell Programming and Scripting 6 05-18-2008 11:22 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
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 03-29-2007
Registered User
 

Join Date: Mar 2007
Posts: 31
Can I read a file character by character?

Hello all respected people,
Can i read a file character by character without using sed,awk and perl commands.

Thanks in advance.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-29-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,391
Quote:
Originally Posted by murtaza
Hello all respected people,
Can i read a file character by character without using sed,awk and perl commands.
[...]
Oops, just saw "without awk ...",
so if you have fold


Code:
cnt=1; for i in $(fold -b1 inputfile);do
echo "Char # $cnt is $i"
cnt=$((cnt + 1)) 
done
With recent versions of bash:

Code:
cnt=1;while IFS= read -n1;do  
 echo "Char # $cnt is $REPLY"
 cnt=$((++cnt)) 
done<inputfile

Last edited by radoulov; 03-29-2007 at 08:43 AM.
Reply With Quote
  #3 (permalink)  
Old 03-29-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,639
Code:
#! /bin/ksh

typeset -L1 single
while read line
do
    while (( ${#line} > 0 ))
    do
        single="$line"
        # do what you like with $single
        echo $single
        line=${line#$single}
    done
done < input.txt
Read a line and process a character at a time.
Reply With Quote
  #4 (permalink)  
Old 03-29-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,434
Code:
while read -n 1 ch; do echo "$ch" ; done < file
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:14 AM.


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