Re: Some bash questions :-)
- From: "composlinuxmisc" <altcomphardware@xxxxxxxxxxx>
- Date: 30 Aug 2006 03:52:51 -0700
Hi John,
John-Paul Stewart wrote:
composlinuxmisc wrote:
Hi all,
(1)
The bash command below is meant to firstly list all files with the
specified name; and then print a line containing -2718.
It is proven very useful for me to make sure the contents of a certain
line match up, and if they don't I can easily identify which *input.txt
needs to be corrected. Thanks to the guy who showed me how to use the
find command some time ago...saved me a lot of clicking ;-)
find ./ -name \*input.txt; find ./ -name \*input.txt -exec cat {} \; |
grep -2718
The problem is that the -ve sign causes the following error:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Options to programs usually start with "-", so grep is trying to
interpret -2718 as an option to grep rather than a pattern. Most Unix
command line utilities use "--" to signal "end of options" so using
'grep -- -2718' will force grep to treat "-2718" as the "pattern".
Yeah thanks. That was what I was trying to do by enclosing -2718 in
quotes.
Seems that quotes don't help and the proper way to do it is with a '\'
or --
[snip]
(2)
Someone also showed me how to find the current working directory of PID
12345.
It involved: readlink /proc/12345/cwd
How do I pipe it to "cd" so I jump immediately to the directory running
PID 12345?
You probably don't want to "pipe" it per se. You want to use the output
of the readlink command as an argument to cd, which is done with the
backtick operator ` in Bash. So:
cd `readlink /proc/12345/cwd`
will do what you're looking for (I think).
(To "pipe" that to cd would mean to send readlink's standard output to
cd's standard input. But cd doesn't read from stdin. It's a minor
difference in terminology when I said earlier that you don't want to
pipe it per se.)
Thanks. So AIUI you have changed `readlink /proc/12345/cwd` into a
variable by putting it in ` `, equivalent to $() as suggested by Chris?
cd `readlink /proc/20235/cwd`
-bash: cd: readlink /proc/20235/cwd: No such file or directory
me@home~> cd $(readlink /proc/20235/cwd)
me@home:~/work>
Hmm, it seems `` and '' are different too. What is the difference
between `, ' and "?
(3)
Let's say I have several files as below:
/home/me/work/1/input.txt
/home/me/work/2/input.txt
/home/me/work/3/input.txt
and so on
I want to change a line containing:
...
Random seed = 2718
...
To:
Random seed = 314159
in all the files. How do I do this conveniently without resorting to
going to each directory and changing them manually?
Use 'find' to locate the files and then use 'sed' to change them. I
wrote a brief note about 'sed' about an hour ago in this very newsgroup.
That should be enough to get you started. A (rough, untested) example:
Yeah, thanks for that. I'm going to try to learn sed. awk too.
find /home/me/work/ -name input.txt -exec \
sed -ie 's/^Random seed = 2718$/Random seed = 314159/' \{} \;
Note that the pattern I've shown will only match when the line reads
exactly as you stated, to avoid corrupting other data. If you just
replace "2718" with "314159" you might end up changing other lines that
happen to contain the substring 2718. (E.g., "Other data = abc 2718
def" could get changed if you're not careful with your pattern.)
The "\{} \;" bit gets expanded to the filename that 'find' prints out,
and is passed to 'sed'.
Thanks.
.
- References:
- Some bash questions :-)
- From: composlinuxmisc
- Re: Some bash questions :-)
- From: John-Paul Stewart
- Some bash questions :-)
- Prev by Date: Re: leafnode was [Linux is a PITA]
- Next by Date: Re: Some bash questions :-)
- Previous by thread: Re: Some bash questions :-)
- Next by thread: Re: Some bash questions :-)
- Index(es):
Relevant Pages
|