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
how to differentiate columns of a file in perl with no specific delimiter Amiya Rath Shell Programming and Scripting 9 07-15-2008 12:51 AM
creating a delimiter string satish@123 Shell Programming and Scripting 0 05-21-2008 05:38 AM
Parsing string rolex.mp UNIX for Dummies Questions & Answers 3 02-20-2007 01:28 PM
String Parsing help jasjot31 UNIX for Dummies Questions & Answers 5 09-27-2006 05:38 AM
split string with multibyte delimiter azmathshaikh Shell Programming and Scripting 1 03-06-2005 06:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-19-2008
primp primp is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 19
Parsing string using specific delimiter

Hi,

I'm wondering what is the best way to parse out a long string that has a specific deliminator and outputting each token between the delim on a newline?

i.e. input
text1,text2,text3,tex4

i.e. output
text1
text2
text3
text4
  #2 (permalink)  
Old 09-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That's not really parsing, that's simple substitution.

Code:
tr , '\n' <input >output
If your tr doesn't grok '\n', try '\012' or a literal newline between single quotes. (Looks weird, but is valid and useful syntax in Bourne shell.)
  #3 (permalink)  
Old 09-19-2008
primp primp is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 19
Oh I can't believe I forgot about tr.

Although, this works, I can only get it to recognize a newline but can't combine with that a newline+few tabs?

I also have a string that in the form of:

variable = "data value"

I have an extended sed to remove all instances of the double quotes, is there a on liner that'll grab the right hand side of the "equal" sign and only output the datavalue without the quotes.

Thanks for your help.
  #4 (permalink)  
Old 09-19-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87
You can use cut / awk for this..

a='variable="data value"'

echo $a | cut -d'=' -f2 | sed 's/\"//g'

echo $a | awk -F= '{print $2}' | sed 's/\"//g'
  #5 (permalink)  
Old 09-19-2008
primp primp is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 19
This is in response to the original question, you said to use "tr" to add newline but I can't seem to get "tabs" to also be added?

So I get an output with a single tab, and the rest are not tabbed.

Script:

Code:
#!/bin/sh

A='text1,text2,text3'

echo -e "Output: \n\t${A}" | tr , '\n'
Script Output:

Code:
Output:
        text1
text2
text3
Desired Output:

Code:
Output:
        text1
        text2
        text3
  #6 (permalink)  
Old 09-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
This removes everything up to the first equals sign on every line:

Code:
sed -e 's/^[^=]*=//' file
This replaces a comma with a newline and two tabs. Again, there are sed variants which recognize \n and \t and others which don't (probably most don't understand \t) so consult your local sed manual page and/or experiment.

Code:
sed -e 's/,/\n\t\t/g' file
  #7 (permalink)  
Old 09-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
tr can't replace one character with many; see the proposed sed solution above instead.
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 11:47 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