General discussion

Locked

Handling CSV file in UNIX

By amit.sethia ·
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 ?

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Gary_W In reply to Handling CSV file in UNIX

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

Collapse -

by amit.sethia In reply to Handling CSV file in UNIX

New line character is a line break in the 137th column.

Collapse -

by ghislain.levesque In reply to Handling CSV file in UNIX

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

Back to Linux Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums