how can i compare te length of the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how can i compare te length of the string
# 1  
Old 09-15-2008
how can i compare te length of the string

how can i campare te length of the string

$var = unix

if ( $var -eq 4 )

then..

is it right
# 2  
Old 09-15-2008
Code:
var="UNIX"

if [ ${#var} -eq 4 ]; then
  # do something
fi

For basic usage like this you really should look at the appropriate man pages or tutorials.
# 3  
Old 09-15-2008
Code:
if [ `printf $var|wc -c` -eq 4 ];then echo "4"; fi

# 4  
Old 09-16-2008
ghostdog74: If $var contains whitespace or special characters, it needs to be double-quoted. The solution posted by Franklin52 seems more succinct and workable anyway; the built-in ${#var} length operator doesn't require any external processes, and doesn't have issues with quoting etc.
# 5  
Old 09-16-2008
true, then put quotes. anyway, its good practice too
Code:
if [ `printf "$var"|wc -c` -eq 4 ];then echo "4"; fi

this method should work on most shells, including bourne, csh
# 6  
Old 09-16-2008
Actually I believe the syntax for csh is different.
# 7  
Old 09-16-2008
Quote:
Originally Posted by era
Actually I believe the syntax for csh is different.
my bad. what i meant is the way to get length, not the if/else.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Compare string length to a number

Hi, I want to compare strings length to a number but i am getting error. I want first name should be length of 8. Please help. #bin !/bin/bash clear echo -n "Enter name " read name IFS=_ ary=($name) for key in "${!ary}"; do echo "$key${ary}"; done ##First name should be equal to 8... (8 Replies)
Discussion started by: rajneesh4U
8 Replies

3. Shell Programming and Scripting

String Length

Hi All, One of my source file is having Date column and the format of the column is YYYY-MM-DD. As per my business logic I have to check if the date format either YYY-MM-DD or YYYY-M-DD. If any records are in this format then I have print all the records and send those invalid records through... (4 Replies)
Discussion started by: suresh_target
4 Replies

4. UNIX for Dummies Questions & Answers

compare 2 very large lists of different length

I have two very large datasets (>100MB) in a simple vertical list format. They are of different size and with different order and formatting (e.g. whitespace and some other minor cruft that would thwart easy regex). Let's call them set1 and set2. I want to check set2 to see if it contains... (2 Replies)
Discussion started by: uiop44
2 Replies

5. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

6. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

7. Shell Programming and Scripting

How to Compare two files depend on line length Chars

I need to compare two files with exactly same length as example: - File1 contain 500 records with length of 640 chars of each line. File2 contain 1500 records with length of 640 chars of each line. I need get an output to be written in File3 which will contain 1000 records difference. but... (4 Replies)
Discussion started by: elj3dah
4 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. UNIX for Dummies Questions & Answers

length of the string

Hi all, pls help me in finding the length of the given string, do we need to write a code seperately or is there any command?? pls help. (3 Replies)
Discussion started by: vasikaran
3 Replies
Login or Register to Ask a Question