Select a specific part of the string and print it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select a specific part of the string and print it
# 1  
Old 11-30-2010
Select a specific part of the string and print it

Hi all,

I have a string that looks like:
Code:
#!/bin/sh

options="arguments: --user=alpha --group=beta --prefix=/usr/share --proxy-path=/proxy --proxy-tmp=/tmp --conf-path=/etc"

My goal is to transform the string into an array, then for each key, if it starts with "--proxy" to print the string after the equal sign:

Code:
#!/bin/sh

options=("arguments: --user=alpha --group=beta --prefix=/usr/share --proxy-path=/proxy --proxy-tmp=/tmp --conf-path=/etc")
for i in ${options[*]}; do
	if [ $i starts with "--proxy" ]; then
		option="the value present after the = sign"
		chmod 0755 $option
	fi
done

Thanks a lot for your help.
# 2  
Old 11-30-2010
Code:
awk -F'[" =]' '/proxy/{for(i=0;++i<=NF;){if($i ~ /--proxy/){system("chmod 0755 " $(i+1))}}}' file

# 3  
Old 11-30-2010
Standard sh dosn't support arrays, many linux flavours link /bin/sh to /bin/bash as this is probably what you have.

Your script is really close, a quick substring and delete from the from should get what you want:

Code:
#!/bin/bash
options=("arguments: --user=alpha --group=beta --prefix=/usr/share --proxy-path=/proxy --proxy-tmp=/tmp --conf-path=/etc")
for i in ${options[*]}; do
    if [ "${i:0:7}" = "--proxy" ]; then
        option=${i##*=} # the value present after the = sign"
        chmod 0755 $option
    fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print the specific part of the file name with file creation date?

Hello Folks, I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern. Filename: MobileProtocol.20171228T154200.157115.udr I want to get the count of files created on each day based on a pattern find. find . -type... (7 Replies)
Discussion started by: sadique.manzar
7 Replies

2. Shell Programming and Scripting

awk to print string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Print particular string in a field of csv file - part 2

Hi, all I need your help and suggestions. I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file. SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw... (7 Replies)
Discussion started by: refrain
7 Replies

4. Programming

Query to SELECT only Column Names that Contain a Specific String?

Hey Guys, I'm using SQuirreL SQL v3.5 GUI to fetch some data that I need for something I'm working on. I'm also using the IBM Informix Driver (*Version 3.5) to connect to the Database. What I want to do, if it's even possible, is to show all COLUMNS if they contain the word "Email". So in... (2 Replies)
Discussion started by: mrm5102
2 Replies

5. Shell Programming and Scripting

Print String Every Specific Line

Dear All, I have input file like this, 001 059 079 996 758 079 069 059 079 ... ... Desired output: AA 001 BB 059 (4 Replies)
Discussion started by: attila
4 Replies

6. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

7. UNIX for Dummies Questions & Answers

Print part of string

I have a file called file.txt It contains strings: ALT=someone@acme.com TO=whoever@lalalulu.com How could find and print the actual address after the = sign for any given instance? I need the command to print one of them - for example someone@acme.com But have in mind that this... (3 Replies)
Discussion started by: svetoslav_sj
3 Replies

8. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

9. UNIX for Dummies Questions & Answers

cut and print part of a string

I have a file that contains: yahoo.com.23456 web.log.common.us.gov.8675 192.168.1.55.34443 john-doe.about.com.22233 64.222.3.4.120 sunny.ca.4442 how can i remove the strings after the last dot (.) and reprint the file? Thanks. (3 Replies)
Discussion started by: apalex
3 Replies
Login or Register to Ask a Question