Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace newline if condition is met in next line in text file with linux command line tools

Input

$ cat input.txt
345 "cuad"  "dfr"
23  "test3" "dfec2
"v1"
33 v2
v3"
32  "key3"  "fer"
12  "rte"   "ef"

Goal, I would like to replace the newline with a space only if the next line not starts with number and tab

345 "cuad"  "dfr"
23  "test3" "dfec2 "v1" 33 v2 v3"
32  "key3"  "fer"
12  "rte"   "ef"

trying this
bash command to remove new lines if condition on the next line is met
but I can’t adapt it to my needs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Attempt, it doesn’t work

$ perl -0777 -pe 's/\n(?=[^0-9]+\t)/ /g' input.txt

Thanks in advance

>Solution :

You may use this perl command with a negative lookahead:

perl -0777 -pe 's/\R(?!\d+\t|\Z)/ /g' file

345 "cuad"  "dfr"
23  "test3" "dfec2 "v1" 33 v2 v3"
32  "key3"  "fer"
12  "rte"   "ef"

RegEx Details:

  • \R: Match any line break character (unicode compliant)
  • (?!\d+\t|\Z): Negative lookahead to assert that we don’t have 1+ digits followed by a tab OR end of input ahead
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading