LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-18-2016, 11:07 PM   #1
l33y
Member
 
Registered: Jan 2014
Location: Gulf Coast, USA
Distribution: Xubuntu
Posts: 50
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by l33y; 08-18-2016 at 11:11 PM. Reason: added background
 
Old 08-19-2016, 12:57 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,533

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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.
 
Old 08-19-2016, 02:36 AM   #3
Bubushi
LQ Newbie
 
Registered: Jan 2015
Distribution: Centos, RedHAT, Fedora, Slackware
Posts: 6

Rep: Reputation: Disabled
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"
 
1 members found this post helpful.
Old 08-19-2016, 02:58 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,189

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
sed 's/pattern/replacement/' is the usual syntax and you can find a lot of online regexp testers (http://www.regexpal.com/)
 
1 members found this post helpful.
Old 08-19-2016, 03:59 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,533

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
@Bubushi, that assumes every line needs modifying. I assumed only a subset did - not enough info from the OP.
 
Old 08-19-2016, 04:14 AM   #6
Bubushi
LQ Newbie
 
Registered: Jan 2015
Distribution: Centos, RedHAT, Fedora, Slackware
Posts: 6

Rep: Reputation: Disabled
@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
 
1 members found this post helpful.
Old 08-20-2016, 01:55 AM   #7
l33y
Member
 
Registered: Jan 2014
Location: Gulf Coast, USA
Distribution: Xubuntu
Posts: 50
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
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

Last edited by l33y; 08-20-2016 at 06:45 AM.
 
Old 08-20-2016, 06:32 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
In vim
Code:
%s/.*\zs /\t-/
I note Bubushi code adds a space after tab ("\t<space>-"), not sure it's wanted
 
1 members found this post helpful.
Old 08-20-2016, 06:40 AM   #9
l33y
Member
 
Registered: Jan 2014
Location: Gulf Coast, USA
Distribution: Xubuntu
Posts: 50
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Wow, that is brilliant, Keefaz! It worked! Thank you. This thread is solved.
 
  


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
Sed substitution gives error "sed: command garbled" gsai0205 Linux - Newbie 3 09-13-2013 09:01 AM
SED - substitution carolflb Linux - Newbie 5 02-06-2010 12:20 AM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
sed substitution error BlkPoohba Programming 1 08-25-2004 02:00 PM


All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration