Help with extracting text from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with extracting text from a string
# 1  
Old 02-25-2010
Help with extracting text from a string

I dont know if I am making any sense here. But I need to do something like this.

I have a variable that contains result from the svnlook command on a post-commit hook script.

Code:
test=`/usr/bin/svnlook changed $REPOS -r $REV | grep "^A.*index.html$"

and I get
Code:
test=A  /content/qa/lesson1/index.html A  /content/qa/lesson2/index.html

basically those two files are just been added to the repository.

Now I need to write some unix script to extract dirctory names lesson1 and lesson2 from that text.

How can I do this? Any help or suggestion is highly appreciated.

Thanks

KM

Last edited by Scott; 02-25-2010 at 06:25 PM.. Reason: Code tags
# 2  
Old 02-25-2010
Question Is your current output only on one line?

Unclear with your sample -- is the output on just one line? Or, is it on multiple lines?
# 3  
Old 02-25-2010
thanks for you response jowyg.

sorry my question is pretty not clear. let me know if you have any question.

I would like to have the output in an array coz i need to do more things with those names.
like
Code:
dir[0]=lesson1
dir[1]=lesson2

Thanks.

Last edited by Scott; 02-25-2010 at 06:25 PM.. Reason: Code tags
# 4  
Old 02-25-2010
Question The output from your command?

Is your output currently
Code:
test=A /content/qa/lesson1/index.html A /content/qa/lesson2/index.html

or
Code:
test=
A /content/qa/lesson1/index.html 
A /content/qa/lesson2/index.html

or something else

And, do you always want the 3rd field in your
Code:
/aaa/bbb/ccc/ddd/eee.html

lines?
# 5  
Old 02-25-2010
yes all the time i am looking into the third field. yes my output is usually like this
test=A /content/qa/lesson1/index.html A /content/qa/lesson2/index.html

I am checking the svn repository if there has been new index.html file added. There could be as many index.html file but the folder name is different. currently in the example I have two files. based on that I need to create a redirect file that points to that location on a server. So I need that folder name to create that url.
# 6  
Old 02-25-2010
Question So is this what you are trying to do?

Code:
>echo test=A /content/qa/lesson1/index.html A /content/qa/lesson2/index.html | gawk '{print $2"\n"$4}' | gawk -F"/"'{print "dir["NR-1"]=",$4}'
dir[0]= lesson1
dir[1]= lesson2

or simply append this to your current command?
Code:
| gawk '{print $2"\n"$4}' | gawk -F"/"'{print "dir["NR-1"]=",$4}'

# 7  
Old 02-25-2010
Thanks joeyg

Sorry if I am not understanding something but i am getting invalid range error:

Code:
$ echo test=A /content/qa/lesson1/index.html A /content/qa/lesson2/index.html | gawk '{print $2"\n"$4}' | gawk -F"/"'{print "dir["NR-1"]=",$4}'
gawk: fatal: Invalid range end: //{print "dir["NR-1"]=",$4}/

$ echo "A /content/qa/lesson1/index.html A /content/qa/lesson2/index.html" | gawk '{print $2"\n"$4}' | gawk -F"/"'{print "dir["NR-1"]=",$4}'
gawk: fatal: Invalid range end: //{print "dir["NR-1"]=",$4}/


Last edited by Scott; 02-25-2010 at 06:25 PM.. Reason: Code tags
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. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. Shell Programming and Scripting

Extracting text from within a section of text using AWK

I have a command which returns the below output. How can I write a script to extract mainhost and secondhost from this output and put it into an array? I may sometimes have more hosts like thirdhost. I am redirecting this output to a variable. So I guess there should be a awk or sed command to... (7 Replies)
Discussion started by: heykiran
7 Replies

4. Shell Programming and Scripting

Extracting several lines of text after a unique string

I'm attempting to write a script to identify users who have sudo access on a server. I only want to extract the ID's of the sudo users after a unique line of text. The list of sudo users goes to the EOF so I only need the script to start after the unique line of text. I already have a script to... (1 Reply)
Discussion started by: bouncer
1 Replies

5. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

Hi , I have input file and i want to extract below strings <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets <resCode>3000</resCode> => 3000 needs to be extracted <resMessage>Request time getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies

6. Shell Programming and Scripting

Extracting String

I am trying to extract a hyperlink from a html document using awk. I have managed to output in the format... href="index.html"> where i would like it just to output index.html. Any ideas on how i would do this? Thanks (2 Replies)
Discussion started by: adpe
2 Replies

7. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

8. Shell Programming and Scripting

extracting a string

Hi All, I am writing a shell script for which I am stuck with an extraction part. I arrived till extraction of a path of file. Lets take an example. I now have a file which contains following one line: 2348/home/userid/mydir/any_num_dir/myfile.text Now I want to extract only... (2 Replies)
Discussion started by: start_shell
2 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. Shell Programming and Scripting

extracting from a string

How do I extract 5th to 10th characters of string as given below stored in a shell variable. "ab cd ef gh ij kl" How is cut to be used on this? Thanks for any help. (1 Reply)
Discussion started by: preetikate
1 Replies
Login or Register to Ask a Question