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 > 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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read a file line by line VENC22 UNIX for Dummies Questions & Answers 4 10-30-2008 11:09 AM
query on how to search for a line and read 4th word from that line jaggesh UNIX for Dummies Questions & Answers 4 07-01-2008 10:21 PM
read the file line by line kittusri9 Shell Programming and Scripting 3 04-24-2008 08:26 AM
How to read last line of a txt file? yongho UNIX for Dummies Questions & Answers 2 06-13-2005 01:20 PM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 09:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-29-2008
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
read a file line by line in ksh

Hi,

In ksh we use 'while read line' statement to read a file line by line. In my input file I have 5 spaces appended at the end of each line. When I use while read line statement it chops off the spaces at the end of each line

Inp.txt
aaaa<five spaces>
bbbb<five spaces>
cccc<five spaces>

pgm.ksh
#!/bin/ksh
while read line
do
len=`echo "$line" | wc -c`
echo $len
done

my_output
5
5
5

But my expected output should be,
10
10
10

Kindly help me on this.

Thanks in advance,
Chella.
  #2 (permalink)  
Old 08-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You need to manipulate IFS to make spaces significant. Search for OLDIFS to see examples.
  #3 (permalink)  
Old 08-29-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
awk '{print $0,length($0)}' FILENAME
  #4 (permalink)  
Old 08-29-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
The read command splits the line into fields separated by white space by default. You can change this by redefining the inter-field separator (IFS). Try this:

Code:
#!/bin/ksh
while IFS="" read line
do
len=`echo "$line" | wc -c`
echo $len
done

Last edited by Annihilannic; 08-29-2008 at 01:54 AM.. Reason: D'oh, too slow. :-)
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 05:18 PM.


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