Need help in script; [ script to control a sentence & its words ]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in script; [ script to control a sentence & its words ]
# 1  
Old 01-22-2009
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.SmilieSmilie
# 2  
Old 01-22-2009
couldn't able to understand your variable and space riddle...Smilie
# 3  
Old 01-22-2009
Quote:
Originally Posted by frozensmilz
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  
Old 01-22-2009
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. Smilie

<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 SmilieSmilie

Last edited by frozensmilz; 01-22-2009 at 10:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

Print one sentence 40 to 50 words end with period in a file

Hi All, Is there another way to achieve this? how get short phrase in a sentence with character count of 100 to 155 words end with period but don't end something like 50,000. . Here's my current script but the output is not good. This will use for my snippets or preview. grep... (6 Replies)
Discussion started by: lxdorney
6 Replies

3. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

4. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

5. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

6. Shell Programming and Scripting

Perl Script to find & copy words from Web.

I need to write a perl script to search for a specific set of numbers that occur after a series of words but before another. Specifically, I need to locate the phrase today at the summit, then immediately prior to the words tonnes/day copy the number that will be between 100 and 9,999, for example,... (1 Reply)
Discussion started by: libertyforall
1 Replies

7. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

8. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

9. Shell Programming and Scripting

Bash script pass sentence in block

Hello, I want to know is it possible to pass a block of sentence using bash. For example, I have a script called Test.sh that takes in $1 and $2. and I'm calling Test.sh in a.sh so in a.sh Test.sh '' 'This is a sentence' Because block are separated by space so when I do that, I get... (6 Replies)
Discussion started by: katrvu
6 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question