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

Time:07-29

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

Attempt, it doesn't work

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

Thanks in advance

CodePudding user response:

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

CodePudding user response:

Without slurping:

perl -pe'
   chomp;
   if ( /^\d \t/ ) {
      print "\n" if $. > 1;
   } else {
      print "\t";
   }
   END { print "\n" if $. > 0; }
'
  • Related