Read First Character of Each Line in File | 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.

Read First Character of Each Line in File

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-11-2007
azelinsk azelinsk is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 18 April 2008, 1:12 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
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

Sponsored Links
    #3  
Old 07-11-2007
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 20 December 2014, 8:12 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 126
Thanked 538 Times in 508 Posts
Try:

Code:
if [ ${line:0:1} = '' ]
then
  # bingo
fi

Regards
    #4  
Old 07-11-2007
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 21 December 2014, 9:40 PM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 822 Times in 764 Posts
${line:0:1}

works in bash just fine, but not ksh.
Sponsored Links
    #5  
Old 07-11-2007
azelinsk azelinsk is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 18 April 2008, 1:12 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old 07-11-2007
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 20 December 2014, 8:12 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 126
Thanked 538 Times in 508 Posts
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
Sponsored Links
    #7  
Old 07-11-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
Quote:
Originally Posted by azelinsk View Post
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.
what do you want to do after you have checked the first character equals something?

Code:
awk  '/^c/{ do something }' file

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 file from nth line to specific character laalesh Shell Programming and Scripting 3 03-22-2012 09:58 AM
How to read one character form each line of the file? arsii Shell Programming and Scripting 0 12-11-2010 10:07 AM
read the text file and print the content character by character.. samupnl Shell Programming and Scripting 1 06-10-2010 04:03 AM
bash: read file line by line (lines have '\0') - not full line has read??? alex_5161 Shell Programming and Scripting 6 04-14-2010 05:58 AM
read in a file character by character - replace any unknown ASCII characters with spa raghav525 Shell Programming and Scripting 1 04-20-2009 02:52 PM



All times are GMT -4. The time now is 10:44 PM.