LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Appending text at the beginning and end of second column of each line (https://www.linuxquestions.org/questions/linux-general-1/appending-text-at-the-beginning-and-end-of-second-column-of-each-line-4175556646/)

Upendra Pratap Singh 10-20-2015 01:42 AM

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

slacksam 10-20-2015 03:50 AM

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".

chrism01 10-20-2015 07:36 PM

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


All times are GMT -5. The time now is 04:37 AM.