ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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
$ 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.
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.
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.
#!/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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.