Extracting a substring from a string in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a substring from a string in unix
# 15  
Old 05-20-2010
Code:
find . -name "*csv" -newer TODAY  -exec basename {} \; > temp1.txt

# 16  
Old 05-21-2010
Hi Madan,

I tried this and it worked fine...

In another case, i need to find the files and also list them.. how can i use basename in that?

Code:
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec ls -ltr {} \; > temp.txt

I am getting the output as -

Code:
-rw-r--r--   1 p494856  dp       2852189 May 21 00:15 ./CTHALL02.csv
-rw-r--r--   1 p494856  dp         35786 May 21 00:02 ./DV.csv

Will it be possible to remove the "./" from here and post the result back to the same file?
If so where should i make the change in the above code?

Regards
Arun
# 17  
Old 05-21-2010
Hi,

try with
Code:
ls -tr1

instead of
Code:
ls -ltr

I don't understand the error caused before....Smilie...but the solution of Matrixmadham is better...Smilie
# 18  
Old 05-21-2010
Hi Albert,

Code:
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec ls -trl {} \; > temp.txt

This is working fine and i'm not getting any errors. But I need to remove the "./" characters from the output...

Here is the output i'm getting now -

Code:
-rw-r--r--   1 p494856  dp       1868503 May 21 00:11 ./CLHALL02.csv
-rw-r--r--   1 p494856  dp       2852189 May 21 00:15 ./CTHALL02.csv
-rw-r--r--   1 p494856  dp         35786 May 21 00:02 ./DV.csv
-rw-r--r--   1 p494856  dp         14582 May 21 00:06 ./ODHALL02.csv

Madan suggested to use basename option to remove the "./"... but i'm not sure where to use it in the above code.

When i try without the ls -trl option, i can use the basename like below -

Code:
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec basename {} \; > temp.txt

will it be possible to have both ls -trl and basename in the same code?

Regards
Arun
# 19  
Old 05-21-2010
Code:
find . -name "*csv" -newer /auto/users-35/p494856/learning/scripts/TODAY -exec basename {} \; | awk '{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" substr($9,3,10000) }'

It's not very beautiful...but Smilie
I would like to know how put "the max lenght of the nineth field" instead of 100000 or a big number...Smilie

---------- Post updated at 02:19 PM ---------- Previous update was at 02:17 PM ----------

Sorry....put only:

Code:
substr($9,3)

this way shows the field from third character to the end!
# 20  
Old 05-21-2010
Hi Albert,

I am getting only the file names.
CLHALL02.csv
CTHALL02.csv
DV.csv
ODHALL02.csv

I'm not getting the details of the files like -

-rw-r--r-- 1 p494856 dp 1868503 May 21 00:11 CLHALL02.csv
-rw-r--r-- 1 p494856 dp 2852189 May 21 00:15 CTHALL02.csv
-rw-r--r-- 1 p494856 dp 35786 May 21 00:02 DV.csv
-rw-r--r-- 1 p494856 dp 14582 May 21 00:06 ODHALL02.csv
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Extracting substring from variable

Hi All, I'm facing issue wherein I have 2 character string like 'CR' and 'DR' and I want to extract just 1st character but am unable to do it. I tried below options but they are returning me 2nd character only, var="CR" echo ${var:1} returns just "R" echo ${var:0} returns "CR" ... (5 Replies)
Discussion started by: arvindshukla81
5 Replies

3. Shell Programming and Scripting

Help on extracting a substring from the input string

Hi, I am new to Unix. I am trying to extract a substring from an input string: Ex - input string: deploy_v11_9_1 i want to extract and store the value v11_9_1 from the input string in a new variable. I am using following command in my shell script file: echo "Enter the folder name u... (5 Replies)
Discussion started by: Pranav Bhasker
5 Replies

4. Shell Programming and Scripting

Need help in extracting the substring in UNIX

Hi all, I need to extract the "abcdef" from the below string Digital_abcdef_20130103.txt.gz The length of the "abcdef" will be changing but it will be always after the word Digital_ and before the second underscore help in this regard is highly appreciated (3 Replies)
Discussion started by: rithushri
3 Replies

5. Shell Programming and Scripting

Extracting substring

Hi, I have string in variable like '/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010' and i want to conevrt this string into only "DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010" (i.e file name). Is there any command to extracting string in some part ?(rather than whole path)? ... (5 Replies)
Discussion started by: shyamu544
5 Replies

6. Shell Programming and Scripting

Extracting substring from string

Hi awk and sed gurus, Please help me in the following. I have the following entries in the file ABCDErules AbHDPrules ABCrules -- -- and other entries in the file. Now, I want to extract from the file that contain entries for *rules and process it separately. How can i do it... (6 Replies)
Discussion started by: sdosanjh
6 Replies

7. Shell Programming and Scripting

Searching a substring from a string in UNIX

Hi all. I need help with a command to locate a substring from a string. My problem is I am passing a direcotry name as a command line argument and I need to ensure that user should not pass certain directories. E.g ther are Directories under ==> /opt/projects/* which should not be passed... (3 Replies)
Discussion started by: Veenak15
3 Replies

8. Shell Programming and Scripting

extracting substring from a file name

hi i need to name a file with a substring of a another file name. i.e. if the old filename is abc.txt , the new filename should be abc_1.txt i should get the substring of the file name and then name the new one please let me know how to do it (4 Replies)
Discussion started by: adityamahi
4 Replies

9. Shell Programming and Scripting

Extracting a substring starting from last occurance of a string/character

Hi All, This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character. for eg. I have a string of a file name with absolute path like... (2 Replies)
Discussion started by: krramkumar
2 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question