LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 11-01-2010, 04:26 AM   #1
Bertieboy7
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Question [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.
 
Old 11-01-2010, 04:50 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
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.
Old 11-01-2010, 05:09 AM   #3
malek
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
/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?
 
Old 11-01-2010, 05:09 AM   #4
Bertieboy7
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
the assignment tells us to use a substitution in vi. AWK is not allowed nor is the use of parameters like $0.
 
Old 11-01-2010, 05:12 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
I'm afraid I don't know regular expressions well enough to help you.
 
Old 11-01-2010, 05:27 AM   #6
Bertieboy7
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pwc101 View Post
I'm afraid I don't know regular expressions well enough to help you.
It's alright, maybe someone else might help me
 
Old 11-01-2010, 06:16 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
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.
Old 11-01-2010, 06:22 AM   #8
Bertieboy7
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by markush View Post
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 .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
End-of-line Characters missing from last line of md5 file. Md5sum fails mehorter Linux - General 5 06-29-2009 08:56 PM
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM
kwrite - show end of line characters? babag Linux - Software 2 06-09-2008 05:04 AM
Shell scripting: How to add characters at the end of the line Micro420 Programming 7 05-18-2007 01:56 AM
PHP - how to put an auto <br> at end of line Boby Programming 6 05-04-2004 10:12 AM


All times are GMT -5. The time now is 07:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration