Using sed for multiple matches instead matching whole file

I have file as

0::question#1::
Love
giberrish fdfd
1::anything#2:
Is
repeat
22::anything#3::
Lie

I want to extract

Love
giberish
****
Is
repeat
****
Lie

cat file.txt | sed 's/::\n([^\n]*)/\1/g'

I'm getting file as

0::question#1::
Love
1::anything#2:
Is
22::anything#3::
Lie

On testing regex is matching here

UPDATE I missed to tell, that file extract is for for multi-line not single line and also i want a deliminator as well. I apologize for trouble

cat org_op.2019.04.06-09.43.59 | sed 's/^.*::.*//g;/^$/d'

I get output as

Love
giberrish fdfd
Is
repeat 2 4 4
Lie

So I added some deliminator in order to split one record from another

cat org_op.2019.04.06-09.43.59 | sed 's/^.*::.*/***/g;/^$/d'

and got

 ***
Love
giberrish fdfd ***
Is
repeat 2 4 4 ***
Lie

To remove top-line i used

cat org_op.2019.04.06-09.43.59 | sed 's/^.*::.*/ ***/g;/^$/d' | tail -n +2

new output is

 Love giberrish fdfd *** Is repeat 2 4 4 *** Lie

I want to capture each record using awk i did

cat org_op.2019.04.06-09.43.59 | sed 's/^.*::.*/ ***/g;/^$/d' | tail -n +2 | awk 'BEGIN{ RS = "" ; FS = "***" }{print $1}'

I get output

Love
giberrish fdfd

I'm unable to remove last empty line which is same across all fields i tried using pipe to sed with /^$/d didn't work

UPDATE 2got it working by

``sed 's/^.::.//g;/^$/d' | tail -n +2 | awk 'BEGIN{ RS = "" ; FS = "" }{print $1}' | sed -e '/^$/d'`

3

2 Answers

sed '/^[0-9]/d; /^$/d' file.txt
Love
Is
Lie
  • /^[0-9]/d: remove lines starting with a number
  • /^$/d: remove empty lines
1

Use the method of matching lines then remnove those matching lines and then remove the spaces...

sed -e '/^.*::.*/d' -e '/^$/d' data.txt

Result:

Love
Is
Lie

Or a shorter version:

sed -E '/^(.*::.*)?$/d'

Info:

  • '/^.*::.*/d': Match lines with :: and delete them
  • '/^$/d': Remove all blank lines
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like