sed append words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed append words
# 1  
Old 09-22-2010
sed append words

Hi all,

I have a file like
Code:
one   two  three for
five   six    seven eight
.....

Actually i need to append a label to the words that belong to the 2 column and get:
Code:
one   two_label  three for
five   six_label    seven eight
....

I was trying with sed inside vim but I can't figure out a way.
Any help please?
Thx in advance
# 2  
Old 09-22-2010
Code:
$ ruby -ane '$F[1]=$F[1]+"_label";puts $F.join(" ")' file
$ awk '{$2=$2"_label"}1' file

This User Gave Thanks to kurumi For This Post:
# 3  
Old 09-22-2010
Code:
sed "s/  *[^ ]*/&_label/" file

This User Gave Thanks to anbu23 For This Post:
# 4  
Old 09-22-2010
Thx a lot both
# 5  
Old 10-16-2010
Quote:
Originally Posted by anbu23
Code:
sed "s/  *[^ ]*/&_label/" file

Why is the output not like that:
Code:
five   six_label    seven_label eight_label

Wh is _label appended only with the word "six" ?
If the output is like this:
Code:
five   six_label    seven_label eight_label

then where should the sed command be changed?
# 6  
Old 10-17-2010
Quote:
Originally Posted by cola
Why is the output not like that:
Code:
five   six_label    seven_label eight_label

Wh is _label appended only with the word "six" ?
If the output is like this:
Code:
five   six_label    seven_label eight_label

then where should the sed command be changed?
Code:
sed "s/  *[^ ]*/&_label/" file

doesn't have a global flag, therefore it matches only the first instance in each line.
Code:
sed "s/  *[^ ]*/&_label/g" file

Will just do what you originally thought
This User Gave Thanks to Aia For This Post:
# 7  
Old 10-17-2010
Code:
awk '$2=$2 "_label" ' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reverse words with sed

Hi guys, I'm wondering what the best way would be to reverse words in a string based on certain characters. For example: echo Word1Word2Word3Word4 | sed ' /\n/ !G s/\(Word.\)\(.*\n\)/&\2\1/ //D ' This returns: Word4Word3Word2Word1 I'm no sed expert but... (2 Replies)
Discussion started by: Subbeh
2 Replies

2. Shell Programming and Scripting

Append variable value with sed

I have a requirement where I need to add a variable value based on a pattern match. When I try this it works: sed '/IS_ETL_DEV/a\ ABCD' File1.txt > File1.tmp Now I want to replace IS _ETL_DEV with a variable like $Pattern and replace the values ABCD with a variable $Var the value "$$XYZ=1".... (4 Replies)
Discussion started by: vskr72
4 Replies

3. Shell Programming and Scripting

Swap words using sed

Hi. I have to swap the first and the third word in all lines of a txt file using sed. Separators between words are: any charachter, except intervall. I hope, you'll understand what I want to do. my english is not so good, sorry for that:) (10 Replies)
Discussion started by: T720
10 Replies

4. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

5. Shell Programming and Scripting

How to search and append words in a file

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958 only in... (2 Replies)
Discussion started by: gsreeni
2 Replies

6. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

7. Shell Programming and Scripting

How to append using sed

Hello all, I want to use sed to append a variable stored in $i, how do i do that? $i contains a path to a directory and i want to append it before the result i get from awk statement, before affecting awk results. rite now out put i get from code below is:- The path is:... (2 Replies)
Discussion started by: asirohi
2 Replies

8. Shell Programming and Scripting

search two words in sed

I've following sed command working fine - sed '/search_pattern1/ !s/pattern1/pattern2/" file Now, I want to search two patterns - search_pattern1 and search_pattern2 . How can put these into above sed statement ? Thanks in advance. (12 Replies)
Discussion started by: ajitkumar2
12 Replies

9. Shell Programming and Scripting

How to append words at the end of first line

Hi Unix gurus This is my first post here. I have a file which looks like this: string1,string2 ,string3 ,string4 ... ... I need to append all words below first line to the first line, ie string1,string2,string3,string4,... Bear in mind that it is not known how many lines follow... (11 Replies)
Discussion started by: lmatlebyane
11 Replies

10. UNIX for Dummies Questions & Answers

sed [delete everything between two words]

Hi, I have the following codes below that aims to delete every words between two pattern word. Say I have the files To delete every word between WISH_LIST=" and " I used the below codes (but its not working): #!/bin/sh sed ' /WISH_LIST=\"/ { N /\n.*\"/ {... (3 Replies)
Discussion started by: Orbix
3 Replies
Login or Register to Ask a Question