| | |
print first / last char from a file
![]() |
•
•
Join Date: Apr 2008
Posts: 58
Reputation:
Solved Threads: 9
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.
The next easy part will be grabbing the first letter, using cut:
Of course, we could have skipped assigning the FIRST_LINE variable with this:
Getting the last character is a little more dificult.
All in all, we end up with:
voila.
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)
FILE=$1 #read the filename as an argument FIRST_LINE=`head -1 $FILE` LAST_LINE=`tail -1 $FILE`
The next easy part will be grabbing the first letter, using cut:
Shell Scripting Syntax (Toggle Plain Text)
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)
FIRST_CHAR=`head -1 $FILE | cut -c 1`
Getting the last character is a little more dificult.
Shell Scripting Syntax (Toggle Plain Text)
num_chars=`echo $LAST_LINE | wc -c` # number of characters in the last line num_chars=`expr $num_chars - 1` #subtract 1 because counting starts at 1, not 0 LAST_CHAR=`echo $LAST_LINE | cut -c $num_chars`
All in all, we end up with:
Shell Scripting Syntax (Toggle Plain Text)
FILE=$1 #read the filename as an argument FIRST_CHAR=`head -1 $FILE | cut -c 1` LAST_LINE=`tail -1 $FILE` num_chars=`echo $LAST_LINE | wc -c` # number of characters in the last line num_chars=`expr $num_chars - 1` #subtract 1 because counting starts at 1, not 0 LAST_CHAR=`echo $LAST_LINE | cut -c $num_chars` echo "${FIRST_CHAR}${LAST_CHAR}" #echo "${FIRST_CHAR} ${LAST_CHAR}" #put a space between #echo "${FIRST_CHAR}\n${LAST_CHAR}" #return on different lines
voila.
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
•
•
•
•
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)
awk 'BEGIN{FS=""}NR==1{print $1}{l=$NF}END{print l}' file
![]() |
Similar Threads
- Reading data file in C (C)
- How to output random line from a file (Java)
- C++ Reading from a text file (C++)
- 2D-Array, switches, and importing from a file (C++)
- writing a simple cat program (C)
- file access problem(access and oracle) (C++)
- reading a file into code (Java)
- Need Help (C)
- File parsing in 'C' (C)
Other Threads in the Shell Scripting Forum
- Previous Thread: new to linux shell script???????
- Next Thread: Replacing string in nth line
| Thread Tools | Search this Thread |





