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
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
Need to serach if a new line character exists on the last line in a file sunilbm78 UNIX for Dummies Questions & Answers 10 02-29-2008 02:15 PM
need to read 3° character from a text file piltrafa UNIX for Dummies Questions & Answers 15 07-26-2005 10:19 AM

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-11-2007
azelinsk azelinsk is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
Read First Character of Each Line in File

I need a better way to read the first character of each line in a file and check if it equals the special character ¤. This character tells me where there is a break in the reports. The file has over 500,000 lines. Currently, this is my code -

if [[ $(echo $line|cut -c1-1) = "¤" ]]
I am using Korn Shell as a scripting language. Is there a better way to do this?

Thank you for your assistance.
  #2 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
I don't know if it will make much difference, but you can try this:
Code:
typeset -L1 mOne
while read mLine
do
  mOne=$mLine
done < input_file
  #3 (permalink)  
Old 07-11-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try:
Code:
if [ ${line:0:1} = '¤' ]
then
  # bingo
fi
Regards
  #4 (permalink)  
Old 07-11-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
${line:0:1}

works in bash just fine, but not ksh.
  #5 (permalink)  
Old 07-11-2007
Nick31 Nick31 is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
Quote:
Originally Posted by jim mcnamara View Post
${line:0:1}

works in bash just fine, but not ksh.
It would work if you use the enhanced korn shell (ksh93). For something that takes this long it might be worth testing to see which is faster, assigning it to a one character variable with typeset in the standard korn shell or using the enhanced korn shell's variable substitution. Of course going from 5 hours to 45 minutes is already a pretty nice improvement.
  #6 (permalink)  
Old 07-11-2007
azelinsk azelinsk is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
Shell_Life's suggestion helped a lot. Processing time went from over 5 hours to 45 minutes. This was the code:

typeset -L1 mOne
while read mLine
do
mOne=$mLine
done < input_file

jim mcnamara was correct that ${line:0:1} would not work.

Thank you for the help.
  #7 (permalink)  
Old 07-11-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Quote:
Originally Posted by azelinsk View Post
Quote:
${line:0:1}

works in bash just fine, but not ksh.
jim mcnamara was correct that ${line:0:1} would not work.
I apologize, didn't realise that.

Regards
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 12:10 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