943,712 Members | Top Members by Rank

Ad:
Jun 3rd, 2008
0

print first / last char from a file

Expand 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.
Similar Threads
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Jun 3rd, 2008
0

Re: print first / last char from a file

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.
Reputation Points: 13
Solved Threads: 9
Junior Poster in Training
omrsafetyo is offline Offline
58 posts
since Apr 2008
Jun 3rd, 2008
0

Re: print first / last char from a file

Click to Expand / Collapse  Quote originally posted by k2k ...
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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: new to linux shell script???????
Next Thread in Shell Scripting Forum Timeline: Replacing string in nth line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC