ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This forum is awesome, has helped me accomplish some data manipulation things really quickly!
I have another question now, not sure if gawk can be used for this or not....
I need to convert an alpha code in a csv file to an alpha-numeric code based on the following criteria:-
Code:
If field starts with N then new code starts with 01
If field starts with C then new code starts with 02
If field starts with L then new code starts with 03
THEN
If the conditions above are true then take three characters after the first character of the original code and add this to the 01,02 or 03 above.
THEN
Add 01 on the end.
If the field starts with something other than N, C or L then leave the field intact.
An example:
NBUP would become 01BUP01
CBUP would become 02BUP01
NWAR would become 01WAR01
012322 would become 012322
The format of the original csv file would be as follows and would require an additional field for the new codes:-
for line in open("file"):
line=line.strip() #strip new line
code=line.split(",")[2].strip() #get the code
if code.startswith("N"):
newcode="01"+code[1:]+"01"
print line+", "+newcode
elif code.startswith("C"):
newcode="02"+code[1:]+"01"
print line+", "+newcode
elif code.startswith("L"):
newcode="03"+code[1:]+"01"
print line+", "+newcode
else:
newcode=""
print line
No, sorry, I don't see what might be going wrong. It works for me (TM). But since the sed one-liner isn't exactly what you wanted anyhow, you would probably be better off using one of the other suggestions.
No, sorry, I don't see what might be going wrong. It works for me (TM). But since the sed one-liner isn't exactly what you wanted anyhow, you would probably be better off using one of the other suggestions.
I have no idea with Python, so will need to look into this.
sed is definitely not a suitable tool to do your stuff.
why didn't you try radoulov's awk solution? Doesn't it work?
If you want to try the Python piece, save the code as as script and on command prompt
Sed can do just about anything, but is very cumbersome to use for larger tasks. It was superb for its time, and still has plenty of value. But who the heck wants to fight with a basic two register machine!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.