Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 08-07-2008
vidyadhar85's Unix or Linux Image
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 17 April 2015, 12:45 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 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 Unix and Linux 08-08-2008
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 July 2015, 1:09 PM EDT
Location: The Netherlands
Posts: 7,731
Thanks: 129
Thanked 547 Times in 515 Posts

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

Regards
    #4  
Old Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 08:26 PM.