LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-13-2012, 03:09 AM   #1
sunilsagar
LQ Newbie
 
Registered: Jul 2011
Posts: 28

Rep: Reputation: Disabled
Find the text and copy to previous line


Hello All,

I have to find the word, if found copy the text to previous line.

Before :

ABC is working
DEF is working
GHI is not-working
ACVFGDSGDDSEDSSS is
working
XYZ is working

Now, if awk '{ print $1}' is working, then the output will be

ABC is working
DEF is working
GHI is not-working
ACVFGDSGDDSEDSSS is working
XYZ is working
 
Old 02-13-2012, 04:52 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
hmmmm ... not sure I can see what that awk statement has to do with anything as it will obviously not produce the output you have shown.

Based on the data you have provided, the following could work:
Code:
awk 'ORS=/working$/?"\n":"\0"' file
 
Old 02-13-2012, 07:23 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian
Posts: 401

Rep: Reputation: 187Reputation: 187
Hi.

The same in sed:
Code:
sed  '/working$/!{N; s/\n/ /}' file
@grail: Wow, cool! I'd add a space before '\0':
Code:
awk 'ORS=/working$/?"\n":" \0"' file
 
Old 02-13-2012, 07:35 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Distribution: RHEL, CentOS
Posts: 1,167

Rep: Reputation: 90
Hi Sunil,

If I understood you need some thing like this.

Say, your present file is as below

Code:
root@box2:/tmp# cat /tmp/oldfile
one is working
two is working
three is working
string is working
four is working
five is working
six is working
string is working
root@box2:/tmp#
Now, you need your file as below, say you need to search line containing keyword "string"
Quote:
one is working
two is working
three is working
string is working
string is working

four is working
five is working
six is working
string is working
string is working
 
Old 02-13-2012, 09:15 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by firstfire
I'd add a space before '\0'
I would have but on copying the OPs data it already has one at the end of the previous line
 
Old 02-13-2012, 10:12 PM   #6
sunilsagar
LQ Newbie
 
Registered: Jul 2011
Posts: 28

Original Poster
Rep: Reputation: Disabled
Yes Vikas

You found it right. I need similar scenario. Thanks
 
Old 02-14-2012, 01:18 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Well I must say I have no idea how Vikas worked this out based on your explanation, but an easy solution is:
Code:
awk '/what your looking for/{print}1' file
 
Old 02-14-2012, 02:00 AM   #8
sunilsagar
LQ Newbie
 
Registered: Jul 2011
Posts: 28

Original Poster
Rep: Reputation: Disabled
Actually I have a file with below content :

ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is
STARTED
ADMU0508I: The Application Server "Server3" is STOPPED

But when I execute the command "awk 'ORS=/STARTED$/?"\n":" \0"' 55.txt"

I am getting the output : Its pulling the line after the stop.

[root@hlixgr217 tmp]# awk 'ORS=/STARTED$/?"\n":" \0"' 55.txt
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Application Server "Server3" is STARTED

I need the output as :
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Application Server "Server3" is STARTED

Please advise. Thanks
 
Old 02-14-2012, 02:26 AM   #9
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian
Posts: 401

Rep: Reputation: 187Reputation: 187
Hi.

I already posted this solution:
Code:
$ sed '/is$/{N;s/\n/ /}'  infile.txt
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Application Server "Server3" is STOPPED
It we encounter the word `is' at the end of line (`$'), then append next line to pattern space (`N'; Now pattern space looks like "...line ending with is\nnext line", \n -- newline character), then replace newline with space. Of cource this solution assumes that next line is a correct one (STARTED or STOPPED). We could check that too, though.
 
Old 02-14-2012, 02:33 AM   #10
chidam
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Posts: 4

Rep: Reputation: Disabled
@Grail - Its working.
What is the meaning of that?
 
Old 02-14-2012, 04:33 AM   #11
vikas027
Senior Member
 
Registered: May 2007
Distribution: RHEL, CentOS
Posts: 1,167

Rep: Reputation: 90
Thumbs up

Quote:
Originally Posted by grail View Post
Well I must say I have no idea how Vikas worked this out based on your explanation, but an easy solution is:
Code:
awk '/what your looking for/{print}1' file
Awesome Grail.

This is the reason, I love this forum. I would do this with some loops and sed/awk combinations. Really, in my wildest dreams I could not think that this could be a one liner.

Grail, request you to please explain how this worked.
 
Old 02-14-2012, 04:50 AM   #12
vikas027
Senior Member
 
Registered: May 2007
Distribution: RHEL, CentOS
Posts: 1,167

Rep: Reputation: 90
Quote:
Originally Posted by sunilsagar View Post
Actually I have a file with below content :

Code:
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED 
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is
            STARTED
ADMU0508I: The Application Server "Server3" is STOPPED
But when I execute the command "awk 'ORS=/STARTED$/?"\n":" \0"' 55.txt"

I am getting the output : Its pulling the line after the stop.

Code:
[root@hlixgr217 tmp]# awk 'ORS=/STARTED$/?"\n":" \0"' 55.txt 
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is            STARTED
ADMU0508I: The Application Server "Server3" is  STARTED
I need the output as :
Code:
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED 
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Application Server "Server3" is STARTED
Please advise. Thanks
Sunil, this is NOT something which you had asked earlier. Also, try to use CODES when pasting outputs of commands, it makes post more readable.
 
Old 02-14-2012, 06:42 AM   #13
vikas027
Senior Member
 
Registered: May 2007
Distribution: RHEL, CentOS
Posts: 1,167

Rep: Reputation: 90
Hi Sunil,

This script should work for you.
Code:
#!/bin/bash
cp -v 55.txt 55.txt_bkup

updatefile ()
{
string=$1
grep -n ^$string 55.txt_bkup | awk -F":" '{print $1}' > lines

for line in `cat lines`;
do
line1=$(expr $line - 1)
sed -i ''$line1's/is/is '$string'/' 55.txt_bkup
done

grep -v ^$string 55.txt_bkup > 55.txt_bkup.tmp
cat 55.txt_bkup.tmp > 55.txt_bkup
rm -f 55.txt_bkup.tmp
}

updatefile STARTED
updatefile STOPPED

rm -f `pwd`/lines
I created a test file, 55.txt and run the script. The script will create a file 55.txt_bkup which should be your required file.

Code:
root@box2:/tmp/abc# cat 55.txt
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED 
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is
STARTED
ADMU0508I: The Application Server "Server3" is STOPPED
ADMU0508I: The Application Server "Server2" is
STARTED
ADMU0508I: The Web server "HTTPServer" is
STOPPED
ADMU0508I: The Web server "HTTPServer" is
STOPPED 
ADMU0508I: The Application Server "Server2" is
STARTED
root@box2:/tmp/abc#


root@box2:/tmp/abc# 
root@box2:/tmp/abc# ./abc.sh 
`55.txt' -> `55.txt_bkup'
root@box2:/tmp/abc# 

root@box2:/tmp/abc# cat 55.txt_bkup
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED 
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Application Server "Server3" is STOPPED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED
ADMU0508I: The Web server "HTTPServer" is STOPPED
ADMU0508I: The Application Server "Server2" is STARTED
root@box2:/tmp/abc#
I am sure there can be an easier way, may be a one liner to do this task.
But as per my limited knowledge in shell scripting, this should work for you.
 
Old 02-14-2012, 07:24 AM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by chidam View Post
@Grail - Its working.
What is the meaning of that?
You might need to tell me where you got this line from chidam??
 
Old 02-14-2012, 07:42 AM   #15
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
OP - you will find that the awk statement does work, however, your new data now has an additional space at the end of the following line:
Quote:
ADMU0508I: The Web server "HTTPServer" is STOPPED $
If you need to be able to have white space at the end you need to advise so it can be adjusted.
Assuming the white space is not there, the following works just fine:
Code:
$ cat -A input_file
ADMU0508I: The Application Server "Portal" is STARTED$
ADMU0508I: The Web server "HTTPServer" is STOPPED$
ADMU0508I: The Node Agent "nodeagent" is STARTED$
ADMU0508I: The Application Server "Server2" is$
STARTED$
ADMU0508I: The Application Server "Server3" is STOPPED$
$ awk 'ORS=/ED$/?"\n":" \0"' input_file
ADMU0508I: The Application Server "Portal" is STARTED
ADMU0508I: The Web server "HTTPServer" is STOPPED
ADMU0508I: The Node Agent "nodeagent" is STARTED
ADMU0508I: The Application Server "Server2" is STARTED
ADMU0508I: The Application Server "Server3" is STOPPED
The cat I used with -A was to illustrate that there are no spaces before the end of any line.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find text in a line, Replace line Cpare AIX 3 04-01-2011 11:33 AM
Help scripting to find line and print previous line to out jamieofansa Programming 4 05-21-2010 01:30 PM
Copy text from the command line fos Linux - Newbie 15 04-25-2009 06:41 AM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
Copy text from command line Duukkis Linux - General 8 08-25-2003 01:29 PM


All times are GMT -5. The time now is 04:31 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration