The UNIX and Linux Forums  

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
Script for pulling words of 4 to 7 characters from a file Azeus Shell Programming and Scripting 2 12-07-2008 02:56 PM
script to convert words into links kazordoon Shell Programming and Scripting 9 08-03-2008 12:33 PM
Script to find all the files that contain any of the words present in another file tsanthosh Shell Programming and Scripting 4 05-21-2008 03:29 AM
Bash script pass sentence in block katrvu Shell Programming and Scripting 6 02-11-2008 11:04 AM
how to find capital letter names in a file without finding words at start of sentence kev269 Shell Programming and Scripting 1 04-10-2006 10:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-22-2009
frozensmilz's Avatar
frozensmilz frozensmilz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 28
Need help in script; [ script to control a sentence & its words ]

Hello friends,

I am looking for any sed/awk/python script that can identify the position of a character or word in a file. Well, I prefer sed. <space> is a tab space since I actually dont know how to make the forum editor display a space as such.

Sample text
-----------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222 555
<space><space><space><space>XYZ 123
<space><space><space><space>BAA BAA

I would like to manipulate the above two sentences as following.

Result 1 [ backspace 1 (value taken from a variable) ]
--------------------------------------------------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222 555
<space><space><space>XYZ 123
<space><space><space><space>BAA BAA

Result 2 [ space 1 (value taken from variable) ]
---------------------------------------------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222 555
<space><space><space><space><space>XYZ 123
<space><space><space><space>BAA BAA

Result 3 [ copy XYZ from line 2 to line 1 beginning ]
------------------------------------------------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222 555 XYZ
<space><space><space><space>123
<space><space><space><space>BAA BAA

Result 4 [ copy 555 from line 1 to line 2 beginning ]
------------------------------------------------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222
<space><space><space>555 XYZ 123
<space><space><space><space>BAA BAA

Kindly help me or give me a start to achieve this script. Thanks in advance.
  #2 (permalink)  
Old 01-22-2009
manas_ranjan's Avatar
manas_ranjan manas_ranjan is offline
Registered User
  
 

Join Date: Jul 2007
Location: Amsterdam
Posts: 177
couldn't able to understand your variable and space riddle...
  #3 (permalink)  
Old 01-22-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by frozensmilz View Post
I am looking for any sed/awk/python script that can identify the position of a character or word in a file.

That's easy:


Code:
FILE=${1?You must have a filename}
STR=${2?You need string to index}

_index() #@ Store position of $2 in $1 in $_INDEX
{
    case $1 in
        "")  _INDEX=0; return 1 ;;
        *"$2"*) ## extract up to beginning of the matching portion
              idx=${1%%"$2"*}
              ## the starting position is one more than the length
              _INDEX=$(( ${#idx} + 1 )) ;;
        *) _INDEX=0; return 1 ;;
    esac
}

file=$( cat "$FILE" )

_index "$file" "$STR"

printf 'The position of "%s" is %d\n' "$STR" "$_INDEX"

Quote:
Well, I prefer sed.

Why sed? It is not much use as a programming language; awk can do much more and in a more legible manner.
Quote:
<space> is a tab space

What's a "tab space"? Do you mean a tab?
Quote:
since I actually dont know how to make the forum editor display a space as such.

Use a space. Put verbatim text inside [code] tags so that a monospace font is used in a <PRE> element.
Quote:

Sample text
-----------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222 555
<space><space><space><space>XYZ 123
<space><space><space><space>BAA BAA

That's:


Code:
    BAA BAA
   ABC 222 555
    XYZ  123
    BAA BAA

Or is it?
Quote:

I would like to manipulate the above two sentences as following.

Result 1 [ backspace 1 (value taken from a variable) ]
--------------------------------------------------
<space><space><space><space>BAA BAA
<space><space><space>ABC 222 555
<space><space><space>XYZ 123
<space><space><space><space>BAA BAA

What did you do to get that result? Describe the process.

Do the same for your other results.
  #4 (permalink)  
Old 01-22-2009
frozensmilz's Avatar
frozensmilz frozensmilz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 28
Need help in script; [ script/s to control a sentence & its words ]

Hello friends,

Thanks very much for the support. Sorry for the confusion.

Explaining the situation with a sample case

Input is a sample text containing a if block as follows

HTML Code:
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 

1             statements .......                             
2                          if ( condition 
3                          ) 
4                         { 
5                                           Statement 1;
6                              Statement 2;
7                                           Statement 3;
8            }
9  statements ..............
10

            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
I would like to do the following with the help of a script

1. Bring the Closing bracket of if statement from LINE 3 to to end of LINE 2
as follows

HTML Code:
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 

1             statements .......                             
2                             if ( condition )
3                                 
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
2. In LINE 7 move the closing brace of if block from column 10 to say column 20 as follows (updated line numbers since the last operation removed a line) such that the "if" & "{" starts from same column.

HTML Code:
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 

5                              Statement 2;
6                                           Statement 3;
7                          }
8  statements ..............
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
3. In LINE 3 move the opening brace of if block from say column 20 to end of LINE 2 as follows. (Note that the statements below should move upwards as the LINE 3 & 4 got removed.)

HTML Code:
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 

1             statements .......                             
2                          if ( condition ) {
3                                          Statement 1;

            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
4. From LINE 3 to LINE 7. The Statement 1 & Statement 3 should align according to position (say, column 23) of Statement 2 as follows. And Add a new line after Statement 3 & before Statement 1.

HTML Code:
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
3
4                              Statement 1;
5                              Statement 2;
6                              Statement 3;
7
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
After all the above operation the final if block should look like this (properly aligned & spaced)

HTML Code:
            10          20          30           40
1           *            *            *            *           50 Columns- just assume 

1             statements .......                             
2                          if ( condition ) { 
3
4                              Statement 1;
5                              Statement 2;
6                              Statement 3;
7
8                          }
9  statements ..............
10

            10          20          30           40
1           *            *            *            *           50 Columns- just assume 
I need a script that is just not meant for a if loop but for any situation which is why i used samples containing AAA, BAA.

<space> meant tab space, i am a beginner & just got comfortable with sed & awk...well, i am ready to have keys to the new doors..so please do provide any new or better methods to solve the problem.

Summary
If possible it will be nice if i can get a script, that can do the following. so that i can have a start.

1. finds the position of the first word in a line.
>for example the script should be able to tell me the position of A in A11 BBB CC1
2. finds the position of the last word in a line (calculating 1st character position of the first word of a line)
> for example the script should be able to tell me the position of '2' in B11 DDD ZZ2 (calculating last character position of the last word of a line)
3. A script that can move any word say B11 to a position after CC1 (line to line movement)
4. A script that can move any word say RRR such that the A11 & RRR should start from same column (column to column movement)
HTML Code:
Before
                     A11 BBB CC1
                           B11 DDD ZZ2
              RRR FFF

After
                     A11 BBB CC1 B11
                           DDD ZZ2
                     RRR FFF
Awaiting for support. Thanks

Last edited by frozensmilz; 01-22-2009 at 10:12 PM..
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 02:24 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