Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-01-2010, 04:26 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2010
Posts: 4
Rep:
|
[Vi] How to put some characters on the end of a line ?
Alright, so the goal here is to use vi to do the following:
We have a list of cyclones in the world. Now i got to check whether or not the second field is more than 1000 mm.
If so, than the line has to have a[*] at the end of the line.
For exemple:
Australia:1411:55.55:Mackay Cyclone 1918:1918:Mackay
to
Australia:1411:55.55:Mackay Cyclone 1918:1918:Mackay[*]
I also have to use a substitute.
So far i've got this but it fails, 6,$s/\([0-9]\)\1\1\1:\1/.* \[\*\]
Last edited by Bertieboy7; 11-01-2010 at 04:37 AM.
|
|
|
|
11-01-2010, 04:50 AM
|
#2
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
Why are you using vi for this? Surely something like awk is much more suitable?
Code:
awk -F: '{if ($2>1000) print $0"[*]"; else print $0}' inputfile.txt > outputfile.txt
If you're desperate to do it in vi (this works in vim I'm sure, though not sure about "proper" vi). In normal mode:
Code:
:%!awk -F: '{if ($2>1000) print $0"[*]"; else print $0}'
|
|
|
1 members found this post helpful.
|
11-01-2010, 05:09 AM
|
#3
|
|
LQ Newbie
Registered: Nov 2010
Posts: 2
Rep:
|
/home/Local_Data
hey everyone,
is /home/Local_Data a directory located on your PC, or on a server? and how can we know that?
|
|
|
|
11-01-2010, 05:09 AM
|
#4
|
|
LQ Newbie
Registered: Nov 2010
Posts: 4
Original Poster
Rep:
|
the assignment tells us to use a substitution in vi. AWK is not allowed nor is the use of parameters like $0.
|
|
|
|
11-01-2010, 05:12 AM
|
#5
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
I'm afraid I don't know regular expressions well enough to help you.
|
|
|
|
11-01-2010, 05:27 AM
|
#6
|
|
LQ Newbie
Registered: Nov 2010
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by pwc101
I'm afraid I don't know regular expressions well enough to help you.
|
It's alright, maybe someone else might help me 
|
|
|
|
11-01-2010, 06:16 AM
|
#7
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,866
|
Hello Bertieboy7 and welcome to LQ,
try this one with vim:
Code:
:%s/\(^\w\+:[1-9]\d\d\d:.\+\)/\1\[\*\]/
Explanation: find "^" beginning of a line followed by a "\w" wordcharacter "\+" more than one of them. Followed by a ":" colon followed by a digit "[1-9]" between 1 and 9 (not 0 because then it will be a number less than 1000) "\d" a digit "\d\d" another two digits, followed by another ":" colon. Here we are, this line needs a "[*]" at the end, we need an additional "." anything "\+" many of them and have yanked the whole line with the "\(" and "\)" (as you did it in your first post). We substitute \1 with \1\[\*\].
Markus
Edit: %s at the beginning of the command means to do it for every line in the whole file.
Last edited by markush; 11-01-2010 at 06:20 AM.
Reason: typo
|
|
|
1 members found this post helpful.
|
11-01-2010, 06:22 AM
|
#8
|
|
LQ Newbie
Registered: Nov 2010
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by markush
Hello Bertieboy7 and welcome to LQ,
try this one with vim:
Code:
:%s/\(^\w\+:[1-9]\d\d\d:.\+\)/\1\[\*\]/
Explanation: find "^" beginning of a line followed by a "\w" wordcharacter "\+" more than one of them. Followed by a ":" colon followed by a digit "[1-9]" between 1 and 9 (not 0 because then it will be a number less than 1000) "\d" a digit "\d\d" another two digits, followed by another ":" colon. Here we are, this line needs a "[*]" at the end, we need an additional "." anything "\+" many of them and have yanked the whole line with the "\(" and "\)" (as you did it in your first post). We substitute \1 with \1\[\*\].
Markus
Edit: %s at the beginning of the command means to do it for every line in the whole file.
|
Thx a lot markus, you saved my day. The only downside of your code is that it is for vim, i got to do it for vi. But with some logic thinking i can easily convert this. Thx a lot, i like this community  .
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:39 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|