Extract string between two special characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract string between two special characters
# 8  
Old 01-24-2015
Sorry, another follow-up.

In

Code:
awk '{ print $1}' |
while read num
do
  foo "$num"
done

supposing I did
Code:
awk '{ print $1 $2}' |

instead of
Code:
 awk '{ print $1}' |

where I only want to execute the while loop on argument $1 if argument $2 equals 16. How should I modify the following while loop? Thank you!
# 9  
Old 01-24-2015
Quote:
Originally Posted by jyu429
Sorry, another follow-up.

In

Code:
awk '{ print $1}' |
while read num
do
  foo "$num"
done

supposing I did
Code:
awk '{ print $1 $2}' |

instead of
Code:
 awk '{ print $1}' |

where I only want to execute the while loop on argument $1 if argument $2 equals 16. How should I modify the following while loop? Thank you!
What following loop?

With your sample input (and some of the suggested solutions to your problem, $2 would be anc). Comparing alphabetic text strings to numbers may be what you want to do, but your sample data would never meet this criteria.

The awk command print $1 $2 concatenates the two input fields into a single output field with no field separator between them. Is that really what you want to do? Did you perhaps intend to use print $1, $2 instead? Why change the loop? Why not just print the line in awk if $2 is greater than 16? For example:
Code:
awk '$2 > 16 {print $1}' file | while read num
do      do something with $num
done

# 10  
Old 01-24-2015
Building on wisecracker's proposal, you could try
Code:
while IFS=">*" read A B C; do [ "$C" == "anc" ] && echo call foo $B; done </tmp/textfile
call foo 1-18
call foo 1-2
call foo 1-24

Replace the comparison constant with 16 for your "real" data
# 11  
Old 01-24-2015
Quote:
Originally Posted by Don Cragun
What following loop?

With your sample input (and some of the suggested solutions to your problem, $2 would be anc). Comparing alphabetic text strings to numbers may be what you want to do, but your sample data would never meet this criteria.

The awk command print $1 $2 concatenates the two input fields into a single output field with no field separator between them. Is that really what you want to do? Did you perhaps intend to use print $1, $2 instead? Why change the loop? Why not just print the line in awk if $2 is greater than 16? For example:
Code:
awk '$2 > 16 {print $1}' file | while read num
do      do something with $num
done

Yes, sorry I did mean $1, $2. I think I'm trying to do something like

Code:
awk '{ print $1, $2}'| 
while read A B;
do 
echo $A, $B
if [ "$B" -eq "16" ];
then 
   grep -A 1 $A $1 | python unreverse.py  
else
   grep -A 1 $A
fi
done

where I apply some python script to the elements in awk output that have a flag of "16" and for the others I want to include in the output file, but not have to apply python unreverse.py.
# 12  
Old 01-24-2015
Try:
Code:
awk 'NF{print $1}' FS=\* RS=\> file |
while IFS=- read A B
do
  echo "A:$A B:$B"
done

# 13  
Old 01-24-2015
Hi jyu429...

Quote:
...where I apply some python script to the elements in awk output that have a flag of "16" and for the others I want to include in the output file, but not have to apply python unreverse.py.
I am curious and probably missing something, from these two lines...
1-18
...and...
1-24
...how are you going to obtain an "equals" value of 16 for the last 2, (1), or more digits at the end, if there exists a range of values linearly from 1?
Should it be "greater than or equal to" 16?
...OR...
Is there another event where there is NOT a range of values but a single fixed value instead?

EDIT:
Assuming "greater than or equal to"...
Code:
#!/bin/bash
# OSX 10.7.5, default bash terminal.
ifs_str="$IFS"
IFS=" >*"
echo ' >1-18*anc
caggttcagctggtgcagtctggagctgaggtgaagaagcctggggcctcagtgaaggtctcctgcaaggcttctggttacacctttaccagctatggtatcagctgggtgcgacaggcccctggacaagggcttgagtggatgggatggatcagcgcttacaatggtaacacaaactatgcacagaagctccagggcagagtcaccatgaccacagacacatccacgagcacagcctacatggagctgaggagcctgagatctgacgacacggccgtgtattactgtgcgagaga
>1-2*anc
caggtgcagctggtgcagtctggggctgaggtgaagaagcctggggcctcagtgaaggtctcctgcaaggcttctggatacaccttcaccggctactatatgcactgggtgcgacaggcccctggacaagggcttgagtggatgggacggatcaaccctaacagtggtggcacaaactatgcacagaagtttcagggcagggtcaccatgaccagggacacgtccatcagcacagcctacatggagctgagcaggctgagatctgacgacacggtcgtgtattactgtgcgagaga
>1-24*anc
caggtccagctggtacagtctggggctgaggtgaagaagcctggggcctcagtgaaggtctcctgcaaggtttccggatacaccctcactgaattatccatgcactgggtgcgacaggctcctggaaaagggcttgagtggatgggaggttttgatcctgaagatggtgaaacaatctacgcacagaagttccagggcagagtcaccatgaccgaggacacatctacagacacagcctacatggagctgagcagcctgagatctgaggacacggccgtgtattactgtgcaacaga' > /tmp/textfile
textarray=($(cat /tmp/textfile))
subarray=1
while [ $subarray -lt ${#textarray[@]} ]
do
	if [ ${textarray[$subarray]:$((${#textarray[$subarray]}-2)):2} -ge 16 ]
	then
		echo "Run a python script..."
	else
		echo "Do something else..."
	fi
	subarray=$((subarray+2))
done
IFS="$ifs_str"
exit 0

Results:-
Code:
Last login: Sat Jan 24 20:37:07 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./get16.sh
Run a python script...
Do something else...
Run a python script...
AMIGA:barrywalker~/Desktop/Code/Shell> _


Last edited by wisecracker; 01-24-2015 at 04:42 PM.. Reason: See above...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract string between two special chracters

Hi Folks - I'm trying to extract the string between two special characters, the "-" and "." symbols. The string format is as such: _PBCS_URL_PRD=https://plan-a503777.pbcs.us6.ocloud.com _PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.comIn the above case, I need to extract "a503777".... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. Shell Programming and Scripting

Help to replace the string with special characters

{"name":"alR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","description":"aLR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","json_class":"Chef::Role","default_attributes":{},"override_attributes":{"yoapp":{"jboss":"5.1.0","port":"2243","warname":"soap","datacenter":"alR","ip":"192.168.211.123","... (3 Replies)
Discussion started by: nikhil jain
3 Replies

3. Shell Programming and Scripting

How to remove some special characters in a string?

Hi, I have string like this ="Lookup Procedure" But i want the output like this Lookup Procedure =," should be removed. Please suggest me the solution. Regards, Madhuri (2 Replies)
Discussion started by: srimadhuri
2 Replies

4. Shell Programming and Scripting

Extract string between 2 special characters

Hi, I have a unix file with contents as below Line1: ABC MNN X$$QWERTY$$ JKL Line2: HELLO $$HOW$$ ARE $$YOU$$ DOING i want to extract the string between $$ and $$ ie i want the output as QWERTY HOW YOU i want those strings seperated by some character say | desired output is... (7 Replies)
Discussion started by: vinredmac
7 Replies

5. Shell Programming and Scripting

Remove string between two special characters

Hi All, I have a variable like AVAIL="\ BACK:bkpstg:testdb3.iad.expertcity.com:backtest|\ #AUTH:authstg:testdb3.iad.expertcity.com:authiapd|\ TEST:authstg:testdb3.iad.expertcity.com:authiapd|\ " What I want to do here is that If a find # before any entry, remove the entire string... (5 Replies)
Discussion started by: engineermayur
5 Replies

6. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
4 Replies

7. Programming

C++ Special Characters in a String?

Hello. How can i put all of the special characters on my keyboard into a string in c++ ? I tried this but it doesn't work. string characters("~`!@#$%^&*()_-+=|\}]{ How can i accomplish this? Thanks in advance. (1 Reply)
Discussion started by: cbreiny
1 Replies

8. Shell Programming and Scripting

Remove special characters from string

Hi there, I'd like to write a script that removes any set of character from any string. The first argument would be the string, the second argument would be the characters to remove. For example: $ myscript "My name's Santiago. What's yours?" "atu" My nme's Snigo. Wh's yors? I wrote the... (11 Replies)
Discussion started by: chebarbudo
11 Replies

9. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

10. Shell Programming and Scripting

Add string after another string with special characters

Hello everyone, I'm writing a script to add a string to an XML file, right after a specified string that only occurs once in the file. For testing purposes I created a file 'testfile' that looks like this: 1 2 3 4 5 6 6 7 8 9 And this is the script as far as I've managed: ... (2 Replies)
Discussion started by: heliode
2 Replies
Login or Register to Ask a Question