So fields are separated by a tilde sign? What is interesting is the delimiter between the lines. It will be a
\n but not a newline.
So as explanation:
s/^/\\n/ command substitutes the line start with
\n but only if the conditions after
if are both true:
- The line number has to greater than 1 (
- The line need to contain at least 5 numbers separated by tilde signs.
Afterwards each line (except empty ones (will be avoided by
-l parameter) will be printed (
printf "%s", $_). The code does not guarantee, that the first 5 fields contain a number!
user@pc:/tmp$ cat inputfile.txt
user@pc:/tmp$ perl -lne 's/^/\\n/ if $.>1 && /^(\d+)~(\d+)~(\d+)~(\d+)~(\d+)/; printf "%s",$_' filename
I am not sure, about your curly brackets around the command, you might want to call it from somewhere else. Another (maybe shorter) version which guarantees the existence of 5 fields containing numbers is:
user@pc:/tmp$ perl -ne 'next unless (/^\d+~\d+~\d+~\d+~\d+/); print $_;' inputfile.txt
It will skip all lines not starting with those five fields and therefore skip empty lines.