extracting substrings from variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting substrings from variables
# 8  
Old 05-27-2011
Great!! Smilie

I have just tested it, it will work just fine, i will replace the print $i with my instructions then.

Thanks a lot for your help!!
# 9  
Old 05-27-2011
You should remove the leading space
Code:
[ctsgnb@shell ~]$ echo $var |awk 'BEGIN{FS="[&][&]"}{for(i=1;i<=NF;i++){print $i}}'
/var/x/www
 /usr/x/share/doc
 /etc/x/logs
[ctsgnb@shell ~]$ echo $var |awk 'BEGIN{FS="[&][&]"}{gsub(" ","",$0);for(i=1;i<=NF;i++){print $i}}'
/var/x/www
/usr/x/share/doc
/etc/x/logs
[ctsgnb@shell ~]$

# 10  
Old 05-27-2011
Any idea why this double pipe causing problem. I am bit confused with that.
# 11  
Old 05-27-2011
double pipe means "OR"
cond1 || cond2
cond2 will be evaluated only if cond1 fails.

Code:
$ a=1
[ctsgnb@shell ~]$ [[ $a -eq 0 ]] && echo "OK" || echo "KO"
KO
[ctsgnb@shell ~]$ a=0
[ctsgnb@shell ~]$ [[ $a -eq 0 ]] && echo "OK" || echo "KO"
OK

# 12  
Old 05-27-2011
Code:
double pipe means "OR"

I am so lost here. I was think something is mystery using unix pipes ... I was totally out of context
# 13  
Old 05-27-2011
sorry to reopen this post.....Smilie

thanks to your solutions things got better but i am having a hard time integrating a command that normally works into the awk "loop" :
Code:
echo $NEW_PATH|awk 'BEGIN{FS="[&][&]"}{for(i=1;i<=NF;i++) find $i -mtime +$RET -exec rm {} \;}'

returns the following error :
Code:
awk: BEGIN{FS="[&][&]"}{for(i=1;i<=NF;i++) find $i -mtime +$RET -exec rm {} \;}
awk:                                                                     ^ syntax error
awk: BEGIN{FS="[&][&]"}{for(i=1;i<=NF;i++) find $i -mtime +$RET -exec rm {} \;}
awk:                                                                        ^ backslash not last character on line

I dont undestand this last character issue.....Smilie

Last edited by Franklin52; 05-27-2011 at 01:51 PM.. Reason: Please use code tags
# 14  
Old 05-27-2011
Another method to consider

Do you have control over the field separator character? If you used a more common separator like a pipe (or converted " && " to a pipe before processing it) you could parse the string and put the values into an array (I am using ksh93 on Solaris by the way):
Code:
#!/usr/dt/bin/dtksh

# Set a string variable.
pathlist="/var/x/www|/usr/x/share/doc|/etc/x/logs"

# Set the Input Field Separator. 
IFS='|'

# Create an array, with elements automatically parsed by the IFS string.
set -A parsed_pathlist_array $pathlist

# Prove it works.
print "1: ${parsed_pathlist_array[0]}"
print "2: ${parsed_pathlist_array[1]}"
print "3: ${parsed_pathlist_array[2]}"

exit 0

Just another way to go about it.

Gary
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: passing shell variables through and extracting text

Hello, new to the forums and to awk. Glad to be here. :o I want to pass two shell (#!/bin/sh) variables through to awk and use them. They will determine where to start and stop text extraction. The code with the variables hard-coded in awk works fine; the same code, but with the shell... (7 Replies)
Discussion started by: bedtime
7 Replies

2. UNIX for Dummies Questions & Answers

Extracting a block of text from a large file using variables?

Hi UNIX Members, I've been tasked with performing the following: Extract a block of data in column form #This data changes each time, therefore automating future procedures Please Note the following: line = reading a line from a file_list that leads to the data The filename is called... (16 Replies)
Discussion started by: Klor
16 Replies

3. Shell Programming and Scripting

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Windows & DOS: Issues & Discussions

Extracting variables between commas : GAWK or SED

Hello, I need some help, I got a CSV file called test.txt with this text in it : 08/02/2011;0,677;0,903;1,079;1,336;1,513;1,683 There's only a line and i need to copy theese numbers into variables : 0,677 0,903 1,079 1,336 1,513 1,683 The output file should look like this... (5 Replies)
Discussion started by: jujulips
5 Replies

5. Shell Programming and Scripting

extracting multiple variables from a filename.

hi all, I'm trying to automate some tasks and while I've got the script itself working, I'm having difficulties with automatic file detection and associated variable setting... for example, in a directory I've got several files... something along the lines of: xis0_NAME_src.file... (2 Replies)
Discussion started by: u5j84
2 Replies

6. Shell Programming and Scripting

Extracting variables from echo

Hello all. I can not remember the command to extract a variable from the date command. Basically what I need to do is to store the values of date in variable and rearrange them. I can not remember the command or the syntax to do so. so.. date Mon Mar 8 06:57:19 GMT 2010 $1 $2 ... (12 Replies)
Discussion started by: adelsin
12 Replies

7. Shell Programming and Scripting

extracting substrings

Hi guys, I am stuck in this problem. Please help. I have two files. FILE1 (with records starting from '>' ) >TC1723_3 similar to Scific_A7Q9Q3 EMSPSQDYCDDYFKLTYPCTAGAQYYGRGALPVYWNYNYGAIGEALKLDLLNHPEYIEQN ATMAFQAAIWRWMNPMKKGQPSAHDAFVGNWKP >TC214_2 similar to Quiet_Ref100_Q8W2B2 Cluster;... (1 Reply)
Discussion started by: smriti_shridhar
1 Replies

8. Shell Programming and Scripting

Extracting a users environment variables

Hi Guys, I want to extract users environment variables via a sh script, and for some reason it is not working. According to the su man page: Example 3: Executing command with user bin's Environment and Permissions To execute command with the temporary environment and per-... (2 Replies)
Discussion started by: Tornado
2 Replies

9. Shell Programming and Scripting

Breaking strings into Substrings

I'm only new to shell programming and have been given a task to do a program in .sh, however I've come to a point where I'm not sure what to do. This is my code so far: # process all arguments (i.e. loop while $1 is present) while ; do # echo "Arg is $1" case $1 in -h*|-H*) echo "help... (4 Replies)
Discussion started by: switch
4 Replies

10. UNIX for Dummies Questions & Answers

extracting return of ls -l into variables

If I do "ls -l filename" in a script, it should return something like this: -rw-r--r-- 1 user group 5945 Feb 28 14:24 filename How do I put each of the above strings into a different variable? eg Permissions, username, groupname, date (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question