-1

i stuck at this problem for a long time. i have a input file with many line like this

<594711>
<name>
<duyduy>
<SV>
<RUN>
<AL_Ptime>
</common/sAS>
<lsf_login07>
</shsv/DASA>
<594712>
<name>
<thanhthanh>
<SV>
<RUN>
<AL_Ptime>
</common/NDWQ>
<lsf_login07>
</shsv/CXZC>

my desire it split it to row like this. It repeat after job ID

<594711> <RUN> <AL_Ptime> <lsf_login07> </shsv/DASA>
<594712> <RUN> <AL_Ptime> <lsf_login07> </shsv/CXZC>
Lucil120
  • 63
  • 5
  • 1
    Please show your code. – Karl Hill Nov 07 '17 at 03:31
  • i even don't know what command to get it i got the input from code grep -oE '<[^>]+>' file. i think about using Paste -s but it seem like doesn't work as well – Lucil120 Nov 07 '17 at 03:35
  • How come `lsf_login07` came in 2nd row, it is not present in second set of `<594712>`? – RavinderSingh13 Nov 07 '17 at 03:37
  • 1
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Alec Fenichel Nov 07 '17 at 03:58
  • @RavinderSingh13 sorry i forget to type it don't mind it bro i edited it – Lucil120 Nov 07 '17 at 03:59
  • @AlecFenichel here sir this is my whole script and i stuck at this session. I read doc and can't find the right command to solve it #!/bin/csh -f rm -rf result120 rm -rf result124 rm -rf result126 foreach file ( `cat aaa` ) echo `bjobs -l $file >> result120` echo "" end awk '{\ gsub(/ /,"",$0)}\ BEGIN {\ RS =""\ FS=","\ }\ {\ if ($1 ~/Job/){\ print $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16}\ }' result120 > result126 grep -oE '<[^>]+>' result126 > result125 – Lucil120 Nov 07 '17 at 04:01
  • 1
    @Lucil120, np, check my solution on same now. And please don't post your tries in comments, post in your POST itself so that could be visible to all then. Enjoy learning !! – RavinderSingh13 Nov 07 '17 at 04:05

1 Answers1

1

Following awk code may help you in same.

awk '
/<\/shsv\/[a-zA-Z]+>/{
  val=val?val OFS $0:$0;
  print val;
  flag="";
  next
}
/<[0-9]+>/{
  flag=1;
  val=$0;
  next
}
val && (($0 ~ /<RUN>/) || ($0 ~ /<AL_Ptime>/) || ($0 ~ /<lsf_login07>/)){
  val=val?val OFS $0:$0
}
'   Input_file

Output will be as follows.

<594711> <RUN> <AL_Ptime> <lsf_login07> </shsv/DASA>
<594712> <RUN> <AL_Ptime> <lsf_login07> </shsv/CXZC>

EDIT: Adding explanation for code here too.

awk '
/<\/shsv\/[a-zA-Z]+>/{   ##Search for string shsv with alphabets till their group in a line, then do following.
  val=val?val OFS $0:$0; ##create variable named val whose value is current line when val is NULL else it will concatenating its own value with each line.
  print val;             ##printing variable val here.
  flag="";               ##making variable flag as NULL here.
  next                   ##Using next will skip all statements further.
}
/<[0-9]+>/{              ##Search <digits till group and if it is TRUE then do following.
  flag=1;                ##Setting variable flag to 1 now.
  val=$0;                ##making variable val as current line.
  next                   ##Using next will skip all statements further.
}
val && (($0 ~ /<RUN>/) || ($0 ~ /<AL_Ptime>/) || ($0 ~ /<lsf_login07>/)){ ##Checking if variable val is NOT NULL AND (either current line is string <RUN> OR <AL_Ptime> OR <lsf_login07>) then do following.
  val=val?val OFS $0:$0  ##create variable named val whose value is value of current line if va; is NULL else it will concatenate its own value with current line.
}
' Input_file            ##Mentioning the Input_file name here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93