Reputation:
Here is the sample file which I was to process. I thought of using keyword "description" as some sort of RS but don't know how to do that ,also its not consistant.
Background: I am processing a log file which contains, date/time stamp (APR12) in first line and in second line there is a description about the log. This description is availabele for few logs and missig for few.
001 APR12 aaa bbb
Description: This is a test file.
002 APR12 aaa bbb
Description: This is another test file.
003 APR12 aaa XXX
004 APR12 aaa bbb
Description: This is another,after skipping one.
001 APR12 aaa bbb Description: This is a test file.
002 APR12 aaa bbb Description: This is another test file.
003 APR12 aaa XXX
004 APR12 aaa bbb Description: This is another,after skipping one.
Upvotes: 1
Views: 771
Reputation: 10039
sed ':a
N;$!ba
s/\n\([^0-9]\)/ \1/g' YourFile
If you have a GNU sed version after 4.2.2 that allow -z
(-Z option). Thanks to @JJoao for this optimized code.
sed -z 's/\n\(^[0-9]\)/ \1/g' YourFile
Upvotes: 1
Reputation: 5337
perl -p0e 's!\n(?=Des)! !g'
(untested) -- It loads all the file into memory...
Upvotes: 0
Reputation: 58351
This might work for you (GNU sed):
sed 'N;s/\n\(Description\)/ \1/;P;D' file
Read pairs of lines throughout the file and replace the newline with a space if the second line of the pair begins Description
.
Upvotes: 1
Reputation: 157947
Probably too complicated, but here comes a solution with sed
:
# Does the line contain description?
# Yes ...
/Description/{
# Exchange hold and pattern space
x
# Append hold space to pattern space
# separated by newline
G
# Remove that newline by a space
s/\n\+/ /gp
}
# No ...
/Description/! {
# Exchange hold and pattern buffer
x
# The hold buffer contains a prefix line
/Description/! {
# Print it
p
}
# Exchange hold and pattern buffer again
x
# Store current line in the hold buffer
h
}
Upvotes: 0
Reputation: 203169
$ awk '{printf "%s%s", (/^[0-9]/?rs:FS), $0; rs=RS} END{print ""}' file
001 APR12 aaa bbb Description: This is a test file.
002 APR12 aaa bbb Description: This is another test file.
003 APR12 aaa XXX
004 APR12 aaa bbb Description: This is another,after skipping one.
Upvotes: 3
Reputation: 74596
You could add a newline every time the current line does not begin with "Description":
awk 'NR>1 && !/^Description/{print ""}{printf "%s ", $0}' file
The NR>1
prevents a newline from being added at the start of the output.
You may also want to add an END
block to add a newline to the end of the output, if any lines were processed: END{if(NR)print ""}
.
Upvotes: 1