Parsing string using specific delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing string using specific delimiter
# 1  
Old 09-19-2008
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  
Old 09-19-2008
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  
Old 09-19-2008
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  
Old 09-19-2008
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  
Old 09-19-2008
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

# 6  
Old 09-19-2008
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

# 7  
Old 09-19-2008
tr can't replace one character with many; see the proposed sed solution above instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific column delimiter

Hi All, I have a file with a pipe delimiter. I need to replace the delimiter with html tags. I managed to get all the delimiters replaced along with first and last but the requirement is that I need to change 7th delimiter with slight change. File1: ... (2 Replies)
Discussion started by: shash
2 Replies

2. UNIX for Advanced & Expert Users

Inserting delimiter after a specific number of chars

Hello guys, I have a problem where I need to add a delimiter, that can be | for example, after each 28000 chars. The problem is that sometimes 1 row, which should contain 28000 chars is split in 2, so I want to put the delimiter after each 28000 so I will know the end of each row. Please... (2 Replies)
Discussion started by: Diogo R Jesus
2 Replies

3. Shell Programming and Scripting

Specific string parsing in Linux/UNIX

Hi, I have a string which can be completely unstructred. I am looking to parse out values within that String. Here is an example <Random Strings> String1=<some number a> String2=<some number b> String3=<some number c> Satish=<some number d> String4=<some number e> I only want to parse out... (1 Reply)
Discussion started by: satishrao
1 Replies

4. Shell Programming and Scripting

Adding String at specific postition of a delimiter

I have a file containing data lines: 1|14|CONSTANT||11111111||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|STA11ACT|ddts555S||||00001|rrttbbggcc| 1|15|CONSTANT||22222222||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|rrtha772|llkis000||||00001|AAEtbbggcc| I want to add a... (3 Replies)
Discussion started by: pparthiv
3 Replies

5. Shell Programming and Scripting

Html parsing - get line after specific string till a point

Hi all :) It sounds complex, for example I want to find the whole html file (there are 5 entries of this string and I need to get all of them) for the string "<td class="contentheading" width="100%">", get the next line from it only till the point that says "</td>", plus removing \t (tabs) ... (6 Replies)
Discussion started by: hakermania
6 Replies

6. Shell Programming and Scripting

parsing filename and grabbing specific string patterns

Hi guys...Wow I just composed a huge post and it got erased as I was logged out automatically Anyways I hope someone can help me out here. So the task I'm working on is like this I have a bunch of files that I care about sitting in a directory say $HOME/files Now my job is to go and loop... (6 Replies)
Discussion started by: rukasetsuna
6 Replies

7. Shell Programming and Scripting

How to remove delimiter from specific column?

I have 5 column in sample txt file where in i have to create report based upon 1,3 and 5 th column.. I have : in first and third coulmn. But I want to retain the colon of fifth coulmn and remove the colon of first column.. 5th column contains String message (for example,... (7 Replies)
Discussion started by: Shirisha
7 Replies

8. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

9. UNIX for Dummies Questions & Answers

Parsing using a Delimiter

Hi, i have a string in format of text1.text2.text3 . how do i parse it using . as delimiter . i understand this can be done by tr .However i am trying to assign these to different variables such as a=text1 b=text2 c=text3 etc? how can i do it (3 Replies)
Discussion started by: paritoshc
3 Replies

10. Shell Programming and Scripting

how to differentiate columns of a file in perl with no specific delimiter

Hi everybody, This time I am having one issue in perl. I have to create comma separated file using the following type of information. The problem is the columns do not have any specific delimiter. So while using split I am getting different value. Some where it is space(S) and some where it is... (9 Replies)
Discussion started by: Amiya Rath
9 Replies
Login or Register to Ask a Question