Discussion Locked

Handling CSV file in UNIX

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have a csv file in unix which has 140 columns (Comma separated). I want to find 'newline character' and replace with null, only in the 137th column in the whole file. Can somebody help ?

3 replies

by gary_w

Is the newline the first newline character in the row? If so, try this in good ole vi:

:%s/^M//

: = command mode
% = run the command on all lines
s/ = Search for
ctrl&V, enter = pattern (newline control character)
// = replace with nothing, 1st occurance on a line only. To replace all occurances use //g (g=global meaning all occurances of the pattern on the line).

Gary

by ghislain.levesque

cat file_in|while read line
do
if [ "`echo$line|cut -c 137`" = "\^M" ]
then echo "`echo $line|cut -c1,136`" "`echo $line|cut -c138-`" >> new_file_in
else
echo $line >> new_file_in
fi
done

This discussion is closed to new replies.