DBAsupport.com Forums - Powered by vBulletin
Results 1 to 9 of 9

Thread: substr in shell

  1. #1
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    hi peeps, does anyone know how to extract the first 3 character in a text using shell?

    I have something like

    Code:
    for x in `grep "$" dir.txt`
    do
    	set -x
    	echo $x
    	awk '{print substr('$x', 1,3)}'
    done
    which obviously doesnt work because I dont know how to reference a variable inside awk, is this possible? Basivally what I am trying to do is extract the first 3 character of directory names inside dir.txt

    Btw is it possible to list just directories but not files using ls -al ?

  2. #2
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    ls -l | grep ^d
    Jeff Hunter

  3. #3
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    ls -l | grep ^d

    works if my files are not named with d, for example dir.txt appears :P

  4. #4
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    Code:
    for x in `grep "$" dir.txt`
    do
       echo $x | awk '{print substr($1,1,3)}'
    done
    Jeff Hunter

  5. #5
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    Originally posted by pando
    ls -l | grep ^d

    works if my files are not named with d, for example dir.txt appears :P
    Works on solaris, ksh. ls -l format is:
    drwxrwxrwx 2 owner group filename.txt

    You have violated your cardinal rule of specify OS and version...
    Jeff Hunter

  6. #6
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    yea try

    -rwxrwxrwx 2 owner group dir.txt (not working!)

    i am not telling my OS!

    ok ok it's windows 2000

  7. #7
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    Tried that, works on my system.

    The ^D (carrot D, not ctrl+D) tells grep to look at the first character of the line and ignore the rest.
    Code:
    dev42:/home/users/jeffh/pando $ ls -l  
    total 2
    -rw-r--r--   1 jeffh    progr          0 Oct 12 17:23 dir.txt
    drwxr-xr-x   2 jeffh    progr        512 Oct 12 17:24 mydir
    -rw-r--r--   1 jeffh    progr          0 Oct 12 17:23 readme.txt
    dev42:/home/users/jeffh/pando $ ls -l | grep ^d
    drwxr-xr-x   2 jeffh    progr        512 Oct 12 17:24 mydir
    Jeff Hunter

  8. #8
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    hmm may be MKS is not working properly in win 2k will try it on redhat later

  9. #9
    Join Date
    Oct 2000
    Location
    Saskatoon, SK, Canada
    Posts
    3,925
    The work of regular expressions

    Sam
    Thanx
    Sam



    Life is a journey, not a destination!


Posting Permissions

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


Click Here to Expand Forum to Full Width