I have this text.
Code=200]:[ (381_3544|null|https_//cdn10.connected-stories.com)(
Penn State World Campus - San Deigo
|TV)]:677:01:SUCCESS
20220929010707:[url=https_//rtr.innovid.com/r1.62b49edb185030.50315160;cb=1372882909, breakId=1
I want to remove a new line \n end of "(" and "Deigo" Example:
Code=200]:[ (381_3544|null|https_//cdn10.connected-stories.com)(Penn State World Campus - San Deigo|TV)]:677:01:SUCCESS
20220929010707:[url=https_//rtr.innovid.com/r1.62b49edb185030.50315160;cb=1372882909, breakId=1
I tried using this command.
sed -ie "s/San Deigo\n/San Deigo/g"
It is not working. I also tried -z option but it deleted all the line and append to the first line.
CodePudding user response:
Using GNU sed
$ sed -E ':a;N;s/((\()|(Deigo))\n /\2\3/;ba' input_file
Code=200]:[ (381_3544|null|https_//cdn10.connected-stories.com)(Penn State World Campus - San Deigo|TV)]:677:01:SUCCESS
20220929010707:[url=https_//rtr.innovid.com/r1.62b49edb185030.50315160;cb=1372882909, breakId=1
CodePudding user response:
With your shown samples please try following GNU awk
. Written and tested with shown samples only.
awk -v RS='\n[0-9]{14}:\\[url=https?_' '
FNR==1{
gsub(/\n/,"")
printf("%s%s",$0,RT)
}
END{
printf("%s",$0,"")
}
' Input_file
CodePudding user response:
sed
by default processes one line at a time; but you can join more than one line into the pattern space and then replace it.
sed -e '/San Deigo$/N;s/\n//' file
If you want to learn sed
, probably spend half an hour reading an introduction to its facilities. The Stack Overflow sed
tag info page has links to resources.
Generally, though, perhaps you are better off using a language which is not write-only.
awk '/San Deigo$/ { printf "%s", $0; next }1' file
Awk out of the box does not have the -i
flag which is supported by many sed
variants (though it's not standard POSIX); GNU Awk has a -i inplace
plug-in but then you need to make sure your Awk is gawk
.
CodePudding user response:
With perl
:
perl -pe 'chomp if /\($|San Deigo$/'
This will only remove the newline character. To also remove starting whitespace of lines being joined:
perl -pe 's/^\s // if $c==$.; if(/\($|San Deigo$/){$c=$. 1; chomp}'
Use perl -i
for inplace editing.
Also, don't use sed -ie
, it will create a backup file of the original with e
appended to the name (unless that's what you wanted).
CodePudding user response:
This might work for you (GNU sed):
sed -E ':a;N;s/((\(|Deigo))\n/\1/;ta;P;D' file
Append the next line to the pattern space.
If the first line in the pattern space ends in (
or Deigo
followed by a newline, remove the new line and go again by jumping to the label :a
.
Othewise, print/delete the first line in the pattern space and repeat.
N.B. The D
command deletes upto and including the first newline or if there is no newline, deletes the entire line. Unlike the d
command it alters the sed cycle in that if there had been a newline it does not read the next line but begins the cycle as if it had and continues with the remnants of the pattern space from the previous cycle.