Sponsored Content
Top Forums Shell Programming and Scripting Trouble cutting characters into a string. Post 302339691 by Makaer on Friday 31st of July 2009 07:07:53 AM
Old 07-31-2009
Trouble cutting characters into a string.

I just have a couple of quick questions.

I am having trouble with this cut. I am basically trying to cut the string so that i can insert the users guess at the appropriate point in the string.
$letters is the character count of the $word.

What it seems to do is cut the character into the string but then extend the original mask instead of replacing it entirely.
Will i have to add an extra piece that allows me to cut from the first character in the string if that is where the guessed letter is?
Or is there a slightly easier bit of code that I am overlooking.

Thanks in advance.
P.S. I am very new to coding so it probably isnt the simplest way to do it, but I dont want to redo the whole function with syntax im not entirely sure of.

Code:
function replaceLetter()
{
for ((x=0;x<="$letters"; x++))
        do
wordLetter=$(echo $word | cut -c$x)


        if [ "$guess" = "$wordLetter" ]

        then
                newMask=$maskWord

                        maskWordTemp1=$(echo $newMask | cut -c-$x)
                        maskWordTemp2=$guess
                        maskWordTemp3=$(echo $newMask | cut -c$x-)

                maskWordTemp=$maskWordTemp1$maskWordTemp2$maskWordTemp3
                maskWord=$maskWordTemp

                echo "Correct $guess is in the word"

                correct=true
        fi
done

if [ ! correct ]
        then
        lives=$(($lives - 1))
        correct=false
fi
                alreadyGuessed+="$(echo $guess | sort -u)"

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting Up a String

I have a file name coming in as such <string>_YYYYMMDD.DAT The string could be anything. I want to cut out the date and put it in a variable. Can someone help me with this? (4 Replies)
Discussion started by: lesstjm
4 Replies

2. Shell Programming and Scripting

cutting part of string

Hi, I wanted to cut a specific portion from given string. How would I do that? Example: /u09/core/inbound/abc.txt is my string. I want abc.txt in a variable. Please help me. Regards, Dhaval (3 Replies)
Discussion started by: dhaval_khamar
3 Replies

3. AIX

CUT command - cutting characters from end of string

Hello, I need to delete the final few characters from a parameter leaving just the first few. However, the characters which need to remain will not always be a string of the same length. For instance, the parameter will be passed as BN_HSBC_NTRS/hub_mth_ifce.sf. I only need the bit before the... (2 Replies)
Discussion started by: JWilliams
2 Replies

4. Shell Programming and Scripting

Cutting last few characters of file name

Dear Friends, Here I have two queries. 1. Want to make folder named as last three characters of a file name (length of file name can vary file to file) E.g File name is AAAACCCCBBBB.txt (12 Characters excluding file extension) then folder to be created is BBB Irrespective of... (7 Replies)
Discussion started by: anushree.a
7 Replies

5. Shell Programming and Scripting

Cutting segment of a string

Hi, I am using bash. My question concerns cutting out segments of a string. Given the following filename: S2002254132542.L1A_MLAC.x.hdf I have been able to successfully separate the string at the periods (.): $ L1A_FILE=S2002254132542.L1A_MLAC.x.hdf $ BASE=$(echo $L1A_FILE | awk -F.... (5 Replies)
Discussion started by: msb65
5 Replies

6. Shell Programming and Scripting

Cutting characters in a variable

I have a variable var which contains "ABCDEFDH" Now I have to remove the first 4 characters that is "ABCD" so my variable should contain only "DEFH" plzz tell me how to do that . i am using bash shell (1 Reply)
Discussion started by: cynosure2009
1 Replies

7. Shell Programming and Scripting

Cutting the value from a string

Hi all I have a string variable ${WHERE_SQL1} where i want to cut the first value of a variable. Eg ${WHERE_SQL1} = 'Where a.id =.................' I the string to be 'a.id =.......' Thanks in advance (2 Replies)
Discussion started by: theeights
2 Replies

8. UNIX for Dummies Questions & Answers

Help with cutting a string

Hi All, I have a string in the following format "abcd | fghfh | qwer | ertete" I need to cut the values in the following format line1 = abcd | fghfh | qwer line2 = ertete Simply speaking a want to cut all the values before the last delimiter from the line and print it on one line and... (11 Replies)
Discussion started by: amit_kv1983
11 Replies

9. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

10. Shell Programming and Scripting

Trouble with simple cutting, joining

Made a file called assignment1.sh and this is what's inside - #!/bin/bash $ cut -c 2 sales.txt > regionalsales.txt $ cut -c 6 sales.txt > regionalsales.txt $ cut -c 1 territory.txt > terrsummary.txt $ cut -c 2 territory.txt > terrsummary.txt $ cut -c 3 territory.txt > terrsummary.txt $ sort... (1 Reply)
Discussion started by: htzdruk
1 Replies
cut(1)							      General Commands Manual							    cut(1)

Name
       cut - cut out selected fields of each line of a file

Syntax
       cut -clist [file1 file2...]
       cut -flist [-dchar] [-s] [file1 file2...]

Description
       Use  the  command to cut out columns from a table or fields from each line of a file.  The fields as specified by list can be fixed length,
       that is, character positions as on a punched card (-c option), or the length can vary from line to line and be marked with a  field  delim-
       iter character like tab (-f option).  The command can be used as a filter.  If no files are given, the standard input is used.

       Use to make horizontal ``cuts'' (by context) through a file, or to put files together in columns.  To reorder columns in a table, use and

Options
       list	   Specifies  ranges  that must be a comma-separated list of integer field numbers in increasing order.  With optional - indicates
		   ranges as in the -o option of nroff/troff for page ranges; for example, 1,4,7; 1-3,8; -5,10 (short for 1-5,10);  or	3-  (short
		   for third through last field).

       -clist	   Specifies character positions to be cut out.  For example, -c1-72 would pass the first 72 characters of each line.

       -flist	   Specifies  the  fields  to be cut out.  For example, -f1,7 copies the first and seventh field only.	Lines with no field delim-
		   iters are passed through intact (useful for table subheadings), unless -s is specified.

       -dchar	   Uses the specified character as the field delimiter.  Default is tab.  Space or other characters with special  meaning  to  the
		   shell must be quoted.  The -d option is used only in combination with the -f option, according to XPG3 and SVID2/SVID3.

       -s	   Suppresses  lines  with  no	delimiter  characters.	 Unless  specified, lines with no delimiters are passed through untouched.
		   Either the -c or -f option must be specified.

Examples
       Mapping of user IDs to names:
       cut -d: -f1,5 /etc/passwd
       To set name to the current login name for the csh shell:
       set name=`who am i | cut -f1 -d" "`
       To set name to the current login name for the sh, sh5, and ksh shells:
       name=`who am i | cut -f1 -d" "`

Diagnostics
       "line too long"	   A line can have no more than 511 characters or fields.

       "bad list for c/f option"
			   Missing -c or -f option or incorrectly specified list.  No error occurs if a line has fewer fields than the list  calls
			   for.

       "no fields"	   The list is empty.

See Also
       grep(1), paste(1)

																	    cut(1)
All times are GMT -4. The time now is 08:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy