LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-20-2015, 01:42 AM   #1
Upendra Pratap Singh
Member
 
Registered: May 2011
Location: India
Distribution: Ubuntu 14.04
Posts: 110
Blog Entries: 4

Rep: Reputation: 0
Appending text at the beginning and end of second column of each line


Hi,

I am creating a symbol table as follows:

Field Value

field1 value1
field2 value2
field3 value3

A single TAB space is present between FIELD and the VALUE

Now I am required to modify the VALUE column so that the symbol table becomes

Field Value

field1 <s> value1 </s>
field2 <s> value2 </s>
field3 <s> value3 </s>

In this case, there is a single space between <s> and value* and there is a single space between value* and </s>. Also, as usual, there is a TAB separating FIELD and VALUE tabs.
How can I do this?

I am to implement this using either AWK, SED or VI EDITOR

Last edited by Upendra Pratap Singh; 10-20-2015 at 01:45 AM. Reason: Making query precise
 
Old 10-20-2015, 03:50 AM   #2
slacksam
Member
 
Registered: Oct 2012
Location: Germany
Distribution: Slackware, Salix, slarm64
Posts: 210

Rep: Reputation: 37
Hi,

with sed, you can use:
Code:
sed -e "s/\t/\t \<s\> /" -e "s/$/ \<\/s\>/"
and with awk it could be
Code:
awk '{gsub(/\t/, "\t <s> "); gsub(/$/, " </s> "); print}'
These examples would work for lists with two columns only.

If there are more than two columns, it would be
Code:
awk 'BEGIN{FS=OFS="\t"}$2="<s> "$2" </s>"'
For detailed informations about the usage of these commands, you can use "man sed" and "man awk".

Last edited by slacksam; 10-20-2015 at 04:24 AM.
 
1 members found this post helpful.
Old 10-20-2015, 07:36 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@OP: please show what you've done so far
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] add text at the beginning and at the end of lines in a file smithy2010 Linux - Newbie 6 07-22-2012 04:07 AM
sed - Problem appending at the end of line jdom Linux - Newbie 1 11-05-2010 01:21 AM
how to add string on the beginning and end of the line packets Programming 2 08-03-2010 09:55 PM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
Appending to a certain line in text w3stfa11 Programming 5 11-09-2006 09:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:06 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
Open Source Consulting | Domain Registration