ksh: Comparing strings that contain spaces and working with substrings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh: Comparing strings that contain spaces and working with substrings
# 1  
Old 02-19-2008
ksh: Comparing strings that contain spaces and working with substrings

Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks.

Here are two such examples.

I want to save the first 19 characters of the following string to a variable.

"Operation Completed and blah blah blah"

I know this works (from another thread):

mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19

But I cannot figure out how to save the output of the result of the cut command to a variable.

So because I could not do that, I tried to compare the result of the cut command to a variable containing "Operation Completed", only I can't get the comparison to work. I keep getting :Operation not found when I run the following shell.

cstring="Operation Completed"
mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19
if [[ "$cstring" = $($mystring | cut -c 1-19) ]]; then
echo "they match"
fi

Thanks in advance.
# 2  
Old 02-19-2008
some ways:
Code:
mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19 | read myvariable
# or --------
myvariable=$(echo $mystring | cut -c 1-19 )
# or ---
myvariable=`echo $mystring | cut -c 1-19`

The ` bactick syntax is old but is allowed.
# 3  
Old 02-19-2008
Quote:
Originally Posted by nancylt723
Forgive me. I am very new to kornshell scripts. The simplest things stop me dead in my tracks.

Here are two such examples.

I want to save the first 19 characters of the following string to a variable.

"Operation Completed and blah blah blah"

I know this works (from another thread):

mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19

But I cannot figure out how to save the output of the result of the cut command to a variable.

So because I could not do that, I tried to compare the result of the cut command to a variable containing "Operation Completed", only I can't get the comparison to work. I keep getting :Operation not found when I run the following shell.

cstring="Operation Completed"
mystring="Operation Completed and blah blah blah"
echo $mystring | cut -c 1-19
if [[ "$cstring" = $($mystring | cut -c 1-19) ]]; then
echo "they match"
fi

Thanks in advance.
Code:
if [ "$cstring" = "$(echo $mystring | cut -c 1-19)" ]; then
  echo "they match"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing multiple substrings for a match

I have a tab-delimited file containing a large genetic dataset with binary base calls, in this format: Chr7 26021407 1/1:0,0,0:5 1/1:0,0,0:5 1/1:0,0,0:5 Chr7 26022023 1/1:0,0,0:3 1/1:0,0,0:3 1/1:28,3,0:5 Chr7 26022087 1/1:0,0,0:6 1/1:25,3,0:9 1/1:25,3,0:9 Chr7 26022656 1/1:0,0,0:3... (1 Reply)
Discussion started by: ljk
1 Replies

2. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

3. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

4. Shell Programming and Scripting

comparing two strings

hi All i am facing prob in comparing two strings that have two word. below is the code snippet. checkValidates="file validates" file3_name="file" if then echo "file" $file3_name "is validated successfully" fi when i run this i get the error as -bash: [: too many arguments ... (1 Reply)
Discussion started by: infyanurag
1 Replies

5. UNIX for Dummies Questions & Answers

Comparing filename-substrings and remove unnecessary files

hi folks... i have to write a sript that removes unnecessary backup-files. iam new to shell scripting so please be patient with me. and no its not homework :p these files look like "javacore303330.1209029863.txt" where the first number is the PID and the second is the timestamp. so there can be... (5 Replies)
Discussion started by: cypher82
5 Replies

6. Shell Programming and Scripting

comparing 2 strings

hi i have 2 strings. i want to compare the strings. please help (2 Replies)
Discussion started by: satish@123
2 Replies

7. Shell Programming and Scripting

Comparing Two Strings

Hi All, While I am trying to run below code I Am getting the exception like ./abs.sh: line 102: syntax error near unexpected token `then' ./abs.sh: line 102: ` then' The Code Snippet is: if then cat $file1 | sed -e... (8 Replies)
Discussion started by: Anji
8 Replies

8. UNIX for Advanced & Expert Users

Comparing strings

I have two strings a=Mar22 b=may21 how can I compare them Is this fine if then; . ... else .... fi or if then (2 Replies)
Discussion started by: yakyaj
2 Replies

9. Shell Programming and Scripting

comparing two strings

Hi How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help if then echo success fi I am not getting the desired output if I do this. plz help (24 Replies)
Discussion started by: ragha81
24 Replies

10. Shell Programming and Scripting

Breaking strings into Substrings

I'm only new to shell programming and have been given a task to do a program in .sh, however I've come to a point where I'm not sure what to do. This is my code so far: # process all arguments (i.e. loop while $1 is present) while ; do # echo "Arg is $1" case $1 in -h*|-H*) echo "help... (4 Replies)
Discussion started by: switch
4 Replies
Login or Register to Ask a Question