CSV | Text manipulation
Hi,
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 Code:
ID, Amount, Old code Code:
ID, Amount, Old code, New code |
sed 's/, N\([A-Z]\+\)/, 01\101/; s/, C\([A-Z]\+\)/, 02\101/; s/, L\([A-Z]\+\)/, 03\101/;'
|
This is for GNU Awk (otherwise you should escape the new lines in the ternary operator):
Code:
awk>new.csv -F', *' 'BEGIN { |
Quote:
I tried... Code:
sed 's/, N\([A-Z]\+\)/, 01\101/; s/, C\([A-Z]\+\)/, 02\101/; s/, L\([A-Z]\+\)/, 03\101/;' input.csv > output.csv Thanks |
It should work like that. What exactly does not work?
|
if you have Python, here's an alternative
Code:
for line in open("file"): |
Quote:
Code:
sed: -e expression #1, char 91: extra characters after command |
That is slightly surprising, since the expression is less than 91 characters long. Are you sure that your placement of quotation marks is correct?
|
Quote:
[HTML]sed 's/, N\([A-Z]\+\)/, 01\101/; s/, C\([A-Z]\+\)/, 02\101/; s/, L\([A-Z]\+\)/, 03\101/;' '/home/ll/Desktop/Extracts/Result/Prime/in.csv' > out.csv[/HTML] Not sure what you mean about the placement of quotation marks, I just copied and pasted your command. Any ideas? Thanks for helping... |
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.
|
Quote:
Thanks for your help though. |
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 Code:
# python script.py |
And yet another way:
Code:
$ cat data |
ghostdog74 wrote:
Quote:
|
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!
I think this was the point being made. |
All times are GMT -5. The time now is 06:52 AM. |