Reputation: 99
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
Upvotes: 1
Views: 372
Reputation: 3089
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
Upvotes: 1
Reputation: 1517
First remove example and put it back in front of what is left.
awk '{sub(/example$/,"");print "example"$0}' file
example123
example321
example34292
Upvotes: 0
Reputation: 88654
With bash and a regex:
s="123example"
[[ $s =~ ([0-9]+)(.*) ]] && echo "${BASH_REMATCH[2]}${BASH_REMATCH[1]}"
Output:
example123
Upvotes: 2
Reputation: 133538
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.
Upvotes: 1
Reputation: 1947
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).
Upvotes: 5