5

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

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

4

Using sed:

cat file | sed -e 's/\(Game #[0-9]*\)/\11/'

2 Comments

UUOC.` sed -e 's/(Game #[0-9]*)/\11/' file`
Fair enough. Some habits die hard.
1
sed 's/ Game #\([0-9]*\):/ Game #1\1:/' yourfile.txt

3 Comments

doesn't work: sed 's/ Game #([0-9]*):/ Game #1\1:/' handHistories1.txt sed: -e expression #1, char 31: invalid reference \1 on `s' command's RHS
@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
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.
0

GNU awk

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.