4k3or3et
Posts: 123
Joined: Sat Nov 17, 2018 5:47 pm

Filtering data in file...

Fri May 29, 2020 3:26 pm

HI All

i have a file like this:

Code: Select all

one five
two four
three one
*four five
five two
What i am trying to achieve is to add star to beginning of every line which contains sting 'five' but not to the one which already the star at the beginning. Anybody could help?

deepo
Posts: 571
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Filtering data in file...

Fri May 29, 2020 3:30 pm

4k3or3et wrote:
Fri May 29, 2020 3:26 pm
HI All

i have a file like this:

Code: Select all

one five
two four
three one
*four five
five two
What i am trying to achieve is to add star to beginning of every line which contains sting 'five' but not to the one which already the star at the beginning. Anybody could help?
School assignment?
What tools have you been taught that could solve this?

/Mogens

User avatar
rpdom
Posts: 16972
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Filtering data in file...

Fri May 29, 2020 3:33 pm

There would be lots of ways to do that. You could write code in Python3, or C, or even bash. It would be a one-liner in sed (which would be my choice).
Unreadable squiggle

User avatar
B.Goode
Posts: 10093
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Filtering data in file...

Fri May 29, 2020 3:35 pm

rpdom wrote:
Fri May 29, 2020 3:33 pm
There would be lots of ways to do that. You could write code in Python3, or C, or even bash. It would be a one-liner in sed (which would be my choice).



… or [g]awk or PERL.

4k3or3et
Posts: 123
Joined: Sat Nov 17, 2018 5:47 pm

Re: Filtering data in file...

Fri May 29, 2020 5:13 pm

Thank you for all replies. It has to be done in Bash. Preferably 'sed' command if possible...

bjtheone
Posts: 706
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Filtering data in file...

Fri May 29, 2020 6:27 pm

4k3or3et wrote:
Fri May 29, 2020 5:13 pm
Thank you for all replies. It has to be done in Bash. Preferably 'sed' command if possible...
I would start with:

Code: Select all

man sed

User avatar
DougieLawson
Posts: 38784
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Filtering data in file...

Fri May 29, 2020 6:34 pm

4k3or3et wrote:
Fri May 29, 2020 5:13 pm
Thank you for all replies. It has to be done in Bash. Preferably 'sed' command if possible...
So what have you tried? Have you got a working regex for it? If not, how does it fail? What part of the failure are you struggling with?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

4k3or3et
Posts: 123
Joined: Sat Nov 17, 2018 5:47 pm

Re: Filtering data in file...

Fri May 29, 2020 7:53 pm

Thaks again for all replies

i stuck with this:

Code: Select all

sed -i 's/.*five/*&/' filename
it adds star to beginning of each line containing string 'five'.

The problem is that it adds star also to the lines which already has start at the beginning. I can't figure out how to exclude lines with '*'...

jahboater
Posts: 5632
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Filtering data in file...

Fri May 29, 2020 7:59 pm

Perhaps insert

^[^*]

at the start ?
Pi4 8GB running PIOS64

User avatar
DougieLawson
Posts: 38784
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Filtering data in file...

Fri May 29, 2020 8:01 pm

Use

Code: Select all

/^five/
to force it to look at the start of the line.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bjtheone
Posts: 706
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Filtering data in file...

Fri May 29, 2020 8:04 pm

Look up anchoring your regexs.

"/^five" does not work for the data line "four five"

User avatar
DougieLawson
Posts: 38784
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Filtering data in file...

Fri May 29, 2020 8:06 pm

bjtheone wrote:
Fri May 29, 2020 8:04 pm
Look up anchoring your regexs.

"/^five" does not work for the data line "four five"
I don't want to, but the OP will learn more by trying mine and seeing how that fails. Your answer is arcane without a complex explanation.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

jahboater
Posts: 5632
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Filtering data in file...

Fri May 29, 2020 8:16 pm

To the OP, if you worked out that sed command yourself then I think you have a pretty good understanding of things!
Here is a bit more.

^ at the start of an RE says to constrain the match to the start of the line (called anchoring it). Similarly $ anchors to the end of the line.
so for example ^hello$ matches lines containing only the word hello, and ^$ matches the empty line.

[abc] is a character class that matches either a or b or c
[^abc] inverts the class matching anything except a or b or c
[^*] therefore matches anything except *

Putting the two together, ^[^*] matches any line not starting with * which is what you want.

Code: Select all

sed -i 's/^[^*].*five/*&/' filename
You could extend it with \<five\> where \< matches the start of a word and \> matches the end of a word.
This would exclude thing like abcfive.
Pi4 8GB running PIOS64

4k3or3et
Posts: 123
Joined: Sat Nov 17, 2018 5:47 pm

Re: Filtering data in file...

Fri May 29, 2020 8:47 pm

Thank you.

However:

Code: Select all

sed -i '/*/!s/.*five/*&/' filename
seems to work fine in my case. No start are duplicated anymore...

bjtheone
Posts: 706
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Filtering data in file...

Fri May 29, 2020 11:24 pm

DougieLawson wrote:
Fri May 29, 2020 8:06 pm
bjtheone wrote:
Fri May 29, 2020 8:04 pm
Look up anchoring your regexs.

"/^five" does not work for the data line "four five"
I don't want to, but the OP will learn more by trying mine and seeing how that fails. Your answer is arcane without a complex explanation.
I suspect the first hit one gets if you put "anchoring regex" into Google would then be very illuminating. The "Look up" was in an extra handy hint to do that. :-)

https://www.regular-expressions.info/ is a good place to wander around for some basic learning.

Return to “Beginners”