ksh cut out words from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh cut out words from string
# 1  
Old 07-06-2010
ksh cut out words from string

Hi,

I have:
Code:
export string1=qwerWhatever
export string2=qwerWhatever1
export currdir=`pwd`

echo $currdir gives back:
Code:
/dir/dir/Whatever1

I want to take first 4 letters from string1 (in this case: qwer), compare it to string2 (in this case qwerWhatever1) and if string2 has in it string1, remove string1 from string2 and put it to string3, so string3 would be: Whatever1. And after this I want to compare if $currdir has $string3.

To take first four letters I can do:
Code:
echo $string1 | awk '{print substr($1,1,4) }'

but I am not sure what to do else.

Maybe somebody can help me how this can be established.

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 07-06-2010 at 08:55 AM.. Reason: code tags
# 2  
Old 07-06-2010
if your characters are same. what i meant was if the first four characters are always same then you can do something like this:-

Code:
lets just say the first four characters are =abcd
export string1=abcdWhatever
export string2=abcdWhatever1
export currdir=`pwd`
echo $currdir /dir/dir/Whatever1

if [[ `grep "abcd" string1` ]]
then 
   if [[ `grep "abcd" string2` ]]
   then
    cat string2 | grep -v "abcd" string2 > string3
   fi
fi

if [[ $curdir == string3 ]]
then 
echo "string 3 is equal to current directory"
fi

# 3  
Old 07-06-2010
with bash (version above 3)

Code:
$ cat match.sh 
#!/usr/local/bin/bash
 
 
string1=qwerWhatever
string2=qwerWhatever1
currdir=/dir/dir/Whatever1
 
 
s1f4=$(echo $string1 | cut -c1-4)
if [[ "$string2" =~ $s1f4 ]]; then
  echo $s1f4 found in string2
  strng3=$(echo $string2 | sed "s/\(.*\)$s1f4\(.*\)/\1\2/")
  if [[ "$currdir" =~ "$string3" ]]; then
        echo Current dir has string3
  fi
else
  echo Intial match failed
fi
$ 
$ 
$ 
$ 
$ ./match.sh 
qwer found in string2
Current dir has string3
$

# 4  
Old 07-06-2010
Quote:
Originally Posted by anchal_khare
with bash (version above 3)

Code:
$ cat match.sh 
#!/usr/local/bin/bash
 
 
string1=qwerWhatever
string2=qwerWhatever1
currdir=/dir/dir/Whatever1
 
 
s1f4=$(echo $string1 | cut -c1-4)
if [[ "$string2" =~ $s1f4 ]]; then
  echo $s1f4 found in string2
  strng3=$(echo $string2 | sed "s/\(.*\)$s1f4\(.*\)/\1\2/")
  if [[ "$currdir" =~ "$string3" ]]; then
        echo Current dir has string3
  fi
else
  echo Intial match failed
fi
$ 
$ 
$ 
$ 
$ ./match.sh 
qwer found in string2
Current dir has string3
$


Thank you, that's exactly what I want, but what this in bash means?

Code:
 =~

I am trying to search analog for KSH for it, because it reports that "=~" unexpected.
# 5  
Old 07-06-2010
That is bash's built-in comparison operator for regular expressions.
Introduced in bash version 3.
that will work only in bash shell. if you want to stick with the ksh, you probably need to use external commands grep/sed.

have a look here for more details.

Last edited by clx; 07-07-2010 at 03:13 AM..
# 6  
Old 07-07-2010
Another Solution.........

Code:
#!/usr/bin/ksh

string1=qwerWhatever
string2=qwerWhatever1
currdir=`pwd`

echo "string1 ==> $string1"
echo "string2 ==> $string2"
echo "currdir ==> $currdir"

string3=$(nawk -v VAR1=`echo $string1 | cut -c1-4` -v VAR2=$string2 'BEGIN {if (VAR2~VAR1) {gsub(VAR1,"",VAR2);print VAR2} else {print VAR1 " not found in string2"; system("exit")}exit;}' )
echo "string3 ==> $string3"

nawk -v VAR3=$string3 -v CURDIR=$currdir 'BEGIN {if (CURDIR ~ VAR3) print "currdir has" VAR3 ;else print "currdir does not have " VAR3 ; exit;}'

This User Gave Thanks to sachin.bajaj For This Post:
# 7  
Old 07-07-2010
no need of cut
Code:
string1=qwerWhatever
s1f4=${string1:0:4}
echo $s1f4
qwer

no need of sed
Code:
string2=qwerWhatever1
string3=${string2//$s1f4/}
echo $string3
Whatever1

This User Gave Thanks to daPeach For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to filter string between specific string in ksh

My argument has data as below. 10.9.9.85 -rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538 -rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr56scr 127869538 ....... (note all these between lines will start with hyphen '-' ) -rwxr-xr-x user1 2019-10-15 17:40... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Grep only words containing specific string

Hello, I have two files. All urls are space seperated. source http://xx.yy.zz http://df.ss.sd.xz http://09.09.090.01 http://11.22.33 http://canada.xx.yy http://01.02.03.04 http://33.44.55 http://98.87.76.65 http://russia.xx.zz http://aa.tt.xx.zz http://1w.2e.3r.4t http://china.rr.tt ... (4 Replies)
Discussion started by: baris35
4 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

5. Shell Programming and Scripting

ksh + isql => output cut at 2 GB

Using a ksh script, I'm dumping the data from our sybase database into an output file. This output file is for what ever reason cut at 2GB. There is enough space on the unix machine and as there is no error message is received I have no clue to start looking for a solution. #!... (1 Reply)
Discussion started by: bereman
1 Replies

6. Shell Programming and Scripting

splitting words from a string

Hi, I have a string like this in a file, I want to retrive the words separated by comma's in 3 variables. like How do i get that.plz advice (2 Replies)
Discussion started by: suresh_kb211
2 Replies

7. Shell Programming and Scripting

Need to cut filename in LINUX ksh

Hi, I need to cut filename in Linux ksh. for example file name is c_xxxx_cp_200908175035.zip. I need to get variable with only c_xxxx_cp value. (10 Replies)
Discussion started by: juliyp
10 Replies

8. Shell Programming and Scripting

ksh :: want to cut the strings

I have contents like 423562143124/53125351276 sdgas/347236 sjhdk;ls'ald/y62783612763 I need a command that would make the string before / and after / as separate output as (A should contain 423562143124 )and B should contain 53125351276). I tried but in vain. Please help. (19 Replies)
Discussion started by: rollthecoin
19 Replies

9. Shell Programming and Scripting

FInd the String between Two words

Hi I would like know how can write a script for find a string between two words. My input like this: a1 IN a1a1a1a1a1a1 OUT b1 IN b1b1b1b1b1b1 OUT c1 IN c1c1c1c1c1c1 OUT . . . now my out put like: a1a1a1a1a1a1 b1b1b1b1b1b1 c1c1c1c1c1c1 please help on this. (6 Replies)
Discussion started by: koti_rama
6 Replies

10. 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
Login or Register to Ask a Question