2

File1

pure info:       myjob-relaed_rawmaterila
Timings:              Full
pure info:       Check-platform-Log-90Days
Timings:              Full
Timings:              Incremental
pure info:       Check-vitorydows-Log-90Days
Timings:              Full
Timings:              Incremental
pure info:       Note_michael
Timings:              Full
pure info:       adhoc-activity-myjob-platform
Timings:              Full
pure info:       adhoc-activity-myjob-vitory
Timings:              Full
pure info:       adhoc-myjob-platform-03
Timings:              Full
Timings:              Full-1month
pure info:       adhoc-onetime-myjob-hotcase
Timings:              Full
pure info:       adhoc-onetime-myjob-platform

output required

pure info: myjob-relaed_rawmaterila Timings:  Full
pure info: Check-platform-Log-90Days Timings:  Full Timings:  Incremental
pure info: Check-vitorydows-Log-90Days Timings:  Full Timings:  Incremental
pure info: Note_michael Timings:  Full
pure info: adhoc-activity-myjob-platform Timings:  Full
pure info: adhoc-activity-myjob-vitory Timings:  Full
pure info: adhoc-myjob-platform-03 Timings:  Full Timings:  Full-1month
pure info: adhoc-onetime-myjob-hotcase Timings:  Full

I tried

cat file1|sed -e 's/^ //' -e 's/$// 
cat file1|perl -pe's/\n/ / if $.  % 3' 

but this is printing only 2 lines in one line and if there is an extra Timings entry then this is zig-zagging the output.

How can I solve this problem?

1
  • 2
    So the last line of input (pure info: adhoc-onetime-myjob-platform) shouldn't be printed at all - is that because there's no Timings line after it?
    – Ed Morton
    Jul 1, 2020 at 12:14

5 Answers 5

1

concerning your perl-solution: due to the -p construct, the provided script processes the input line by line. This makes it difficult to join lines, since a line does not know what follows.

When you process the input as one string, you can do smart replacements to join lines. Here is an example similar to your way of calling perl:

perl -e 'local $/;my $data = <>; $data =~ s/\nTiming/ Timing/g ; print $data' file1

This script reads the whole input in one string ($data) and replaces the newline before Timing with a space. It does so for all occurences (g switch for Global after replace)

2
  • thnx a lot ruud ..it worked ...brilliant !!! Jul 1, 2020 at 8:04
  • 1
    You may skip cat: perl -e'...' file1
    – Quasímodo
    Jul 1, 2020 at 11:55
1

A GNU sed solution:

sed -rz 's/\n(Timings)/ \1/g' file1
3
  • sed is giving error as sed: invalid option -- z Jul 1, 2020 at 8:49
  • It means you do not have GNU sed.
    – seshoumara
    Jul 1, 2020 at 8:51
  • I will try to get that ..thnx a Lot seshoumara ..for big help appreciate it Jul 1, 2020 at 8:56
1

The perl one-liner version:

perl -0777 -pe 's/\n(?=Timings:)/ /g' file

The -0777 option forces Perl to slurp the whole file into $_. Then, all newlines that appear before "Timings:" are replaced.

If there are other headings beside "Timings", you could use a negative look-ahead:

s/\n(?!pure info:)/ /g
0

Late to the show but if spacing is not an issue then....

awk -v RS="pure info: " 'NR>1{$1=$1; print RS$0}' file

Just use pure info: as the record separator RS, recompose $1=$1 and print prepending RS

pure info: myjob-relaed_rawmaterila Timings: Full
pure info: Check-platform-Log-90Days Timings: Full Timings: Incremental
pure info: Check-vitorydows-Log-90Days Timings: Full Timings: Incremental
pure info: Note_michael Timings: Full
pure info: adhoc-activity-myjob-platform Timings: Full
pure info: adhoc-activity-myjob-vitory Timings: Full
pure info: adhoc-myjob-platform-03 Timings: Full Timings: Full-1month
pure info: adhoc-onetime-myjob-hotcase Timings: Full
pure info: adhoc-onetime-myjob-platform

And if you are desperate for the spacing then gsub one back in...

awk -v RS="pure info: " 'NR>1{$1=$1; gsub("Timings:","Timings: ",$0);print RS$0}' file

pure info: myjob-relaed_rawmaterila Timings:  Full
pure info: Check-platform-Log-90Days Timings:  Full Timings:  Incremental
pure info: Check-vitorydows-Log-90Days Timings:  Full Timings:  Incremental
pure info: Note_michael Timings:  Full
pure info: adhoc-activity-myjob-platform Timings:  Full
pure info: adhoc-activity-myjob-vitory Timings:  Full
pure info: adhoc-myjob-platform-03 Timings:  Full Timings:  Full-1month
pure info: adhoc-onetime-myjob-hotcase Timings:  Full
pure info: adhoc-onetime-myjob-platform
0

Using :

perl -ne '
    my $s = $_ if
        /pure info:\s+myjob-relaed_rawmaterila/
        ..
       /pure info:\s+adhoc-onetime-myjob-hotcase/;
    do{
        chomp;
        s/\s{3,}/ /;
        $_ = sprintf "%s ", $s;
        s/pure info/\n$&/g;
        print
    } for $s;
    END{ print "\n" }
' file1
2
  • The spaces are not like in the expected output (the question shows two between Timings: and Full).
    – Quasímodo
    Jul 1, 2020 at 16:08
  • Not sure the exact spaces (1 char difference) are needed, I commented your answer, because that was king of tab delimited before your edit Jul 1, 2020 at 16:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .