Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Replace wrong lines in csv

I have a csv file like that :

0;test1;description;toto
1;test2;description;tata
2;test3;desc
ription;tutu
3;test4;description;tete

In shell, I would like to replace all the line that doesn't start with a number. In this exemple I want to replace \nription by ription

I don't find the correct expression with sed, grep... :(

I want this result :

0;test1;description;toto
1;test2;description;tata
2;test3;description;tutu
3;test4;description;tete

Thanks a lot

EDIT 1 : I have try something like this :

LC_ALL=C tr '(\n)[0-9]' ' ' < hotels.csv > test.csv

Or this :

sed ':a;N;$!ba;s/\r\n?![0-ç-9]/ /g' hotels.csv 

But i think my regex is wrong and it doesn't work :(

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

With awk this seems feasible:

awk -F ';' '{if (NR>1  && match($1,/^[0-9]+$/)) printf("\n"); printf("%s",$0);} END{printf("\n")}' infile.csv

What it does:

  • from the second line: check if first field is a number and print a newline
  • in any line: print the entire line ($0) without trailing newline

Output is sent to STDOUT, input comes from infile.csv

EDIT: Sorry, i missed to copy the match(...)

over 4 years ago · Santiago Trujillo Relatório

0

Using grep -P

grep -P "^\d" file.csv 

Use grep to match lines that begin with a digit.

over 4 years ago · Santiago Trujillo Relatório

0

due to peculiarities of sed's pattern space processing, you will have to use something like this ..

Note: ~ must be a char not present in your text

$cat file
0;test1;description;toto
1;test2;description;tata
2;test3;desc
ription;tutu
3;test4;description;tete
$ sed 'N;s/\n/~/' file | sed -r 's/~([0-9])/\n\1/g;s/~//g'
0;test1;description;toto
1;test2;description;tata
2;test3;description;tutu
3;test4;description;tete

PS: if your input file has Windows line endings you will have to use \r\n instead of \n

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda