Find the answer to your Linux question:
Results 1 to 9 of 9
i have filename like the following 1_40_37573.dbf 1_41_37573.dbf 1_42_37573.dbf the output of "echo $filename | cut -c3-4" is 40 41 42 i also have filenames like 1_101_37573.dbf 1_102_37573.dbf 1_103_37573.dbf so ...
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4

    get substring of filename

    i have filename like the following

    1_40_37573.dbf
    1_41_37573.dbf
    1_42_37573.dbf

    the output of "echo $filename | cut -c3-4" is
    40
    41
    42

    i also have filenames like
    1_101_37573.dbf
    1_102_37573.dbf
    1_103_37573.dbf

    so if i use the above command i'll get the output
    10
    10
    10
    actually i want to extract the substring between 2 underscores
    e.g. in case of 1_40_37573.dbf i need 40 and in case of 1_101_37373.dbf i need 101 and so on

    can anybody suggest to accomplish the task?

    regards,
    Muhammad Shakeel Azeem

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum
    here's one way

    awk 'BEGIN {FS = "_"}{print $2}' <filename
    the sun is new every day (heraclitus)

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    You can do it in two steps with the shell's built-in variable editing facility, which is quicker than running an external filter
    VAR=${filename#*_}
    VAR=${VAR%_*}
    echo $VAR

  4. #4
    Just Joined!
    Join Date
    Oct 2010
    Posts
    3
    If you want to use nearly the same expression, you can accomplish it simply with :
    Code:
    echo $filename | cut -d_ -f2
    Have a look at the man of cut, it is a very powerful command.
    Here the -d option tells which caracter should be considered as a delimiter, here it is the underscore caracter, and then with the -f option you tell it which field (which substring) to cut, here the second one.
    So if you have 1_40_.... or 16_468_.... or sausage_ham_... you'll always get the second field (40, 468, and ham there).

  5. #5
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    Here is another requirement
    Actually i am copying from remote location "/u02/flash_rec_area/" which contains files like

    1_41_732936151.dbf
    1_42_732936151.dbf
    1_43_732936151.dbf
    1_44_732936151.dbf
    1_45_732936151.dbf
    1_46_732936151.dbf
    1_47_732936151.dbf
    1_48_732936151.dbf
    1_49_732936151.dbf
    1_50_732936151.dbf
    1_51_732936151.dbf
    1_52_732936151.dbf

    2_13_732936151.dbf
    2_14_732936151.dbf
    2_15_732936151.dbf
    2_16_732936151.dbf
    2_17_732936151.dbf
    2_18_732936151.dbf

    now i want to just ignore the files start with 2_ ,to accomplish i am using the code mentioned below but its also ignoring the files like 1_42_732936151.dbf and 1_52_732936151.dbf which is against my requirement ,please suggest

    for filename in `ssh rac1 'ls '/u02/flash_rec_area/'' | sort | grep -v ' '2_' '`
    do
    echo $filename
    done

    thanks and regards,

  6. #6
    Just Joined!
    Join Date
    Oct 2010
    Posts
    3
    You can do
    Code:
    ls --format=single-column | egrep -v "^2_"
    According to the man the option -C is the equivalent to --format=single-column but it doesn't work for me.
    And the egrep will remove each line which begins with "2_".

    EDIT : i forgot the SSH command, but I think it should work fine with it.
    Last edited by Sathors; 10-29-2010 at 07:46 AM.

  7. #7
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    its working on local computer but not getting the desired results with ssh

    [oracle@standby dbscripts]$ pwd
    /u01/app/dbscripts

    [oracle@standby dbscripts]$ ls -al
    total 48
    drwxr-xr-x 2 oracle oinstall 4096 Oct 29 11:55 .
    drwxrwxr-x 8 oracle oinstall 4096 Oct 26 15:26 ..
    -rwxr--r-- 1 oracle oinstall 1499 Oct 29 10:56 archivemove1.sh
    -rwxr--r-- 1 oracle oinstall 1528 Oct 29 10:57 archivemove2.sh
    -rwxr--r-- 1 oracle oinstall 1410 Oct 29 11:11 archivemove.sh
    -rw-r--r-- 1 oracle oinstall 39 Oct 26 12:52 cmdfile.rcv
    -rwxr--r-- 1 oracle oinstall 820 Oct 26 19:06 generic.sh
    -rw-r--r-- 1 oracle oinstall 258 Oct 26 12:50 getrecid1.sql
    -rw-r--r-- 1 oracle oinstall 258 Oct 26 12:50 getrecid2.sql
    -rwxr--r-- 1 oracle oinstall 72 Oct 26 16:30 recover1.sh
    -rw-r--r-- 1 oracle oinstall 790 Sep 21 16:18 recover.sh
    -rwxr--r-- 1 oracle oinstall 268 Oct 29 14:59 test.sh

    [oracle@standby dbscripts]$ cat test.sh
    #!/bin/sh
    ls --format=single-column | egrep -v ''^2_''
    for filename in `ssh rac1 'ls --format=single-column '/uninstall/primdir'' | sort | egrep -v ''^2_''`
    do
    echo $filename
    done

    look at the output of test.sh

    [oracle@standby dbscripts]$ ./test.sh
    archivemove1.sh
    archivemove2.sh
    archivemove.sh
    cmdfile.rcv
    generic.sh
    getrecid1.sql
    getrecid2.sql
    recover1.sh
    recover.sh
    test.sh

    1_10_234.arc
    1_1_234.arc
    1_41_732936151.dbf
    1_42_732936151.dbf
    1_43_732936151.dbf
    1_44_732936151.dbf
    1_45_732936151.dbf
    1_46_732936151.dbf
    1_47_732936151.dbf
    1_48_732936151.dbf
    1_49_732936151.dbf
    1_50_732936151.dbf
    1_51_732936151.dbf
    1_52_732936151.dbf

  8. #8
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    my mistake

    its working

  9. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Sathors View Post
    You can do
    Code:
    ls --format=single-column | egrep -v "^2_"
    According to the man the option -C is the equivalent to --format=single-column but it doesn't work for me.
    The -1 (that's the digit one) option does single column output in ls, but of course, it does single column output anyway when its stdout is not connected to a terminal (try "ls | cat" for evidence). The -C flag is used to do multi-column, even into a pipe.

Posting Permissions

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