Operating System - HP-UX
1826177 Members
2279 Online
109691 Solutions
New Discussion

Re: 2 vi and 1 grep questions

 
SOLVED
Go to solution
Terrence
Regular Advisor

2 vi and 1 grep questions

I'll bet these are positively intuitive for royalty.

First I need to delete some lines from the password file, but as I delete (or yank) them I want to output them to either the buffer or another file and make sure that everything I've removed is safely in another file for reference.

Secondly I need to do a global removal of the characters ./ that appear at the beginning of each line in a file.

Finally the grep question (or any other technique); I have a directory that I want to list all the files in that directory over a certain age. I can use the find command by itself, but I want to see the list with the same output as a normal ll command. I can't figure out how to grep the ll output to search for files over 180 days old.

Easy points for someone I'll bet!
12 REPLIES 12
Renda Skandier
Frequent Advisor
Solution

Re: 2 vi and 1 grep questions

I can help with the grep/find question

find . \( -name '*' \) -mtime +180 -exec ls -l {} \;


-exec ls -l{} yields the ll results

you can replace the . right after the find with a directory name, if you want to search a specific dir.

renda
John Poff
Honored Contributor

Re: 2 vi and 1 grep questions

Hi,

For your first question, I would just make a copy of your passwd file to something like passwd.save, edit the passwd file and delete the records, and then use 'diff' to pick up the changes. Something like this might work:

cp passwd passwd.save
vipw passwd
diff passwd passwd.save >passwd.diff

For the second question, here is one way to do it with sed:

sed 's/^\.\///' somefile


JP
Mark Greene_1
Honored Contributor

Re: 2 vi and 1 grep questions

For your vi questions:

Make a backup copy of the password file before you edit. Then use vipw to do the edit. See the man page for vipw for more info.

For the remove, try:
:s-"^./"--g

The trick is to remember that the field delimeter for the substitute command can be any character not in the search or the replace strings.

HTH
mark
the future will be a lot like now, only later
James R. Ferguson
Acclaimed Contributor

Re: 2 vi and 1 grep questions

Hi Terrence:

1. Make your modifications (changes, deletions, etc. Then write your output file with a *different* name an quit without updating your original file. Now, 'diff' the original and the modified file.

2. %s/^\///
...will eliminate forward slash (/) characters at the beginning (only) of all lines.

3. find /tmp -xdev -type f -mtime +180 -exec ls -l {} \;|sort -k9|more

Regards!

...JRF...
Terrence
Regular Advisor

Re: 2 vi and 1 grep questions

For the record, James' search and delete string worked, and Mark's and John's did not.

Both Renda's and James' find commands worked. Thank you.

The diff comparison is clever but I know there is a way to yank stuff and output it to another file either with an ending command or appending to a seperate file as you work. I've done it before but didn't write it down.

Oh, for a clearly written vi reference with lots of examples.
Geoff Wild
Honored Contributor

Re: 2 vi and 1 grep questions

You might find some good stuff here for vi:

http://www.math.fu-berlin.de/%7Eguckes/vi/index.php3

http://www.faqs.org/faqs/editor-faq/vi/


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: 2 vi and 1 grep questions

Hi (again) Terrence:

You can (at least) do this:

To write lines 10-17 (for instance) of the file you are editting to 'myfile' do:

:10,17w myfile

To append lines 10-17 of the file you are editting to 'myfile' do:

:10,17w >> myfile

To write (or append) the entire file you are editting, simply drop the (address,address) couple:

:w >> myfile #...appends to 'myfile'

Regards!

...JRF...
vasundhara
Frequent Advisor

Re: 2 vi and 1 grep questions

Hi,

For 1st question, take a backup when u r changing the file.

For u r second question, open the file in vi and give the following in escape mode. Search for "./" in begining of line and replace with nothing.

:s/^\.\///g

I tried the above command. It worked.

For the grep question,
| xargs ll should work.
for example,
find . -name "*" | xargs ll

Thanks and Regards
VJ.
vasundhara
Frequent Advisor

Re: 2 vi and 1 grep questions

Hi,

I am really sorry. I missed 3chars in search and replace command. I forgot to give line numbers.

Here is the right one.

:1,$s/^\.\//g

Regards
VJ.
Piergiacomo Perini
Trusted Contributor

Re: 2 vi and 1 grep questions

Hi Terence,

about your
'but I know there is a way to yank stuff and output it to another file either with an ending command or appending to a seperate file'

try these steps :
- vi file1
- "A n yy (i.e. buffering n lines in A buffer)
- :e file2 (i.e. open new file named 'file2')
- "A p
- then save and close the 'file2'.

Hih

Piergiacomo Perini
Trusted Contributor

Re: 2 vi and 1 grep questions

sorry Terrence (not Terence)

bye
Stephen Day
Occasional Advisor

Re: 2 vi and 1 grep questions


The easiest way to remove './' at the begining of files:

In vi type:

:1,$s#^./##

At the command line:

cat | sed 's#^./##' >

It's normally easier to use '#' instead of '/' with regular expressions when dealing
with path names. It makes them far easier to read.


It was like that when I got here.