SED or AWK to move character to end of line

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ericramos1990

SED or AWK to move character to end of line

Post by ericramos1990 »

Hello dudes!

Anyone know how to go about this:

Code: Select all

Hell=o
2the=re
m3y
n2==ame
4is
Er==ic
to this:

Code: Select all

Hello=
there2=
my3
name2==
is4
Eric==
I would like to move any numbers "[0-9]" then "=" to the end of the line.

Multiple commands would be fine, as long as it works. Thanks guys!

First person with answer that works, I'll make a lyrics video to the song of your choice :)
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Jamesc359

Re: SED or AWK to move character to end of line

Post by Jamesc359 »

Neither, I'd use Python. Admittedly I don't care for SED or AWK...

Save this to a file, make it executable (chmod +x <filename>) and run it like so; scriptname < infile.txt > outfile.txt

Code: Select all

#!/usr/bin/env python

from sys import stdout, stdin, stderr

data = stdin.read()

numbers = [chr(n) for n in range(48, 58)]
endofline = ["="]

for line in data.split("\n"):
	a = ''
	n = ''
	eol = ''
	for char in line:
		if char in numbers:
			n += char
		elif char in endofline:
			eol += char
		else:
			a += char
			
	stdout.write(a+n+eol+"\n")
ericramos1990

Re: SED or AWK to move character to end of line

Post by ericramos1990 »

Hey Jamesc359, that work flawlessly!!

May I ask why you don't care for SED or AWK?

I had no idea you can do such thing with Python, I'm pretty impressed.

Would you mind adjusting the code so it will also move hyphens to the end of the line as well?

ex.
Word3-=

Also, I said I would make a lyric video in return, so what song do you want? :)
Jamesc359

Re: SED or AWK to move character to end of line

Post by Jamesc359 »

I never cared for SED or AWK because I'm too lazy to learn them. :lol:

Python is a very powerful language that's capable of doing almost anything imaginable.

Did you want the hyphens to come in order? e.g. W-ord=-3 would become Word3-=- or would you prefer it to be Word3--= ?

This will spit them out as they come. e.g. Word3-=-

Code: Select all

#!/usr/bin/env python

from sys import stdout, stdin, stderr

data = stdin.read()

numbers = [chr(n) for n in range(48, 58)]
endofline = ["=", "-"]

for line in data.split("\n"):
	a = ''
	n = ''
	eol = ''
	for char in line:
		if char in numbers:
			n += char
		elif char in endofline:
			eol += char
		else:
			a += char
			
stdout.write(a+n+eol+"\n")
This will place them in a specific order. e.g. Word3--=
To change the order all you have to do is change the order of the endofline list. e.g. endofline = ["=", "-"] would result in Word3=--

Code: Select all

#!/usr/bin/env python

from sys import stdout, stdin, stderr

data = stdin.read()

numbers = [chr(n) for n in range(48, 58)]
endofline = ["-", "="]

for line in data.split("\n"):
	a = ''
	n = ''
	eol = ['' for e in endofline]
	for char in line:
		if char in numbers:
			n += char
		elif char in endofline:
			eol[endofline.index(char)] += char
		else:
			a += char
	
	e = "".join(eol)
	
	stdout.write(a+n+e+"\n")
ericramos1990

Re: SED or AWK to move character to end of line

Post by ericramos1990 »

Jamesc359, that works just great!

I used the 2nd option, thanks for giving me choices!

I wasn't able to find a way to make python edit files in place, so I did a simple workaround using mv.
Please let me know if there's a formal way to edit files in place with python! :D

Code: Select all

relocatescript < words.txt > adjustedwords.txt
mv adjustedwords.txt words.txt
Also I noticed you didn't request a song, it won't take me much time to do, here is an example:
https://www.youtube.com/watch?v=-8Ohdxv ... 3NF88Z7MBA

And it's ok if you are not interested, I just wanted to return the favor somehow for the help 8)
Jamesc359

Re: SED or AWK to move character to end of line

Post by Jamesc359 »

ericramos1990 wrote:Jamesc359, that works just great!

I used the 2nd option, thanks for giving me choices!
Your Welcome. :)
ericramos1990 wrote:I wasn't able to find a way to make python edit files in place, so I did a simple workaround using mv.
Please let me know if there's a formal way to edit files in place with python! :D

Code: Select all

relocatescript < words.txt > adjustedwords.txt
mv adjustedwords.txt words.txt
This would require a small tweak in the code, but it's certainly doable. You can change PROMPT_FOR_OVERWRITE = False to disable the overwrite prompt.

Code: Select all

#!/usr/bin/env python

from sys import argv
from os.path import isfile, basename

PROMPT_FOR_OVERWRITE = True

try: # This is for Python 2.x compatibility.
	input = raw_input
except:
	pass

if len(argv) < 2:
	print("Usage: %s <infile> [outfile]"%(basename(argv[0])))
	exit(1)

if isfile(argv[1]):
	data = open(argv[1], "r").read()
	if not data:
		print("Error: unable to get contents of %s"%(basename(argv[1])))
		exit(1)
else:
	print("Error: file %s doesn't exist."%(basename(argv[1])))
	exit(1)

if len(argv) == 2:
	argv.extend([argv[1]])

if isfile(argv[2]) and PROMPT_FOR_OVERWRITE:
	s = "Overwrite: %s already exists, overwrite? (y,n) "
	i = input(s%(argv[2]))
	if i[0].lower() != "y":
		exit(0)
		
output = open(argv[2], "w")
if not output:
	print("Unable to open file %s"%(basename(argv[2])))
	exit(1)

numbers = [chr(n) for n in range(48, 58)]
endofline = ["-", "="]

for line in data.split("\n"):
	a = ''
	n = ''
	eol = ['' for e in endofline]
	for char in line:
		if char in numbers:
			n += char
		elif char in endofline:
			eol[endofline.index(char)] += char
		else:
			a += char
	
	e = "".join(eol)
	output.write(a+n+e+"\n")

output.close()
ericramos1990 wrote:Also I noticed you didn't request a song, it won't take me much time to do, here is an example:
https://www.youtube.com/watch?v=-8Ohdxv ... 3NF88Z7MBA

And it's ok if you are not interested, I just wanted to return the favor somehow for the help 8)
I appreciate the offer, however I don't really listen to a lot of music anymore. :)
Locked

Return to “Scripts & Bash”