LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [Vi] How to put some characters on the end of a line ? (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bvi%5D-how-to-put-some-characters-on-the-end-of-a-line-841641/)

Bertieboy7 11-01-2010 04:26 AM

[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/.* \[\*\]

pwc101 11-01-2010 04:50 AM

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}'

malek 11-01-2010 05:09 AM

/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?

Bertieboy7 11-01-2010 05:09 AM

the assignment tells us to use a substitution in vi. AWK is not allowed nor is the use of parameters like $0.

pwc101 11-01-2010 05:12 AM

I'm afraid I don't know regular expressions well enough to help you.

Bertieboy7 11-01-2010 05:27 AM

Quote:

Originally Posted by pwc101 (Post 4145631)
I'm afraid I don't know regular expressions well enough to help you.

It's alright, maybe someone else might help me :D

markush 11-01-2010 06:16 AM

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.

Bertieboy7 11-01-2010 06:22 AM

Quote:

Originally Posted by markush (Post 4145661)
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 :p.


All times are GMT -5. The time now is 06:42 AM.