0

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 https://regex101.com/

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 2 got it working by

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

3
  • 1
    Try this sed 's/^.*::.*//g' data.txt ! Commented Apr 6, 2019 at 19:49
  • It works, splendid, pls have it as answer so i can approve it
    – asadz
    Commented Apr 6, 2019 at 19:54
  • This works better sed -e '/^.*::.*/d' -e '/^$/d' data.txt Commented Apr 6, 2019 at 20:00

2 Answers 2

2
sed '/^[0-9]/d; /^$/d' file.txt
Love
Is
Lie
  • /^[0-9]/d: remove lines starting with a number
  • /^$/d: remove empty lines
1
  • 1
    To combine both in one expression: sed -E '/^([0-9].*)?$/d'
    – dessert
    Commented Apr 6, 2019 at 20:20
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
  • 2
    /::/d is a shorter version of your first expression, combined that is: sed -E '/^(.*::.*)?$/d'
    – dessert
    Commented Apr 6, 2019 at 20:23
  • 1
    interesting didn't know i could use ? like that! Commented Apr 6, 2019 at 20:30
  • please see update!
    – asadz
    Commented Apr 6, 2019 at 22:43

You must log in to answer this question.

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