LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-11-2009, 03:07 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 931

Rep: Reputation: 44
bash/shell - switch text ?


I have text like this:
Each part starts with line "case".
Below "case" are 3 sections:
1st - begin with @
2nd - begin with # - this one is not very necessary
3rd - begin with $
last line - begin with details

What I'd like to have is to switch all @-lines to 3rd section and all $-lines to 1st section (switch them)

here is real sample:
Code:
                case "3": ### - blablabla
@Text1 = "blalalal"
@Text2 = "bnothndfds"
@SomeText3 = "adsfdsfds S"
@WhatText = 1
@AgainText = 0
@NoText = @x + @S
                        # some
                        # unnecessary
                        # comments
                        $varible_1 = x
                        $some_other_varible_1 = x
                        details(something)                        
                case "3": ### - xzxzxzxzxzxz
@Text1 = "xxxblalalasl"
@Text2 = "rtybnothndfds"
@SomeText3 = "ereadsfdsfds S"
@WhatText = 1
@AgainText = 0
@NoText = @x + @S
                        # some
                        # unnecessary
                        # comments
                        # with more lines
                        $varible_1 = x
                        $some_other_varible_1 = x
                        $more_varibles = x
                        details(something)
What I need:
Code:
                case "3": ### - blablabla
                        # some
                        # unnecessary
                        # comments
                        $varible_1 = x
                        $some_other_varible_1 = x
@Text1 = "blalalal"
@Text2 = "bnothndfds"
@SomeText3 = "adsfdsfds S"
@WhatText = 1
@AgainText = 0
@NoText = @x + @S
                        details(something)                        
                case "3": ### - xzxzxzxzxzxz
                        # some
                        # unnecessary
                        # comments
                        # with more lines
                        $varible_1 = x
                        $some_other_varible_1 = x
                        $more_varibles = x
@Text1 = "xxxblalalasl"
@Text2 = "rtybnothndfds"
@SomeText3 = "ereadsfdsfds S"
@WhatText = 1
@AgainText = 0
@NoText = @x + @S
                        details(something)
Anyone know how to do this ?
 
Old 02-11-2009, 03:34 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe using awk:
Code:
/case/ {

    print
    
    while ( $1 !~ "details" ) {
      getline
      if ( $1 ~ /^@/ ) {
         count0++
         array0[count0] = $0
	 }
      else {
         count1++
         array1[count1] = $0
	 }
    }
    
    # print array1
    for ( i = 1; i <= count1-1; i++ )
        print array1[i]
	
    # print array0
    for ( i = 1; i <= count0; i++ )
        print array0[i]
	
    # print details
    print array1[count1]
	
    # delete arrays
    for ( i in array0 )
        delete array0[i]
    for ( i in array1 )
        delete array1[i]
	
    # reinitialize counters
    count0 = 0
    count1 = 0
    
}

Last edited by colucix; 02-11-2009 at 03:36 AM.
 
Old 02-11-2009, 05:51 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 931

Original Poster
Rep: Reputation: 44
Looks good
The only thing is that case lines are duplicated but it is a small problem.

Can I have a little explanation of what is going on in code ?
 
Old 02-11-2009, 07:14 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by czezz View Post
Looks good
The only thing is that case lines are duplicated but it is a small problem.
This is really strange. Looking at the code there is no way you can get the case line twice, unless you have two consecutive case lines in the original input.
Quote:
Can I have a little explanation of what is going on in code ?
Of course. The whole code starts every time it encounter a line containing the "case" pattern. First it simply print that line, then starts a while loop which ends when it encounters the "details" pattern. This is one of the assumptions: each section should always end with a "details" line.

Then it stores lines in two diffrent arrays. Array0 contains lines which begin with a "@", array1 stores all the rest:
Code:
if ( $1 ~ /^@/ ) {
    array0 is pouplated
    }
else {
    array1 is populated
    }
When the while loop ends, it prints the content of the arrays in revers order: first array1, then array0. All the lines in the array1 are printed except the last one (that one containing "details"), which in turn is printed after array0.

Finally it deletes the content of the arrays and reset the counters. When it encounters the next "case" line the code is run again.

If you have problems with the duplicated case, please attach the actual input file to your post, so I can try to process it on my machine and discover the bug. I don't remember if the ability to attach files is reserved to "Senior Members". Eventually you can e-mail me (you can find a link to send an e-mail in my LQ profile).
 
Old 02-11-2009, 08:36 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# awk '/^@/{ s=s $0 "\n";next}/details/{ print s; s=""}1' file
 
Old 02-11-2009, 10:26 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ghostdog74 View Post
Code:
# awk '/^@/{ s=s $0 "\n";next}/details/{ print s; s=""}1' file
ghostdog74 beats colucix 6-0!!! A really impressive one-liner!
 
  


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
Text is garbled when I switch from X back to text mode Dark Jirachi Linux - Laptop and Netbook 18 09-20-2007 09:41 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
how to switch form bash to another shell slim27616 SUSE / openSUSE 1 02-06-2005 12:34 PM
how change text (and background) color within the bash shell? Xavius Linux - Newbie 4 03-29-2004 02:21 PM

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

All times are GMT -5. The time now is 12:20 PM.

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