Print output as columns

Normal grep is not working to get the output.

Sample Input:

newjob: abc
command name: a=b+c
newjob: bbc
command name: c=r+v
newjob:ddc
newjob:kkc
command name: c=l+g
newjob:mdc
newjob:ldc
newjob:kjc
command name: u=dl+g
newjob:lkdc
newjob:lksdc
command name: o=udl+g

Sample output:

NEWJOB~COMMAND
abc~a=b+c
bbc~c=r+v
ddc~
kkc~c=l+g
mdc~
ldc~
kjc~u=dl+g
lkdc~
lksdc~o=udl+g
while read line ; do [ `echo $line | grep "newjob" -c` -gt 0 ] && echo $line | awk -F":" '{print $2"~"}'; [ `echo $line | grep "command" -c` -gt 0 ] && echo $line | awk -F":" '{print $2}' ;done < inputFile

Is this OK for you ?
[I know the script is little long !]

above can be done just by awk

 
awk -F":" '/newjob/{v=$2"~"};/command/{a=$2};{print v""a;v="";a=""}' filename

I am just struggling to get those record in one line as per requirements

This awk gives what OP request

awk -F"[: ]+" '/newjob/&&f {printf "\n%s",$2} /newjob/&&!f {printf $2;f=1} /command/ {print "~"$3;f=0}' file
abc~a=b+c
bbc~c=r+v
ddc
kkc~c=l+g
mdc
ldc
kjc~u=dl+g
lkdc
lksdc~o=udl+g
1 Like

Just that tild ~ missing where command is not found :slight_smile:

Here is a slightly different awk script that produces the requested heading line and the trailing tildes when there is no command associated with the job:

awk -F ':[ \t]*' '
BEGIN { OFS="~"
        print "NEWJOB", "COMMAND"
}
/^newjob/ {
        if(j != "") {
                print j, c
                c = ""
        }
        j = $2
}
/^command/ {
        c = $2
}
END {   if(j != "") print j, c
}' Input > output

which places

NEWJOB~COMMAND
abc~a=b+c
bbc~c=r+v
ddc~
kkc~c=l+g
mdc~
ldc~
kjc~u=dl+g
lkdc~
lksdc~o=udl+g

in the output file.

Now all should be included, header too :slight_smile:

awk -F"[: ]+" 'BEGIN {print "NEWJOB~COMMAND"} /newjob/&&f {printf "~\n%s",$2} /newjob/&&!f {printf $2;f=1} /command/ {print "~"$3;f=0}' file
NEWJOB~COMMAND
abc~a=b+c
bbc~c=r+v
ddc~
kkc~c=l+g
mdc~
ldc~
kjc~u=dl+g
lkdc~
lksdc~o=udl+g

Easy to read version

awk -F"[: ]+" '
BEGIN {print "NEWJOB~COMMAND"} 
/newjob/&&f {printf "~\n%s",$2}
/newjob/&&!f {printf $2;f=1} 
/command/ {print "~"$3;f=0}' file