print first / last char from a file

Reply

Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

print first / last char from a file

 
0
  #1
Jun 3rd, 2008
can anyone tell me how I can print the first or the last character from a sh file ?

i have done research on regular expression and the grep... i couldn't find any cmd that i can use to achieve this task. Thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: print first / last char from a file

 
0
  #2
Jun 3rd, 2008
I don't know of any command that will do this directly for you - and awk would probably be the way to go, but I'm no awk guru. This could probably also be done through some very impressive sed magic - but again, I'm no sed guru.

So doing this with a shell script, I would say the first logical step would be to get the first line, and the last line - from there we can manipulate those two strings.

Shell Scripting Syntax (Toggle Plain Text)
  1. FILE=$1 #read the filename as an argument
  2. FIRST_LINE=`head -1 $FILE`
  3. LAST_LINE=`tail -1 $FILE`

The next easy part will be grabbing the first letter, using cut:

Shell Scripting Syntax (Toggle Plain Text)
  1. FIRST_CHAR=`echo $FIRST_LINE | cut -c 1`

Of course, we could have skipped assigning the FIRST_LINE variable with this:

Shell Scripting Syntax (Toggle Plain Text)
  1. FIRST_CHAR=`head -1 $FILE | cut -c 1`

Getting the last character is a little more dificult.

Shell Scripting Syntax (Toggle Plain Text)
  1. num_chars=`echo $LAST_LINE | wc -c` # number of characters in the last line
  2. num_chars=`expr $num_chars - 1` #subtract 1 because counting starts at 1, not 0
  3. LAST_CHAR=`echo $LAST_LINE | cut -c $num_chars`

All in all, we end up with:
Shell Scripting Syntax (Toggle Plain Text)
  1. FILE=$1 #read the filename as an argument
  2. FIRST_CHAR=`head -1 $FILE | cut -c 1`
  3. LAST_LINE=`tail -1 $FILE`
  4.  
  5. num_chars=`echo $LAST_LINE | wc -c` # number of characters in the last line
  6. num_chars=`expr $num_chars - 1` #subtract 1 because counting starts at 1, not 0
  7.  
  8. LAST_CHAR=`echo $LAST_LINE | cut -c $num_chars`
  9.  
  10. echo "${FIRST_CHAR}${LAST_CHAR}"
  11.  
  12. #echo "${FIRST_CHAR} ${LAST_CHAR}" #put a space between
  13. #echo "${FIRST_CHAR}\n${LAST_CHAR}" #return on different lines

voila.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: print first / last char from a file

 
0
  #3
Jun 3rd, 2008
Originally Posted by k2k View Post
can anyone tell me how I can print the first or the last character from a sh file ?

i have done research on regular expression and the grep... i couldn't find any cmd that i can use to achieve this task. Thanks a lot.
Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN{FS=""}NR==1{print $1}{l=$NF}END{print l}' file
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC