LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2009, 12:55 AM   #1
AutoC
Member
 
Registered: Jan 2008
Posts: 30

Rep: Reputation: 15
need help parsing


Hi,
I have a file with entries as follows..
Quote:
|
|
point [ p oy n t ]
pointed [ p oy n ] [ t ah d ]
|
|
I need to rewrite them as

Quote:
|
|
point p_oy_n_t
pointed p_oy_n t_ah_d
|
|
what can I do for this?
any help is appreciated
 
Old 08-03-2009, 12:58 AM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
You need to be a little more general, that looks really specific. How do you know what spaces get replaced by underscores and what spaces stay as they were? syllables?

Can you generate a longer example?
 
Old 08-03-2009, 02:47 AM   #3
AutoC
Member
 
Registered: Jan 2008
Posts: 30

Original Poster
Rep: Reputation: 15
1. the word remains as it is.
2. each set of characters enclosed in braces are merged using underscore
eg. [ a bc d ] becomes a_bc_d
that is, the left brace and space between left brace and first character is eliminated.similarly, the right brace and space before it.
all spaces inbetween are made underscore.

3. one space between each of the groups enclosed by braces.
eg. [a bc d ] [e fg h] becomes a_bc_d e_fg_h


here's a long example

according [ ah ] [ k ao r ] [ d ih ] [ ng ]
has to become
according ah(one space)k_ao_r(one space)d_ih(one space)ng

Last edited by AutoC; 08-03-2009 at 02:51 AM.
 
Old 08-03-2009, 03:39 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -F"]" '/\[/{
    for(i=1;i<=NF;i++){
        match($i,/\[/)
        o=substr($i,1,RSTART)
        s=substr($i,RSTART+1)
        gsub(/ +/,"_",s)
        gsub(/^_|_$/," ",s)
        $i = o s       
    }
}1' OFS="]" file
output
Code:
# more file
|
|
point [ p oy n t ]
pointed [ p oy n ] [ t ah d ]
|
|
# ./test.sh
|
|
point [ p_oy_n_t ]
pointed [ p_oy_n ] [ t_ah_d ]
|
|
 
Old 08-03-2009, 03:54 AM   #5
AutoC
Member
 
Registered: Jan 2008
Posts: 30

Original Poster
Rep: Reputation: 15
awesome.thanks
 
Old 08-03-2009, 05:48 PM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Using GNU sed
Code:
echo '
point [ p oy n t ]
pointed [ p oy n ] [ t ah d ]
according [ ah ] [ k ao r ] [ d ih ] [ ng ]' |

sed -r ':a s/(\[[^]]*) /\1_/; ta; s/\[_|_\]//g'

point p_oy_n_t
pointed p_oy_n t_ah_d
according ah k_ao_r d_ih ng
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing String. msgforsunil Linux - Newbie 8 06-11-2009 01:47 PM
parsing data - better way of doing that kshkid Programming 10 01-08-2007 06:05 AM
Parsing C Strings anamericanjoe Programming 2 12-12-2006 12:27 AM
Parsing with Vim mijohnst Linux - General 2 06-18-2004 09:38 AM
Parsing. liguorir Programming 2 09-04-2003 04:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration