0

I need to remove all the contents that appear in a column after a specific character in CSV file. This is the format it is currently in:

2020-01-01T12:00:00,Transaction Description,Initiator,$00.00

I want to remove the contents in the first column that appear after T, so that it looks like:

2020-01-01,Transaction Description,Initiator,$00.00

I tried sed but it didn't work as expected.

2

Using standard sed in a similar way as Timur uses Perl:

$ sed 's/T[^,]*//' file
2020-01-01,Transaction Description,Initiator,$00.00

This removes all text from the first T on each line, up to (but not including) the first comma, by means of a substitution.

Redirect to a new file to save the result.

| improve this answer | |
0

Use this Perl one-liner:

perl -pe 's{T[^,]*}{}' input_file > output_file

Here,
-e command line flag tells Perl to use the code that follows in-line, rather than look for code in the specified script,
-p tells Perl to loop over input one line at a time, printing it after the specified code in executed,
s{T[^,]*}{} replaces T followed by any characters other than , zero or more times (* specifies greedy, or longest, match). This "eats up" the characters starting with the first T, inclusive, and ending with the first following comma, exclusive.

| improve this answer | |
0

Method1:

awk -F "," 'OFS=","{gsub(/T.*/,"",$1);print $0}' filename

output

2020-01-01,Transaction Description,Initiator,$00.00

command2

perl -pne "s/,/\n/g" filenmame| sed '/.T/s/T.*//g'|perl -pne "s/\n/,/g"| sed "s/,$//g"

output

2020-01-01,Transaction Description,Initiator,$00.00

method3

#!/usr/bin/python
import re
p=re.compile(r'T.*')
k=open('p.txt','r')
for i in k:
    l=i.split(",")
    z=re.sub(p,"",l[0])
    rt=",".join(l[1:])
    er=",{0}".format(rt)
    print z+er,

output

2020-01-01,Transaction Description,Initiator,$00.00
| improve this answer | |
  • 1
    awk -F "," 'OFS=","{gsub(/T.*/,"",$1);print $0}' filename makes no sense and I highly doubt that perl | sed | perl | sed is ever a reasonable solution to any problem. – Ed Morton Apr 4 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.