[SOLVED] Extracting lines from a file using shell scripting
Linux - NewbieThis 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
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Extracting lines from a file using shell scripting
I am trying to read a text file using bash shell scripting based on certain conditions.
Sample File Content -
Application Name: xyz
Service Name: Process Archive.par
Deployment Status: Success
Service Instance Name: Process Archive
Machine Name: abc
Status: Running
Condition - Print Application Name having Status as Running
I am able to read file using while IFS and tried different awk combinations also. But nothing worked. The sample file content contains spaces and tab as this is the result of another script I made.
#For list of files
files="
file1.txt
file2.txt
file3.txt
"
for i in $files; do
#For whole directory
for i in *.txt; do
if grep -q "Status: Running" $i; then
echo $i
fi
done
for i in `cat $1`; do
if grep -q " Status: Running" $i; then
echo $($i-5)
fi
done
As I wanted to display the application name when the status is running, I echo ($i-5).
But this is not displaying the expected output.
Below is the output of the above code snippet.
======
grep: Deployment: No such file or directory
grep: Status:: No such file or directory
grep: Success: No such file or directory
grep: Service: No such file or directory
grep: Instance: No such file or directory
=======
I specifically need only Vabc/xyz as output as this application satisfies the condition.I may later try trimming it. But the above command is listing all application names irrespective of the condition.
$1 is the argument that I am passing.
./ListApp.sh sample.txt
Content of sample.txt
Initializing ...
Finished initialization
Application Name: Vabc/xyz
Service Name: Process Archive.par
Deployment Status: Success
Service Instance Name: Process Archive
Machine Name: abc
Status: Running
Application Name: Atest/test
Service Name: Process Archive.par
Deployment Status: Success
Service Instance Name: Process Archive
Machine Name: xyz
Status: Stopped
As others have shown, awk is better suited to the problem, but bash is still fine. I would add that your solution from post #6 does not reflect what you intially told us:
Quote:
I am able to read file using while IFS and tried different awk combinations also.
None of the items highlighted in red appear anywhere in your solution. Furthermore, for loops should not be used to read a file.
@linuxuser06, since the file is created by another script, shouldn't it possible to extract running application name from it and avoid doing a 2nd pass?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.