Need help to extract a string delimited by any special character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to extract a string delimited by any special character
# 1  
Old 05-31-2005
Need help to extract a string delimited by any special character

I have a string as follows
IS*blahblah
TED~blahblah
etc.
I want to list down only IS and TED

Can someone help me?
# 2  
Old 05-31-2005
How about this ?

Code:
#! /bin/sh

while read line
do
tmp=`echo $line | awk -F"[*~]" '{ printf $1 }'`
echo $tmp
done < input.txt

You can use tmp if you need to retain the value.

If you need to avoid tmp then this should work

Code:
#! /bin/sh

while read line
do
echo $line | awk -F"[*~]" '{ printf $1 "\n" }'
done < input.txt

From the prompt this would work,

Code:
awk -F"[*~]" '{ printf $1 "\n" }' < input.txt

You can introduce all your required delimiters within the "[ ]" of -F

Vino

Last edited by vino; 05-31-2005 at 08:05 AM..
# 3  
Old 05-31-2005
MySQL use sed

Try This :--

sed -e 's/^IS.[a-z]*/IS/' -e 's/^TED.[a-z]*/TED/' < filename


Thnx
Rahul
Smilie
# 4  
Old 05-31-2005
Quote:
Originally Posted by vino
From the prompt this would work,
Code:
awk -F"[*~]" '{ printf $1 "\n" }' < input.txt

Can be simplified further....

awk -F"[*~]" '{print $1}' input.txt

Cheers
ZB
# 5  
Old 06-01-2005
Hi folks,

None of the solutions did not work. Can someone please help me.

My requirement was
The file has many lines. In each line, after 3 or 4 characters, there is a special character like *, ~ or + etc. The first 3 or 4 characters can be Alphabet, or Numeric. I wanted to extract the first 3 or 4 characters from each line with out the special character.

Thanks,
AK.
# 6  
Old 06-01-2005
And..

Code:
awk -F"[*~]" '{print $1}' input.txt

didnt work ??

If it didnt work, post the results of the above awk script for a sample section of your input file. Best would be the part of the input file, where the script fails.

Vino
# 7  
Old 06-01-2005
Thanks Vino for your help

My input file is as below

ISA~00~ ~00~ ~ZZ~12345 ~ZZ~54321 GS~SH~12345 ~54321
ST~86~000000007
BSN~00~0958
DTM~011~ET

And, when I ran the code above, the below results appeared

ISA~00~ ~00~ ~ZZ~12345 ~ZZ~54321 GS~SH~12345 ~54321
ST~86~000000007
BSN~00~0958
DTM~011~ET


My requirement is to get the first 2 or 3 characters like ISA, GS, ST, BSN and DTM. In this file, the special character is '~', but in other files it may vary.
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

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

3. UNIX for Dummies Questions & Answers

Extract string between two special characters

Hi, I have a file that looks something like >1-18*anc... (12 Replies)
Discussion started by: jyu429
12 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

How to replace with a special character in String

Hi, I am beginner to Shell Scripting. I have a String like this "testabcdef", i need the first character as it is and the remaining character should be replaced by the the '*' character. e.g(t***********) PLZ Suggest me. (5 Replies)
Discussion started by: nanthagopal
5 Replies

6. Shell Programming and Scripting

Extract character from string

ps -eaf | grep “oracleTRLV (LOCAL=NO)” | while read ora_proc do echo $ora_proc done I would like to modify the above shell so that if character 13 and 14 equal "12" to do something. Sorry I'm new to shell:( (14 Replies)
Discussion started by: NicoMan
14 Replies

7. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

8. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

9. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question