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
Parse for errors shell script bubba112557 Shell Programming and Scripting 2 04-02-2009 11:25 AM
How to: Parse text string into variables using Korn shell shew01 Shell Programming and Scripting 7 05-23-2008 10:01 AM
Parse a string in XML file using shell script ayhanne Shell Programming and Scripting 46 01-09-2008 12:33 PM
split files by specifying a string (bash shell) vikas027 Shell Programming and Scripting 12 11-01-2007 01:57 PM
How to parse config variables from external file to shell script pradsh Shell Programming and Scripting 2 07-09-2007 02:21 PM

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 08-07-2008
yajaykumar yajaykumar is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 8
Shell script to parse/split input string and display the tokens

Hi,
How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant.

The input file could be as below:

one~two~three~four~five~six
11~12~13~14~15
AAA~BBB~CC~DD~EEEE~FFF~GGGGG

Output should be :

Line 1
one
two
three
four
five
six
Line 2
11
12
13
14
15
Line 3
AAA
BBB
CC
DD
EEEE
FF
GGGGG

Your help/hints are highly appreciated.

Thank you,
Ajay
  #2 (permalink)  
Old 08-07-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
try this....

try this
fnsonlq0-/home/fnsonlq0>cat filename|awk '{print "Line",NR,"~",$0}'|sed -e 's/~ /\
> /g' -e 's/~/\
> /g'
Line 1
one
two
three
four
five
six
Line 2
11
12
13
14
15
Line 3
AAA
BBB
CC
DD
EEEE
FFF
GGGGG

Last edited by vidyadhar85; 08-08-2008 at 12:48 AM..
  #3 (permalink)  
Old 08-08-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
Code:
awk '{gsub("~","\n");print "Line " NR;print}' file
Regards
  #4 (permalink)  
Old 08-08-2008
yajaykumar yajaykumar is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 8
Vidhyadhar85 and Franklin52,

Thanks a lot! Your suggestions/inputs have been of great help. They helped me in resolving my problem.

Hava a good one!
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 07:15 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