Shell script to parse/split input string and display the tokens | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Shell script to parse/split input string and display the tokens

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-07-2008
yajaykumar yajaykumar is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 27 July 2009, 2:44 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 08-07-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 107 Times in 103 Posts
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..
Sponsored Links
    #3  
Old 08-08-2008
Franklin52's Avatar
Franklin52 Franklin52 is online now Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 16 April 2014, 3:13 AM EDT
Location: The Netherlands
Posts: 7,667
Thanks: 116
Thanked 525 Times in 496 Posts

Code:
awk '{gsub("~","\n");print "Line " NR;print}' file

Regards
    #4  
Old 08-08-2008
yajaykumar yajaykumar is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 27 July 2009, 2:44 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing string as a input parameter for a shell script vidhyaS Shell Programming and Scripting 12 07-02-2011 05:01 AM
Need tokens in shell script AB10 Shell Programming and Scripting 3 10-01-2010 06:48 AM
SPLIT STRING in bash shell script karthinvk Shell Programming and Scripting 7 03-16-2010 03:27 PM
Shell Script to replace tokens in multiple files cbo0485 Shell Programming and Scripting 20 11-20-2008 06:52 AM
Parse a string in XML file using shell script ayhanne Shell Programming and Scripting 46 01-09-2008 11:33 AM



All times are GMT -4. The time now is 03:14 AM.