views:

98

answers:

4

Hi,

I have a multi line text file where each line has the format

..... Game #29832: ......

I want to append the character '1' to each number on each line (which is different on every line), does anyone know of a way to do this from the command line?

Thanks

+1  A: 
sed 's/ Game #\([0-9]*\):/ Game #1\1:/' yourfile.txt
Nathan Kidd
doesn't work:sed 's/ Game #([0-9]*):/ Game #1\1:/' handHistories1.txtsed: -e expression #1, char 31: invalid reference \1 on `s' command's RHS
Aly
@Aly, that works with gnu sed. Depending on your sed version drop the backslash before the ( and ) or see if you have arguments to your sed version to enable that syntax
Nathan Kidd
@Nathan: you had formatted your command as a quote, so the backslashes got eaten up. I made it format like code. The OP wants to *append* '1', by the way.
Alok
@Alok: Thanks, I didn't notice quote formatting eats things.
Nathan Kidd
+3  A: 

Using sed:

cat file | sed -e 's/\(Game #[0-9]*\)/\11/'
anshul
UUOC.` sed -e 's/\(Game #[0-9]*\)/\11/' file`
ghostdog74
Fair enough. Some habits die hard.
anshul
+2  A: 
sed -i -e 's/Game #[0-9]*/&1/' file

-i is for in-place editing, and & means whatever matched from the pattern. If you don't want to overwrite the file, omit the -i flag.

Alok
A: 

GNU awk

awk '{b=gensub(/(Game #[0-9]+)/ ,"\\11","g",$0); print b }' file
ghostdog74