1

I want to move the digits/numeric string from front of line to end of line, example of input:

123example
321example
34292example

expected output:

example123
example321
example34292
Rahul Verma
  • 2,946
  • 14
  • 27
Uni VPS
  • 99
  • 1
  • 9
  • 4
    You may use `sed -E 's/^([0-9]+)(.*)/\2\1/' file` – anubhava Mar 17 '18 at 11:05
  • 1
    What did you try for yourself? – Inian Mar 17 '18 at 11:09
  • @anubhava that puts the numbers onto a new line, so line 1 = example line 2 = 123 – Uni VPS Mar 17 '18 at 11:10
  • @Inian honestly I have no idea what to attempt, I'm new to bash. I know stuff just not sure how I would move digits from front to end.. I know how to remove digits etc just not move them to end of line – Uni VPS Mar 17 '18 at 11:11
  • @NoobLearning: Fair enough, but atleast you could lookup similar questions (plenty of them) and make an attempt. – Inian Mar 17 '18 at 11:12
  • @NoobLearning you should still add those attempts to question.. otherwise your question is not a fit for SO... see https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users .. also, the info pages for the tags, ex: https://stackoverflow.com/tags/awk/info have plenty of help for beginners – Sundeep Mar 17 '18 at 11:14
  • With anubhava's suggestion: `dos2unix file | sed -E 's/^([0-9]+)(.*)/\2\1/'` – Cyrus Mar 17 '18 at 11:34
  • Or with any POSIXly `sed` - `dos2unix file | sed 's/^\([0-9]*\)\(.*\)/\2\1/'` – Inian Mar 17 '18 at 11:38

5 Answers5

5

GNU awk's match function can do the job:

gawk 'match($0, /^([0-9]+)(.*)$/, m) {print m[2] m[1]}' yourfile.txt

but, honestly, I would use sed for this task (as @anubhava suggested).

Flopp
  • 1,887
  • 14
  • 24
  • it seems to print digits on a new line rather than at the end of the string, example: Line 1 = example Line 2 =123 Thanks for your help, appreciate the answers :) – Uni VPS Mar 17 '18 at 11:26
  • 1
    Did you create the input file on Windows? There may be an issue with the line-endings (see https://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-a-bash-script) – Flopp Mar 17 '18 at 11:29
  • + 1 for your comment, that's very useful information for me in the future & that was also the issue that fixed this :). – Uni VPS Mar 17 '18 at 11:35
2

With bash and a regex:

s="123example"
[[ $s =~ ([0-9]+)(.*) ]] && echo "${BASH_REMATCH[2]}${BASH_REMATCH[1]}"

Output:

example123
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

GNU awk

awk -F"[[:digit:]]+" '{split($0,f,FS,s); print $2 s[1]}' file
example123
example321
example34292

You can use stream of digits as FS. Split the string using FS and the characters matching the separators will be stored in array s. Use it as you like

Rahul Verma
  • 2,946
  • 14
  • 27
1

Following awk may help you without using array in match:

awk '{gsub(/\r/,"");match($0,/^[0-9]+/);print substr($0,RSTART+RLENGTH+1) substr($0,RSTART,RLENGTH)}' Input_file

Also added now gsub(/\r/,"") for removing all carriage returns from your Input_file too.

Also in case you want to save output into your Input_file itself then append following to above code > temp_file && mv temp_file Input_file

Explanation: Adding explanation too here now.

awk '
{
  gsub(/\r/,"")                                              ##Removing all carriage returns from current line here by using gsub out of box function of awk.
  match($0,/^[0-9]+/);                                       ##Using match function of awk to match starting digits in current line.
  print substr($0,RSTART+RLENGTH+1) substr($0,RSTART,RLENGTH)##Printing substrings here 1st substring is to print from the value of RSTART+RLENGTH+1 to till the end of the line and second substring is to print from the RSTART to RLENGTH where RSTART and RLENGTH are the variables of awk which will be SET once match is having a TRUE value in it. RSTART denotes the index value of matched regex in line/variable and RLENGTH is the length of the matched regex by match.
}
' Input_file                                                ##Mentioning the Input_file name here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

First remove example and put it back in front of what is left.

awk '{sub(/example$/,"");print "example"$0}' file

example123
example321
example34292
Claes Wikner
  • 1,457
  • 1
  • 9
  • 8