Search code examples
linuxbashshellunixredhat

How to delete \n from the end of some particular lines in file [shell]?


In Redhat I have file.csv that has below data:

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free 
text 3"
170033101;20170302;;;"Free text 4"

I want to create another corrected file (Correct_file.csv) after removing the wrong \n from the file to be as below:

170033101;20170302;;;"Free text 1"
170033101;20170302;;;"Free text 2"
170033101;20170302;;;"Free text 3"
170033101;20170302;;;"Free text 4"

My solution:

I made the below shell script to find rows preceding those lines that don't start with 170, and then create sed.txt that has a sed line for each wrong row to replace the \n with space.

I am unable to perform the sed command or tr command to remove a specific line based on the line number

My Script:

>sed.txt;
for i in `grep -nv '^[1706]' $1|cut -f 1 -d \:`
do
if [ $i -eq 1 ]
then
continue
else
j=`expr $i - 1`
echo $j"s/\n//" >>sed.txt
fi
done
sed -f sed.txt $1 >$2

I call the script and pass 2 parameters 1- old file 2- new corrected file, and the new file is exactly as the old with no correction.


Solution

  • You can use this sed:

    sed '/^170/{:loop; N;/\n170/{P;D;t}; s/\n//g;b loop}' file
    

    Input:

    $ cat file
    170033101;20170302;;;"Free text 1"
    170033101;20170302;;;"Free text 2"
    170033101;20170302;;;"Free 
    text 3"
    170033101;20170302;;;"Free 
    text 
    4"
    

    Test:

    $ sed '/^170/{:loop; N;/\n170/{P;D;t}; s/\n//g;b loop}' file > correct_file.csv
    170033101;20170302;;;"Free text 1"
    170033101;20170302;;;"Free text 2"
    170033101;20170302;;;"Free text 3"
    170033101;20170302;;;"Free text 4"