The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract the last character of a string annelisa Shell Programming and Scripting 8 07-05-2008 12:57 AM
Sed-Special character replacement usshell Shell Programming and Scripting 3 05-22-2008 11:06 AM
how to extract a tilde delimited file in unix trichyselva Shell Programming and Scripting 12 03-06-2007 12:19 AM
replacing string with special character ??? imppayel Shell Programming and Scripting 4 12-08-2004 05:07 AM
special character ? mile1982 High Level Programming 1 10-19-2004 09:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-31-2005
kumariak kumariak is offline
Registered User
  
 

Join Date: May 2005
Posts: 14
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 (permalink)  
Old 05-31-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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 (permalink)  
Old 05-31-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
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
  #4 (permalink)  
Old 06-01-2005
kumariak kumariak is offline
Registered User
  
 

Join Date: May 2005
Posts: 14
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.
  #5 (permalink)  
Old 06-01-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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
  #6 (permalink)  
Old 06-01-2005
kumariak kumariak is offline
Registered User
  
 

Join Date: May 2005
Posts: 14
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.
  #7 (permalink)  
Old 06-01-2005
kumariak kumariak is offline
Registered User
  
 

Join Date: May 2005
Posts: 14
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.

Thanks,
AK
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0