Cut wildcard characters from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut wildcard characters from a string
# 1  
Old 03-17-2014
Cut wildcard characters from a string

Hello All,

I have to write a shell logic inside my ANT Script , the data from my ANT is in the string like

string a = "test1.props,test2.props,*,test3.props,?,test4,props"

how do i remove this wild card characters from my string a and assign them to an other string , like i only want to remove * ? and put them to other string

string b = "*, ?"
and
string c = "test1.props,test2.props,*,test3.props,?,test4,props"

because i wanted to put a logic where if this string value is * or ? , I will write a logic to exist the build and give the failure to user.
# 2  
Old 03-18-2014
Hi guy,
Could understood your string a to string b, may this could work.
Code:
$ a="test1.props,test2.props,*,test3.props,?,test4,props"
$ echo $a|awk '{for(i=1;i<=NF;i++){if($i~/[*?]/){$i=a[FR]++?","$i:$i;printf "%s",$i}}}' FS=','
*,?

But seems like that string c is same as string a. So what's the meaning of your "remove * ?"
# 3  
Old 03-18-2014
Hello Raokl,

Not sure if this meets your requirement, could you please try the following.
This is to remove * and ?.

Code:
echo "test1.props,test2.props,*,test3.props,?,test4,props" | awk 'gsub(/\*/,X) gsub(/\?/,Y) 1'

Output will be as follows.

Code:
test1.props,test2.props,,test3.props,,test4,props


Thanks,
R. Singh
# 4  
Old 03-18-2014
Hello Lucas,

Thank you very much for your reply

I am sorry for the typo error, the requirement should have to be

string a = "test1.props,test2.props,*,test3.props,?,test4,props"

so in this i need to remove the wildcard characters and assign them as the below

string b = "*,?"

string c = "test1.props,test2.props,test3.props,test4.props"

your commands works great , but is there any way that for example if i have below

string a = "test1.props,test2.props,*,test3.props,?,test4.props,*,test5.props,?"


then i need the loop to exit the moment it finds the first wildcard character , so in this case

string b="*"

string c="test1.props,test2.props,test3.props,test4.props,test5.props"

because based on the string b i will write a conditional statement in my ANT to display error to user if string b = "*" or string b= "?"

and continue my ANT operation on the string c

Sing- Thank you for your response but i am getting the below error

a ="test1.props,test2.props,*,test3.props,?,test4.props,*,test5.props,?"
Code:
echo $a| awk 'gsub(/\*/,X) gsub(/\,Y) 1'
awk: gsub(/\*/,X) gsub(/\,Y) 1
awk:                    ^ unterminated regexp
awk: cmd. line:1: gsub(/\*/,X) gsub(/\,Y) 1
awk: cmd. line:1:                          ^ unexpected newline or end of string


Last edited by Scrutinizer; 03-18-2014 at 02:06 AM.. Reason: code tags
# 5  
Old 03-18-2014
Hello Raokl,

Sorry my bad it should be as follows.

Code:
echo $a| awk 'gsub(/\*/,X) gsub(/\?/,X) 1'
 
cho "test1.props,test2.props,*,test3.props,?,test4,props" | awk 'gsub(/\*/,X) gsub(/\?/,Y) 1'

Thanks,
R. Singh
# 6  
Old 03-18-2014
Let us modify recent code a little, then may it works as your purpose.
Code:
$ echo $a|awk '{for(i=1;i<=NF;i++){if($i~/[*?]/){printf "%s",$i;exit}}}' FS=','
*
$ echo $a|sed 's/\(,\{0,1\}\)[*?]\(,\{0,1\}\)/\1\2/g;s/,,/,/g'
test1.props,test2.props,test3.props,test4,props

# 7  
Old 05-12-2014
Lucas- Thank you for the response , i am sorry for the delay in responding

your solution pefectly works

echo $a|awk '{for(i=1;i<=NF;i++){if($i~/[*?]/){printf "%s",$i;exit}}}' FS=','

but when i am using this in my ANT arguments it is throwing an error if we give operator < , XML is treating it as a closing tag and throwing parsing error , i tried my best to convernt it into CDATA but still it throws build error , Is there any other way/logic you would suggest me without using the '<' operator in i<=NF.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replace characters using a wildcard

Hello, I have some data that looks like the following, > <SALTDATA> (OVS0199262) HCl > <IDNUMBER> (OVS0199262) OVS0199262 > <SUPPLIER> (OVS0199262) TimTec > <EMAIL> (OVS0199262) info@timtec.net > <WEBSITE> (OVS0199262) http://www.timtec.net I need to remove the data in... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Cut a string for last 8 characters

Hello All I have a file like this abc.tpt.ctl bdc.tpt.ctl cdw.tpt.ctl I have looped every line using the for Loop, now I want to take each line and cut the .tpt.ctl part of it and store it in a variable and use the variable in same loop. The part I am stuck at is how do I cut the last... (9 Replies)
Discussion started by: nnani
9 Replies

3. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

4. Shell Programming and Scripting

sed cut characters of string

helloo I wonder if there's a way to cut characters out of a string and keep only the last 2 by using sed. For example if there's the todays' date: 2012-05-06 and we only want to keep the last 2 characters which are the day. Is there a quick way to do it with sed? (2 Replies)
Discussion started by: vlm
2 Replies

5. Shell Programming and Scripting

sed delete wildcard within a string

Hi I would like to batch delete the "note" entry from bib files. The start would be defined by "note ={" and the end by "}." (see example bib entry below). I tried the following command which does not have any effect: cat input.bib| sed -e 's/note = {.*}.//' > output.bib Any help would... (2 Replies)
Discussion started by: gerggeismann
2 Replies

6. Shell Programming and Scripting

Find replace a particular string of data with wildcard

Hi I am having a csv file in which lots of data are available wherein i need to find a particular kind of data and replace it with null value. here is the sample data.. I need to find the string starting with 404-064- and up to the first space i have to remove the data and keep the... (4 Replies)
Discussion started by: aemunathan
4 Replies

7. Shell Programming and Scripting

use sed do batch wildcard string replace

Hi, Here is what I want to do I want to search local directory and its sub directory, all the files which contain any string like _12345, then remove this string. String is a combination of _ plus a random integer number. For example, here is one line in a file before <properties... (1 Reply)
Discussion started by: bp5000
1 Replies

8. Solaris

Problem in using wildcard characters in xargs with find

Hi, Under my parent diectory I have directory named "Response" in many of its subfolders. I am interested to see all files with extention .pro in Response Directory. I am giving following command - find . -name "Response" -type d | xargs -i ls -lrt {}/*.pro but it is not giving result. ... (3 Replies)
Discussion started by: sanjay1979
3 Replies

9. Shell Programming and Scripting

how to cut string with wildcard (sed)

i got text file and contain.... SKYPE Dec 11 09:26:05 IN=eth0 OUT=eth1 SRC=75.38.161.80 DST=192.168.1.56 PROTO=UDP SPT=30645 DPT=12630 LEN=66 SKYPE Dec 11 09:26:05 IN=eth1 OUT=eth0 SRC=192.168.1.56 DST=118.109.39.86 PROTO=UDP SPT=12630 DPT=15889 LEN=75 SKYPE Dec 11 09:26:05 IN=eth1 OUT=eth0... (2 Replies)
Discussion started by: slackman
2 Replies

10. AIX

CUT command - cutting characters from end of string

Hello, I need to delete the final few characters from a parameter leaving just the first few. However, the characters which need to remain will not always be a string of the same length. For instance, the parameter will be passed as BN_HSBC_NTRS/hub_mth_ifce.sf. I only need the bit before the... (2 Replies)
Discussion started by: JWilliams
2 Replies
Login or Register to Ask a Question