How to change last character in string with a variable value.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change last character in string with a variable value.
# 1  
Old 04-13-2011
How to change last character in string with a variable value.

Hey Guys,

I have text such as this.
Code:
28003,ALCORN,2
28009,BENTON,2
28013,CALHOUN,2
28017,CHICKASAW,2
47017,CARROLL,2
05021,CLAY,0


The last digit after the final "," is a variable value. This is the base file. I have to do further execution on this file later and I need to update the 3rd value at that time. So, How do I open this file, and append the last digit to my new variable, and also save as the same filename. I have tried multiple unsuccessful options.

An example of what I want is essentially the same output only with the new variable values appended. Using shell by the way.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 04-13-2011 at 02:36 PM.. Reason: code tags, please!
# 2  
Old 04-13-2011
Code:
awk -F, '{$3=$3"new value"}1' file

Change red "new value" with whatever you want to append there.
# 3  
Old 04-13-2011
Quote:
Originally Posted by bartus11
Code:
awk -F, '{$3=$3"new value"}1' file

Change red "new value" with whatever you want to append there.

Can it be a variable? Like $x
# 4  
Old 04-13-2011
Code:
awk -F, -vx=$x '{$3=$3""x}1' file

# 5  
Old 04-13-2011
Quote:
Originally Posted by bartus11
Code:
awk -F, -vx=$x '{$3=$3""x}1' file

OK. Does the x in -vx represent the variable x? Is the $x the only time the variable is referenced?

Also. How would I do this in a loop. The script has to parse through the file appending if necessary, and not appending if not necessary. Obviously I'll need a "for" loop and an "if" statement. Just not sure how to set it up. Do I need to set your code to a variable itself?

Something such as
for.......
if argument is true
#append the third column in the file on this line only
else
#do nothing go to next line.
fi
done
# 6  
Old 04-13-2011
-vx <- this "x" is awk's variable, while $x is shell's variable. You don't need shell's loop to go through all the lines. It can be done in awk. Just let me know what is the condition for appending.
# 7  
Old 04-13-2011
There's so many way to scripting but I am not really sure if it is worth it.,,if ever that would be working if i use all the shell scripts in the same time..
My friend sharing me to all his information bout shell script but does not working don't know why i think it must be at the right shell programming before to function.. guys do you have any information regarding in this case if possible

Thanks for considerateSmilie)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help building a variable string from a keyword - character replacements!

Hello scripting geniusii! I come to kneel before the alter of your wisdom! I am looking to take a keyword and replace characters within that keyword and add them to a string variable. I would like this to only go through however many characters the word has, which may vary in size. ... (10 Replies)
Discussion started by: ghaniba
10 Replies

2. Shell Programming and Scripting

Bash - Inserting non printable character(s) in string variable

Hello. I have a string variable named L_TEMP to test a very simple filter. L_TEMP="50AwL.|KWp9jk" I want to insert a non printable character between K and W. I have try this : linux-g65k:~ # a='50AwL.|K' linux-g65k:~ # b='Wp9jk' linux-g65k:~ # L_TEMP="$a$'\x07'$b" linux-g65k:~ # echo... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

Assign a variable the nth character of a string.

I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script. $1 will be "pia" I need to assign the first character to stmttype. (10 Replies)
Discussion started by: klarue
10 Replies

4. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

5. Shell Programming and Scripting

awk: change string variable

Hi. How to change string variable in awk? for example, I parse with awk script text file named some_name_with_extension.txt I want to print only some_name in my script .... varCompName = FILENAME print varCompName How to put not all symbols from FILENAME to variable? thank you This... (4 Replies)
Discussion started by: cintlt
4 Replies

6. Shell Programming and Scripting

Delete first character from a string stored in a variable

Hallo! Example. #!/bin/bash BACKUP_DIR=/home/userx/backups/evolution echo $BACKUP_DIR # delete the first character from the string BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-) echo $BACKUP_DIR It works. It does want I want, delete the first character from string in the... (11 Replies)
Discussion started by: linuxinho
11 Replies

7. Shell Programming and Scripting

Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done.. But I could not even do following simple task. How do I Change existing variable value only when user enteres non-empty string. ? #!/usr/bin/ksh uid="scott" # Assign new value user enters to uid, else leave it... (7 Replies)
Discussion started by: kchinnam
7 Replies

8. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

9. Shell Programming and Scripting

read and assign each character from the string to a variable

How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you! (1 Reply)
Discussion started by: Tek-E
1 Replies

10. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies
Login or Register to Ask a Question