LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-02-2015, 09:43 PM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Rep: Reputation: 0
Help with sed/awk


===File a.log
----------------------
Code:
Rep N1
Forever
Limited
2015

Rep N2
Limited
2016
----------------------
I want to search for "Forever" After each line of Rep and if i find Forever print "Forever Yours" if i dont fine "Forever" insert line "Healthy Wealthy"

for above file the output should look like this...

Code:
Rep N1
Forever Yours
Limited
2015

Rep N2
Healthy Wealthy
Limited
2016
 
Old 10-02-2015, 10:00 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,733

Rep: Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125
Yet another thread where you apparently expect someone else to do all your work.
Show us you've made some effort yourself.
 
2 members found this post helpful.
Old 10-02-2015, 10:17 PM   #3
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
there is a file which i made some scripting to get to this level.

Now that am struk here. i know i cannot ask all my work here. So was asking for help.
 
Old 10-02-2015, 10:29 PM   #4
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
I know with sed i can replace but i am not sure how to seach for word if matches replace and if doesnt after specific word by inserting new text
 
Old 10-02-2015, 11:01 PM   #5
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
Any ideas are appreciated.
 
Old 10-03-2015, 03:04 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 763
Blog Entries: 3

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Quote:
Originally Posted by maddyfreaks View Post
Any ideas are appreciated.
[sigh] You have to realize that, just like in your other post, nobody is going to write this for you (well, I just did). But we are willing to help you figure it out.

I have solved this by writing a small script:
Code:
./readMaddy.sh 
Rep N1
Forever yours
Limited
2015

Rep N2
Healthy Wealty
Limited
2016
And I will be more than happy to share at least parts of my solution and the logic I used, once you have shown us some of your own efforts. Mind you, my solution might not be the best/fastest/smartest, but I have at least solved this problem. Using what knowledge and talent I have. I am 100% sure that not only I, but other users would feel inclined to help you out with this, but it all boils down to, and I am sorry if I repeat myself, helping you help yourself.

So... please show us what you have done this far.

With best regards,
HMW

Last edited by HMW; 10-03-2015 at 03:06 AM. Reason: spelling
 
Old 10-03-2015, 08:15 AM   #7
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
i tried ti make them as single line but not sure how to add some text after certain column

Code:
$ cat x.log |xargs -n 5
Rep N1 Forever Limited 2015
Rep N2 Limited 2016
i want some text after column 2
 
Old 10-03-2015, 08:45 AM   #8
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
tried this way but am not getting whole file.

Code:
$ cat x.log |xargs -n 5|awk '{ if ( $3 !~ /Forever/ ) {printf( "%s\n%s\n", "Healthy", $3 );} else {print $3;}}'
Forever
Healthy
Limited
 
Old 10-03-2015, 10:37 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Are you confusing questions? Your other question was about making all into one line, which this one does not mention, whereas this question was about updating / adding text.

You may wish to keep everything on the same topic or restate your question as this will confuse those trying to assist.
 
Old 10-03-2015, 12:43 PM   #10
maddyfreaks
Member
 
Registered: May 2011
Posts: 70

Original Poster
Rep: Reputation: 0
I want the multiple lines in file to be starting with Rep and ending with year.
So the file i made it to singel line. But the num of feilds are not same . I want the missing feild to be inserted as some text.

for 2nd line am missing 3rd columns so i want to fill it with some dummy value.

either make or add missing 3rd col in straight line

(or)

make or add 3rd line for the file
 
Old 10-03-2015, 01:00 PM   #11
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 763
Blog Entries: 3

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Quote:
Originally Posted by maddyfreaks View Post
I want the multiple lines in file to be starting with Rep and ending with year.
So the file i made it to singel line. But the num of feilds are not same . I want the missing feild to be inserted as some text.

for 2nd line am missing 3rd columns so i want to fill it with some dummy value.

either make or add missing 3rd col in straight line

(or)

make or add 3rd line for the file
WHAT!?!

This is hopeless. Do even you know, for sure, what your desired output is supposed to be. Or is that defined by some entity whispering to you!?!

Sorry if I sound slightly vexed, but this is the second thread where you alter either the input or the desired output and/or change the criteria for your problem.
I am not trying to be mean or disrespectful - but this is, quite frankly, aggravating.

Tell you what, I am going to give you a solution, to your ORIGINAL problem, as specified by you here. You'll get this for free, as in "gratis", and after that I will stay the **** out of your threads.

Here it comes:
Code:
cat maddyfreaks.txt | tr '\n' '+' | sed 's/\(Rep N[0-9]+Forever\)/\1 Yours/;s/\(Rep N[0-9]\)+\([A-EG-Z]\)/\1+Healthy Wealthy+\2/' | tr '+' '\n'
Rep N1
Forever Yours
Limited
2015

Rep N2
Healthy Wealthy
Limited
2016
Take care and all that. I'm out.

HMW

Last edited by HMW; 10-03-2015 at 01:01 PM.
 
1 members found this post helpful.
Old 10-03-2015, 03:43 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Yes it is particularly exasperating, so I also will make note to avoid posts with no attempts in them:
Code:
awk '{if(p && /Forever/)$2 = $2 "Yours";else if(p) print "Healthy Wealthy";p = /Rep/?1:0}1' file
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk or sed help cli Linux - Newbie 7 03-05-2015 12:43 AM
awk/sed help mailvaganam Programming 5 05-03-2011 12:35 AM
Help with awk or sed. tuxtutorials Linux - Software 1 07-23-2009 02:45 AM
awk or sed help cmontr Programming 16 05-14-2008 10:59 AM
awk and/or sed linux2man Linux - General 7 01-22-2007 10:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration