0

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line in the block and is unique to each. There will always be a newline seperating the blocks and that line FileName_ID is not processed only printed. It is possible that there is noting above the FileName_ID and if this happens then it is also printed as is.

file

00-0000-Lname-Fname-REPEAT
xxxx_0001 xxxx_0002
111111-yyyy
xxxx_0003 xxxx_0008
111111-yyyy-0
xxxx_0009 xxxx_0006
FileName_ID

FileName_ID

desired

xxxx_0002 00-0000-Lname-Fname-REPEAT
xxxx_0008 111111-yyyy
xxxx_0006 111111-yyyy-0
FileName_ID

FileName_ID

awk

awk 'NR%2{printf "%s ",$0;next;}1' file | sed 's/xxxx_[0-9][0-9][0-9][13579]//g'

1 Answer 1

0

Perl:

perl -lane 'if ($.%2) {$a=$_; next} if ($F[1]) {print "$F[1] $a"} else {print $a; print} END {print $a if $.%2}' file

AWK:

awk 'NR%2 {a=$0; next} $2 {print $2" "a} !$2 {print a; print} END {if (NR%2) print a}' file

You must log in to answer this question.

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