Getting last character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting last character
# 1  
Old 04-19-2011
Getting last character

Hi i am passingsome parameter while running the script. so ineed to take the last character of thse parameters. i know we can take value of parametr using $

But i need last character and last but one character to store in seperate variables.

This is because, i need to find out the records with the starting characters. i dont know how to pass parameter dynamically so i am appending these with file names.

Eg: Script.sh File01 File19 so on(can be no. of files might be more or less.).

then i can take the File01 and take the last and last but one characters.
Is there any other way to pass these values.
# 2  
Old 04-19-2011
Which script interpreter? For ksh/bash, there are length and substring operators, and arithmetic, and you can ask for a scan of a constructed string using 'expr', like the last argument is:
Code:
expr '$'"$#"
 
For quick and dirty, you can:
 
echo $* | tail -2c    # two is one for line feed and one for character

# 3  
Old 04-19-2011
We are using bash

Last edited by cnrj; 04-19-2011 at 05:37 PM..
# 4  
Old 04-19-2011
Well, I know what it can do, and I just look up the details when I occasionally need them, like google 'bash substring tutorial'.
# 5  
Old 04-20-2011
To get last and last but one character
Code:
VAR=$(echo 'File01'|sed 's/.*\(..$\)/\1/')
VAR=$(echo 'File01'|tail -c3)


Last edited by michaelrozar17; 04-20-2011 at 04:15 AM.. Reason: added variable VAR
# 6  
Old 04-20-2011
thanks to all.

I got the characters waht i want.

But there is two more problem with my script i am using positionsal parameters to pass the all parameter which are used while running the script.

Code:
 
for file in $@

this is working fine but i need to pass the directory path also as arguement. so how do we skip that path positional parameter.

Code:
 
For Eg: Script.sh <Directory path> <file1> <file2> <file3>....

2. some times there might be a chance to get the ebcdic format files how can we handle these files?

Below is my code:
Code:
#!/bin/bash
#################################################################################################
# This Script will check the Whether Headers and Trailers exist and not more than 1. If any file#
# does not meet this criteria script will return the non zero. If all files are ok it returns #
# zero(0). #
# Usage: Script.sh <filename><Header_identifier><Trailer_Identifier> <filename><Header_identifie#
#r><Trailer_Identifier>..... and so on. #
# #
#################################################################################################
set -x
Result=0
[ -f "$0.log" ] && > $0.log
function log
{
echo "$@" >> "$0.log"
}
for file in $@
do
################################################################################################
# Retrieve the File name, Header and Trailer identification from the arguements.
################################################################################################
file_temp=`echo $file`
length_file=${#file_temp}
filename=${file:0:length_file-2}
headerid=${file:length_file-2:1}
trailerid=${file:length_file-1:1}
################################################################################################
cd /informationserver/projectsw/TescoBank_SoW043/landing/
if [ -f $filename ]
then
if [ `grep "^[$headerid]" $filename | wc -l` = 1 ]
then
if [ `grep "^[$trailerid]" $filename | wc -l` = 1 ]
then
Result=` expr $Result + 0`
else
Result= ` expr $Result + 1`
log There is No Trailer in $filename
fi
else
if [ `grep "^[$trailerid]" $filename | wc -l` = 1 ]
then
Result=` expr $Result + 1`
log There is No Header in $file
else
Result=` expr $Result + 1`
log There is No Header&Trailer in $filename
fi
fi
else
log No $filename exists in the directory.
Result=` expr $Result + 1`
fi
done
 
echo $Result

# 7  
Old 04-20-2011
Parametrs: Usually, either:
  • those sort of parameters go first, and you shift them away after storing or acting on them, or
  • you recognise them right under the do, cd to that dir or store it in a variable, and continue.
EBCDIC: Once you detect that (how? name ? failure to parse as ascii ?), in ksh where there is /dev/fd/# or bash you can set the file interrogation name variable to "<( dd i-forget-the-details $file_in - )".

tail -3c, is this a WinDOS file with cr and lf? cat -v and cat -vte provide a nice way to see hidden characters, even on binary files.

Last edited by DGPickett; 04-20-2011 at 01:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

3. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

4. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

5. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

7. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

8. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

9. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

10. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies
Login or Register to Ask a Question