Finding out the length of a string held within a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding out the length of a string held within a variable
# 1  
Old 01-11-2002
Network Finding out the length of a string held within a variable

Smilie Does anyone know which command I can use to find out the length of a string held within a variable?
# 2  
Old 01-11-2002
"length=${#variable}" works in ksh and bash.

For lower voltage shells, 'echo "$variable" | wc -c' comes close, but it includes the newline character so you need to subtract one.

length=`expr length "$variable"` used to work, but some modern versions of expr no longer support it.
# 3  
Old 01-11-2002
Computer Superb

Smilie Thank you very much for your help
# 4  
Old 01-27-2002
Quote:
Originally posted by Perderabo
"length=${#variable}" works in ksh and bash.

For lower voltage shells, 'echo "$variable" | wc -c' comes close, but it includes the newline character so you need to subtract one.

length=`expr length "$variable"` used to work, but some modern versions of expr no longer support it.
so how can i get the actual length of the word?
how to subtract 1 from the 'echo "$variable" | wc -c'
?
XXXXXXXXXX
# 5  
Old 02-09-2002
This was done on a Dynix/ptx 4.4.8 OS. (the NUMA-Q system formerly known as Sequent)

off_count=`echo "$variable" | wc -c`

or

off_count=$(echo "$variable" |wc -c)

Then the next two commands will calculate and display the result.

(( ok_count = $off_count - 1 ))
echo $ok_count
thekid
# 6  
Old 02-19-2002
try ...

let count=`$variable | wc -c`-1
echo $count

Smilie
inquirer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

3. Shell Programming and Scripting

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Match and variable length string

Hello all, source file looks like this: cat toto.txt NAME) VAR1=tata OPT4=toto USER=blabla TZ=/usr/share/zoneinfo/Hongkong OTHEROPT=something ;; NAME2) VAR1=tata OPT4=toto USER=blabla TZ=/usr/share/zoneinfo/Hongkong SOMETHING=else ... (5 Replies)
Discussion started by: maverick72
5 Replies

5. Shell Programming and Scripting

Finding the length of a string in a field

I have a field MPN-BANK-NUMBERO:006,00:N in a file FILE1 How can i display the length of the Bank Number.Any idea ? The output shud take the following format FILE 1 6 Where 1 is the starting position and 6 is the Ending position. (9 Replies)
Discussion started by: bobby1015
9 Replies

6. Shell Programming and Scripting

[bash] Executing script that is held in a variable

Hi, I'm building an installation system that uses separate data files that contain the individual characteristics of each package. Within the data file, I would like to incorporate a section that contains a bash script that is loaded into an array which is then redirected to bash to be... (13 Replies)
Discussion started by: ASGR
13 Replies

7. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

8. Shell Programming and Scripting

Replace variable length numeric string

I have a customer who logged some cc and bank account numbers in their apache logs. I got the cc numbers x'd out with sed -e 's/args=\{16\}/args=XXXXXXXXXXXXXXXX/g' -e 's/cardnum=\{16\}/cardnum=XXXXXXXXXXXXXXXX/g'but that wasn't too difficult due to the value being 16 digits. The bank account... (7 Replies)
Discussion started by: mk4mzid
7 Replies

9. Shell Programming and Scripting

Trying to create variable, value not being held

I tried creating a variable with the awk command. The correct value is being printed to the screen; however, the variable max is not being set. The command "echo $max" is null. Any help would be greatly appreciated. Thank you. cat test.txt: -rw-r--r-- 1 root root 0 2005-01-12 20:51... (2 Replies)
Discussion started by: cstovall
2 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question