If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Extracting a character from a string in KSH

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Mar 2007
Posts: 30
Post Extracting a character from a string in KSH

Dear All,

Good day. I'm having problems with my KSH.

I have a text file with lines like this: (impsrc.lst)

!WK29_20070720_1109_v26

=SCMUIUSER

This is my script:

cat impsrc.lst | while read line
do
{if line contains '=' then do} <--- don't know how to do this
filez=${line##*"="}
echo "Found: "$filez
{end if}
done
exit 1


What it does is strip away the charater '=' and just prints the lines without the '='. But it also prints all the lines which start with '!'. All I want is to just print the lines starting with '=' without the '='.

I was thinking of:

1. check if line contains '='
2. if yes then print without '='

So the output should be:
SCMUIUSER

Right now I get this as output:
!WK29_20070720_1109_v26

SCMUIUSER

Thanks in advance for all the help.
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 320
You can do something like that :
Code:
while read line
do
   if [[ "$line" = =* ]]
   then
      filez=${line#=}
      echo "Found: "$filez
   fi
done < impsrc.lst
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Mar 2007
Posts: 30
Thanks a lot aigles. It worked after a few changes. :-)
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,353
If all you want is to print the lines containing =
And strip the = and everything in front of the =

sed -n 's/\(.*\)=/Found: /p' impsrc.lst
Reply With Quote
  #5 (permalink)  
Old
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,527
or even ...
Code:
grep "^=" impsrc.lst | sed 's/^=//'
Which just shows lines that start with = and then strips the = at the start of the line.

Mike
Reply With Quote
  #6 (permalink)  
Old
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
With zsh:

Code:
zsh 4.3.4% cat file
!WK29_20070720_1109_v26

=SCMUIUSER

!WK29_20070720_1109_v26

=SCMUIUSER2
zsh 4.3.4% print -l ${${(M)$(<file):#\=*}#\=*}
SCMUIUSER
SCMUIUSER2
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
Join Date: Nov 2006
Posts: 19
In the example from aigles, would you put words you are looking for in quotes? I need to parse a file looking for specific words in the lines.

Thank you.
Reply With Quote
  #8 (permalink)  
Old
Registered User
 
Join Date: Nov 2006
Posts: 19
I should clarify that I need to see what is in the file line by line, so I cannot use grep.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On