Results 1 to 8 of 8
  1. #1
    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.

  2. #2
    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.

  3. #3
    Join Date
    Mar 2007
    Posts
    30
    Thanks a lot aigles. It worked after a few changes. :-)

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    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.

  8. #8
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •