Sponsored Content
Top Forums Shell Programming and Scripting Need help in script; [ script to control a sentence & its words ] Post 302279295 by cfajohnson on Thursday 22nd of January 2009 10:49:57 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy