LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   vi substitution question (or sed) (https://www.linuxquestions.org/questions/linux-newbie-8/vi-substitution-question-or-sed-4175587396/)

l33y 08-18-2016 11:07 PM

vi substitution question (or sed)
 
I was wondering if you could tell me the vi substitution command to insert a tab and a minus sign (-) between the "N" in QUEENSTOWN and the 19.80. This is a line in a text file. There is already a tab between the date and "VODAFONE"

I will have hundreds of similar entries, that I would like the substitution command to work for. The dollar amount could be xxx.xx and xxxx.xx. I am trying to format the text file with tab delimiters before inserting it into a mysql database. Thank you in advance!

Code:

03-08-2016  <tab>  VODAFONE RENTAL QUEENSTOWN 19.80
I would like for it to be

Code:

03-08-2016  <tab>  VODAFONE RENTAL QUEENSTOWN  <tab>  -19.80

syg00 08-19-2016 12:57 AM

Seems a pretty straight-forward requirement.
People may be more inclined to help if you show us your attempts, and why you think they didn't do as you wished.

Bubushi 08-19-2016 02:36 AM

Code:

awk '{ $(NF)="\t -"$(NF); print }' ./file.txt
this could do the trick if you are trying to add tab"\t" and "-" sign before the last column "$NF"

pan64 08-19-2016 02:58 AM

sed 's/pattern/replacement/' is the usual syntax and you can find a lot of online regexp testers (http://www.regexpal.com/)

syg00 08-19-2016 03:59 AM

@Bubushi, that assumes every line needs modifying. I assumed only a subset did - not enough info from the OP.

Bubushi 08-19-2016 04:14 AM

@syg00, I asumed he was trying to add tab and minus in front of last argument, otherwise it is straight forward replacing text on vi
Code:

:%s/patern/replacement/
or
Code:

:%s/old-text/new-text/g
or with sed as pan64 mentioned ;)

l33y 08-20-2016 01:55 AM

Bubushi, your first suggestion worked perfectly. Thank you! Sorry for not providing more background. I would like the command to execute hundreds of lines of the same file. Basically I am starting with the following format:

Code:

03-03 SQUARE THERAPEUTICS PORT OCONNOR TX 1988.88
03-03 GANDER MOUNTAIN DALLAS TX 146.12
03-04 RACEWAY6945 23969454 LEAGUE CITY TX 6.45
03-06 VODAFONE RENTAL QUEENSTOWN 19.80

I was able to use the following single substitution command to add the date and a tab:
Code:

:%s/\ /-2016\t/
That produced the following:
Quote:

03-03-2016 SQUARE THERAPEUTICS PORT OCONNOR TX 1988.88
03-03-2016 GANDER MOUNTAIN DALLAS TX 146.12
03-04-2016 RACEWAY6945 23969454 LEAGUE CITY TX 6.45
03-06-2016 VODAFONE RENTAL QUEENSTOWN 19.80
Bubushi's awk command did exactly what I needed.
Code:

awk '{ $(NF)="\t -"$(NF); print }' ./file.txt
Quote:

03-03-2016 SQUARE THERAPEUTICS PORT OCONNOR TX -1988.88
03-03-2016 GANDER MOUNTAIN DALLAS TX -146.12
03-04-2016 RACEWAY6945 23969454 LEAGUE CITY TX -6.45
03-06-2016 VODAFONE RENTAL QUEENSTOWN -19.80

keefaz 08-20-2016 06:32 AM

In vim
Code:

%s/.*\zs /\t-/
I note Bubushi code adds a space after tab ("\t<space>-"), not sure it's wanted

l33y 08-20-2016 06:40 AM

Wow, that is brilliant, Keefaz! It worked! Thank you. This thread is solved.


All times are GMT -5. The time now is 07:27 PM.