LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   perl one liner to delete line in csv file if one columns is toobig (https://www.linuxquestions.org/questions/linux-newbie-8/perl-one-liner-to-delete-line-in-csv-file-if-one-columns-is-toobig-4175531062/)

casperdaghost 01-14-2015 08:07 PM

perl one liner to delete line in csv file if one columns is toobig
 
I have a little bash script that cats out a file and tells me if there is a line
where the 11th column has more than 6 characters in it.
It emails me where there is a bad line in a file - bead meaning that it will break a
donwstream process.

anyhow when i get the email saying that there is a bad file i just log in to the pc via
vpn and the I sed out the lines from the file that I get in the email. The bad lines are
always in danny.csv not danny1.csv
It has been the same lines killing the downstream process for a few weeks, so i put the "sed -i's" into
the script and it does it automagically.

[CODE]
for i in danny.csv danny1.csv
do
cat /come/and/play/with/$i | perl -ne 'print if length((split /,/)[10]) > 6' | mail -s "danny.csv bad line" casper@casperr.com
done

#it would be nice to find a perl change the file in place
sed -i '/D,642,0642,UBF,EVL,,M,,S,S,FOREVER,213,213,/d' /come/and/play/with/us/danny.csv
sed -i '/D,642,0642,UBF,EVL,,M,,S,S,QSP-U=C,4,4,/d' /come/and/play/with/us/danny.csv
[CODE]

However when a new line gets put into this file, I am going to have to log in and take out the line.
SO I have been trying to write a perl one liner that will edit the file in place, like sed, and make a
backup of the file. I just need a perl one liner that will delete any line where the 11th columns has more
than 6 characters in it.
[CODE]
perl -p -i.bak -e 's/\,\w{7}\,//g - which does not work.
[CODE]
I tried something like this:

[CODE]
perl -nle 'print if /\,\w{7}\,/' /come/and/play/with/us/danny.csv
[CODE]
but that does not catch the QSP-U=C and it catches more lines than just the
FOREVER. for a solutinog I need to focus on the the 11th column.

pan64 01-15-2015 12:47 AM

I do not really understand it, but first please use [code] and [/code] instead of [CODE].
you have already a working sed, why do you want rewrite it perl?
in your perl script you ought to use the same regexp (as in your sed).

jpollard 01-15-2015 07:38 AM

First, sed doesn't do in-place edits either. It uses a workfile, then at the end, it deletes the original file and renames the workfile.

You can do the same thing within perl, or use an mv to move the output to the same name as the original file (this is easier for perl one liner commands).


All times are GMT -5. The time now is 02:53 AM.