Re: Some bash questions :-)



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.

.



Relevant Pages

  • strict grep when patterns are presented on standard input
    ... Grep is used in the pipe of commands ... be presented to grep, so grep is used with -f - option. ... its arguments from standard input in the pipe (i.e. as standard output from ... I can add just another sed command to the pipe ...
    (comp.unix.shell)
  • Re: want to ask how to use grep -v repeatly
    ... i am a new user for unix, and is writing a script for working purpose. ... could someone tell how can I "grep -v " more than a string in ... a command, cause the machine i'm using is very busy. ... when I pipe the ...
    (comp.unix.shell)
  • Re: how to pass file names get from grep to sed
    ... I write a shell script which first save the result of 'grep -lr 'cat' ... But why pipe command cannot work? ... result to the standard input of the next command? ...
    (comp.unix.shell)
  • want to ask how to use grep -v repeatly
    ... i am a new user for unix, and is writing a script for working purpose. ... could someone tell how can I "grep -v " more than a string in ... a command, cause the machine i'm using is very busy. ... when I pipe the ...
    (comp.unix.shell)
  • Re: MacTeX 2007 and documentation
    ... may be the most useful command available when doing searches at the ... will find every file on your system that has the text "memoir" on it, ... If locate generates 30 lines of text, where each line is a file, grep ... I need a Unix guru to chat to for half an hour or so to put me straight. ...
    (comp.text.tex)