LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   return and exit (plus more questions about shell scripting) (https://www.linuxquestions.org/questions/linux-newbie-8/return-and-exit-plus-more-questions-about-shell-scripting-937730/)

ofer4 04-05-2012 07:13 AM

hi


thanks you i found a solution...

i have one more problem...

if i have a input file that stored in the path:

tests\input.in

the bash recognize it as a full name of input, it wrote to me:

"./testInOut: line 5: tests/input.in.in: No such file or directory"

as i understand ' the bash thinks that the path which i provided, this is the input file name: "tests/input.in"

how can i wrote to the bash that this is a path to the input file and the input file is only input.in?

thanks alot

catkin 04-05-2012 09:44 AM

Quote:

Originally Posted by ofer4 (Post 4645368)
...
tests\input.in
...
"./testInOut: line 5: tests/input.in.in: No such file or directory"
...
"test/input.in"

Please post your script or the first 5 lines if it is long.

Is the directory tests/ or test/?

ofer4 04-05-2012 09:56 AM

thanks for the advice

i edit my post (i mean tests directory)

please, soeone can help me?

ofer4 04-05-2012 10:24 AM

i have one more question:

i wrote this code:

Code:

#!/bin/bash

cat "1.memory.log" | grep ==*==

i want to display all the names which start with "==" and end with "=="

however my script doesnt work

any ideas?

David the H. 04-05-2012 11:35 AM

"*" is a reserved shell character, the basic glob. You have to quote the grep string to protect it. But also, grep uses regular expressions, not globbing patterns, so you need to use ".*" instead. You may also want to anchor the string to the start and end of the line.

Finally, Useless Use Of Cat.

Code:

grep '^==.*==$' 1.memory.log
By the way, this thread is getting quite far off the topic of the original thread title. But rather than start a new thread, I suggest that you use the the "report" button and ask a moderator to change the title of this one to something more appropriate, like "some questions about scripting".

catkin 04-05-2012 11:47 AM

Quote:

Originally Posted by ofer4 (Post 4645510)
please, soeone can help me?

Please post your script or the first 5 lines if it is long.

uhelp 04-05-2012 12:05 PM

"read" can do do the proper assignments on it's own.

Code:

IFS='@'
while read description in out;
    do
    #....
    #....
done < theFileContainigThisStuff

And, please, do not extend one thread to handle loads of different problems.
Instead one ask per thread.
Makes it easier and can be easily searched provided you named the thread properly.

ofer4 04-05-2012 01:28 PM

Quote:

Originally Posted by David the H. (Post 4645601)
"*" is a reserved shell character, the basic glob. You have to quote the grep string to protect it. But also, grep uses regular expressions, not globbing patterns, so you need to use ".*" instead. You may also want to anchor the string to the start and end of the line.

Finally, Useless Use Of Cat.

Code:

grep '^==.*==$' 1.memory.log
By the way, this thread is getting quite far off the topic of the original thread title. But rather than start a new thread, I suggest that you use the the "report" button and ask a moderator to change the title of this one to something more appropriate, like "some questions about scripting".

hi

first of all, i send a report to the admin, i hope he will change the title soon.


moreover, i wrote your code however it does not work for me...any ideas?


and please someone post a solution to my second problem...i didnt find any solution for it

Quote:



thanks you i found a solution...

i have one more problem...

if i have a input file that stored in the path:

tests\input.in

the bash recognize it as a full name of input, it wrote to me:

"./testInOut: line 5: tests/input.in.in: No such file or directory"

as i understand ' the bash thinks that the path which i provided, this is the input file name: "tests/input.in"

how can i wrote to the bash that this is a path to the input file and the input file is only input.in?

thanks alot

catkin 04-05-2012 09:53 PM

Quote:

Originally Posted by ofer4 (Post 4645707)
and please someone post a solution to my second problem...i didnt find any solution for it

Please post the script that is having the problem

ofer4 04-06-2012 05:08 PM

Quote:

Originally Posted by David the H. (Post 4645601)
"*" is a reserved shell character, the basic glob. You have to quote the grep string to protect it. But also, grep uses regular expressions, not globbing patterns, so you need to use ".*" instead. You may also want to anchor the string to the start and end of the line.

Finally, Useless Use Of Cat.

Code:

grep '^==.*==$' 1.memory.log
By the way, this thread is getting quite far off the topic of the original thread title. But rather than start a new thread, I suggest that you use the the "report" button and ask a moderator to change the title of this one to something more appropriate, like "some questions about scripting".


hi

thanks everybody i still have a problem regarding this issue..please let me know if there is any solution for it...

hi catkin, regarding the last problem i solved it...thanks everybody

David the H. 04-07-2012 06:21 AM

Sorry, I can't see any problems with it, and it works for me.

Please show us an actual example of the text that needs to be matched, and the exact commands you've used including the output you get, if any.

ofer4 04-07-2012 04:38 PM

hi the directory logs contains a file which named 1.memory.log and

it wrote there ==24031== so the output must be 24031.

however the output of your code is empty..

and other suggestions?

thanks

David the H. 04-08-2012 09:55 AM

Again, use code tags around all of your actual code or data, and cut&paste wherever possible to capture the exact text, including any non-printing characters. The true format of the text can be very important in cases like this.


As I suspected, your actual requirements are different from what you described. You want to capture the part between the equal signs, not just match the line itself.

grep's only function is to search for regex patterns within lines of text, and output either the entire line or the entire matched pattern (with the -o option). It can't extract substrings from a given pattern.

The pattern I gave above is for matching an entire line. If a line starts (^) with two equal signs, and ends ($) with two equal signs, then that whole line will match and print.

So you should at least be getting the entire line in the output. If you're matching nothing, then there's something about the file or the command you used that you still haven't shown us. Again, cut&paste the exact file contents and the output of your terminal, in code tags.

But in any case you can't extract the number in the middle this way. To extract a substring from a regex pattern, you have to use sed (or perhaps awk) instead. Those tools provide the line-editing ability needed to separate the parts you want from the parts you don't want.

Code:

sed -rn '/^==/ s/^==([^=]*)==$/\1/p' file
This should capture the part between the equal signs, and print out only that part of the line.


You really need to take some time to learn how to use regular expressions if you intend to do a lot of text manipulation like this. They are supported by a large number of scripting tools like these.

A couple of quick regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html


You should also familiarize yourself with the tools you're using. Take some time to look at the info pages for grep, sed, awk, find, and the coreutils, at the very least.

The grymoire site also has pretty good tutorials of some of these tools:
http://www.grymoire.com


All times are GMT -5. The time now is 12:20 PM.