Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!