Pregunta

I am trying to write a command (sed/awk) for replacing a newline with a dash under the following conditions:

This should not be replaced with a dash because there is no new line in the CSV:

X00000;111111;1111111111;This is just a text

Anyway, in this example, the new line should be replaced with a dash:

X00000;111111;1111111111;This is a longer text which contains a 
new line sign.

The output of the replacement should look like this:

X00000;111111;1111111111;This is a longer text which contains a - new line sign.

Edit: This should also work for lines like this:

X00000;111111;1111111111;"This is a longer text which contains a
new line sign
or even more

or a line that even contains only a new line sign

"

In this case, the following output is expected:

X00000;111111;1111111111;"This is a longer text which contains a - new line sign - or even more - - or a line that even contains only a new line sign - "
¿Fue útil?

Solución

Here is an option using sed:

$ cat file
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a 
new line sign.
X00000;111111;1111111111;"This is a longer text which contains a
new line sign
or even more

or a line that even contains only a new line sign

"

$ sed  ':a;$bc;N;s/\n/ - /;ba;:c;s/ - X00000;/\nX00000;/g' file
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a  - new line sign.
X00000;111111;1111111111;"This is a longer text which contains a - new line sign - or even more -  - or a line that even contains only a new line sign -  - "

Explanation:

sed '
    :a                         # Create a label a
    $bc                        # If it is last line, branch to label c
    N                          # Append next line to pattern space
    s/\n/ - /                  # Remove the \n and replace it with -
    ba                         # Keep repeating above steps until file is complete
    :c                         # Our label c. Do the following when end of file is reached
    s/ - X00000;/\nX00000;/g   # We do this substitution to add newlines where needed. 
' file

Otros consejos

Using awk you can do:

awk -F ';' 'NF<4{print p, "-", $0;p="";next} p{print p} {p=$0} END{if (p) print p}' file.csv
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a  - new line sign.

Here is an awk that just join all together.

awk '{printf (NR==1||!/X00000/)?$0:RS $0} END {print ""}' file
X00000;111111;1111111111;This is just a text
X00000;111111;1111111111;This is a longer text which contains a new line sign.
X00000;111111;1111111111;"This is a longer text which contains anew line signor even moreor a line that even contains only a new line sign"

It will not add the -

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top