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
need help extracting this part finalight Shell Programming and Scripting 6 05-20-2008 06:03 AM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 03:17 AM
Extracting part of the basename madhunk Shell Programming and Scripting 3 02-13-2007 11:54 AM
extracting uncommon part between two files sabyasm Shell Programming and Scripting 2 11-06-2005 01:25 PM
extracting from a string preetikate Shell Programming and Scripting 1 03-11-2004 08:08 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 04-23-2007
sam_78_nyc sam_78_nyc is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 16
Extracting part of a string

Hi all,
I have to extract only the second part of a database column (VARCHAR) and the value is seperated by a "~"

xyz~
chxyz36r~
abder~000082685
mnops~000083554
fulfil302~00026

Above are some examples of the values and for each record I have to extract the value after "~" , if there is a value by using shell scripts. As you can see they are multiple lenghts and in multiple formats and the only way to seperate them is by a "~".
Thanks in advance.
  #2 (permalink)  
Old 04-23-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
How about this...
Code:
$ cat script
#! /usr/bin/ksh

echo "xyz~
chxyz36r~
~whatif
abder~000082685
mnops~000083554
fulfil302~00026" | while read line ; do
        one=${line%%~*}
        two=${line##*~}
        printf "%30s %15s %15s \n" "$line" "$one" "$two"
done
exit 0
$ ./script
                          xyz~             xyz
                     chxyz36r~        chxyz36r
                       ~whatif                          whatif
               abder~000082685           abder       000082685
               mnops~000083554           mnops       000083554
               fulfil302~00026       fulfil302           00026
$
  #3 (permalink)  
Old 04-23-2007
kapilraj kapilraj is offline
Registered User
  
 

Join Date: Dec 2006
Location: Maryland
Posts: 162
awk -F "~" '{print $2}'

If you don;t want empty lines ,

awk -F "~" '{print $2}' | grep -v ^$
  #4 (permalink)  
Old 04-23-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,535
Code:
awk -F "~" '$2 !~ /^$/ {print $2 }' "file"
  #5 (permalink)  
Old 04-24-2007
napster_san napster_san is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 6
cat $filaname | sed 's/.*~\(.*\)/\1/'
  #6 (permalink)  
Old 04-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
cut -d"~" -f2 filename
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 08:17 PM.


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