Replacing text/characters in vi


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text/characters in vi
# 1  
Old 03-08-2011
Replacing text/characters in vi

ok, so i have the following text to replace but it's not working. can someone please help me out:

Code:
:%s~awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'~tail -1~g

I want to replace the entire awk command with tail -1.

thanks
# 2  
Old 03-08-2011
Try again: input, desired output and attempts so far.
# 3  
Old 03-08-2011
The text you are trying to match contains a lot of regular expression metacharacters that require escaping. Which specific characters require escaping depends on which vi you are using, since different clones use slightly different flavors of regular expression.

Regards,
Alister
# 4  
Old 03-08-2011
The square braces are pretty good for escape if \ is not working. Just watch out for the -, ^ and ] ! If you do not need them, use a dot over them.
# 5  
Old 03-09-2011
Hi,

Try:
Code:
$ vim -h | head -1 | sed -e 's/(.*//'
VIM - Vi IMproved 7.3
(inside Vim)
:%s~\Vawk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'~tail -1~g

Regards,
Birei
# 6  
Old 03-09-2011
Since you haven't posted your data file, I'll assume that it looks like this -

Code:
$
$
$ cat -n f0.bak
     1  line 1 =>|awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'|<==
     2  line 2 =>|awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'
     3  awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'
     4  blah
     5  line 5 =>|awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'|<==  and =>|awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'|<==
     6  line 6 =>|awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}' and =>|awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'
     7  awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}' and awk '/[#|^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} /-/ {b=a} END{print b}'
     8  blah blah
$
$

I open the file shown above in Vim 7.3 and run the following editor command:

Code:
:% s/awk '.\{-}'/X/g

That's because I intend to replace the awk script awk '...' by X at all places on each line.

My file looks like this after the operation -

Code:
$
$
$ cat -n f0
     1  line 1 =>|X|<==
     2  line 2 =>|X
     3  X
     4  blah
     5  line 5 =>|X|<==  and =>|X|<==
     6  line 6 =>|X and =>|X
     7  X and X
     8  blah blah
$
$
$

HTH,
tyler_durden
# 7  
Old 03-14-2011
Using vim to call awk n batch seems a bit weird, as a UNIX solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

2. Shell Programming and Scripting

Echo is replacing characters

Hi All, I'm using KSH and am writing a small script that counts the lines of various files in several folders under one root folder. Below is the script: #!/usr/bin/ksh common_path_in="/interface_in/rsc" file_out="/interface_in/rsc/record_count.csv" tot_rec_count=-1 act_rec_count=-1... (5 Replies)
Discussion started by: jagari
5 Replies

3. UNIX for Dummies Questions & Answers

Replacing hex characters

I have the following file consisting of dates and sample measurements: 05��Oct��2010 1.31�� 06��Oct��2010 1.32�� 07��Oct��2010 1.31�� The hex characters are \xc2\xa0 in sequence. I have tried to remove the characters as follows: sed -i '' -e 's/\xc2\xa0//g' file.dat and as follows... (6 Replies)
Discussion started by: figaro
6 Replies

4. Shell Programming and Scripting

Replacing characters

Hi fellow experts, I have a question for you. Data looks like: 00877,05/13/2010,PBO,P,0000708331,518 00877,05/13/2010,PBO,P,0000708331,519 ... ... 00877,05/13/2010,PBO,P,0000708331,2103 00877,05/13/2010,PBO,P,0000708331,2104,etc,etc Basically I have to replace 518,519,2103,2104,... (4 Replies)
Discussion started by: Devski123
4 Replies

5. Shell Programming and Scripting

replacing characters with awk

Hi there! I'm new in this and probably is a quite simple task but I still cannot manage to do it: I have to read some files line by line and then change the input format into another one, but the very first step is to replace the empty variables by error values. I mean, each line looks like:... (2 Replies)
Discussion started by: thoreman
2 Replies

6. Shell Programming and Scripting

Replacing Characters with |

Hi All, example data.log 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 I want like this to 526569346|66815531961|09 526569346|66815531961|09... (4 Replies)
Discussion started by: ooilinlove
4 Replies

7. Shell Programming and Scripting

Replacing Characters

Hi All, I have a file which is delimeted with the character '. i need to replace this character with the same character and also a new line. Can anyone please help me with the tr command for this. Many thanks Karan (11 Replies)
Discussion started by: karansachdeva
11 Replies

8. Shell Programming and Scripting

replacing characters

hi all I have a file that has sone spaces in start then / at last. i want to get rid of this. how to do? eg. 11414/ 49878/ 27627/ I WANT THE FILE AS 11414 49878 27627 PLEASE HELP (3 Replies)
Discussion started by: infyanurag
3 Replies

9. UNIX for Dummies Questions & Answers

replacing characters

Hi, I have a script for replacing bad characters in filenames for f in *; do mv $f `echo $f | tr '+' '_'` done; this replaces + for _ But I need to replace all bad characters ? / % + to _ Pls how can i do this in one script ? (3 Replies)
Discussion started by: palmer18
3 Replies

10. Shell Programming and Scripting

Replacing all but last Hex characters in a text line

I must remove hex characters 0A and 0D from several fields within an MS Access Table. Since I don't think it can be done in Access, I am trying here. I am exporting a Table from Access (must be fixed length fields, I think, for my idea to work here) into a text format. I then want to run a... (2 Replies)
Discussion started by: BAH
2 Replies
Login or Register to Ask a Question