LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-21-2013, 10:48 AM   #1
Anuj12
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
replace command


Hi

I have a file which looks like

I 23 value of I here=56
I 45 value of I here=36
I 45 value of I here=20
II 56 value of II here=22
III 56 value of III here=32
I 45 value of I here=11
II 11 value of II here=79
I 22 value of I here=74
II 3 value of II here=45
III 45 value of III here=43
BTR 12 value of BTR here=11
BT 11 value of BT here=23

I Want it to replace I by string1 and II by string2 and so on.... BT by string4 BTR by string5
I used

sed 's/I\t/string1\t/' infile >sortedfile
sed 's/II\t/string2\t/' infile >sortedfile
sed 's/III\t/string3\t/' infile >sortedfile
sed 's/BT\t/string4\t/' infile >sortedfile
sed 's/BTR\t/string5\t/' infile >sortedfile


but it replaced III as string1string1string1
whT AM i doing wrong.?

Also I want it to change only the first column/tab and not affect the other tabs.

I want it to look like this

string1 23 value of I here=56
string1 45 value of I here=36
string1 45 value of I here=20
string2 56 value of II here=22
string3 56 value of III here=32
.....

string5 12 value of BTR here=11
string4 11 value of BT here=23
PLease help.
 
Old 01-21-2013, 11:35 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
try this:

Code:
sed 's/^I /string1\t/' infile >sortedfile
sed 's/^II /string2\t/' infile >sortedfile
sed 's/^III /string3\t/' infile >sortedfile
sed 's/^BT /string4\t/' infile >sortedfile
sed 's/^BTR /string5\t/' infile >sortedfile

Last edited by divyashree; 01-21-2013 at 11:37 AM.
 
Old 01-21-2013, 12:19 PM   #3
Anuj12
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Unhappy

Hi
I tried this mentioned command too.
sed 's/^I /string1\t/' infile >sortedfile
sed 's/^II /string2\t/' infile >sortedfile
sed 's/^III /string3\t/' infile >sortedfile
but it works on my III as


string1 II III 56 value of III here=32

instead of writing it as

string3 56 value of III here=32
 
Old 01-21-2013, 12:24 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
It's little tricky. but if your actual file is exactly like what you've mentioned, then following one-liner awk code could help:

Code:
~$ awk '{FS=" "; OFS=" "}; {gsub(/^[I]/,"string1",$1); gsub(/string1I/,"string2",$1);gsub(/string2I/,"string3",$1);gsub(/BTR/,"string4",$1);gsub(/BT/,"string5",$1); print $0}' infile.txt
But in order to solve the actual problem, specify that how large your file is, and how much strings or words you want to replace, so looping and regexp can be used. Anyway, provide little more clarification

Last edited by shivaa; 01-21-2013 at 12:26 PM.
 
Old 01-22-2013, 12:29 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


To limit your regexes, use "^" and "\b". "^" matches the start of the line, and \b is a zero-width anchor that matches the transition between a word and non-word character (word characters are [a-zA-Z0-9_]).

And there's no need to use separate sed commands. Just give one command multiple expressions.

Code:
sed 's/^I\b/1/ ; s/^II\b/2/ ; s/^III\b/3/ ; s/^BT\b/4/ ; s/^BTR\b/5/ ; s/^/string/' infile
And just for fun, here's another awk solution for you:

Code:
awk 'BEGIN{ a["I"]=1 ; a["II"]=2 ; a["III"]=3 ; a["BT"]=4 ; a["BTR"]=5 } { $1=a[$1]?"string"a[$1]:$1 }1' infile
@shivaa, there's no need to use sub/gsub if you want to replace the entire value of the field. Just set it as you would a variable (e.g. $1="newstring").

Last edited by David the H.; 01-22-2013 at 12:40 PM. Reason: moar better code
 
1 members found this post helpful.
Old 01-22-2013, 04:48 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
The simple explanation is that you did it in the wrong order.

Because you tried the shortest replacement first, the s/I\t/string1\t/... matched EVERY I in the file.

Had you done the longest exchange first: sed 's/III /string3\t/' ..., then only sequences of III would be replaced - leaving the II and I sequences alone. Then you can do the s/II / sequence.

The same holds true for the 's/BT\ being done before the 's/BTR'. it replace the BT in all positions.

Do the s/BTR first, and it will only find those situations with BTR, and leave the BT entries alone.
 
1 members found this post helpful.
  


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 command search and replace zulkifal Linux - Newbie 8 11-26-2012 11:56 AM
sed command to replace \t(Tab) with '|' anshaa Linux - Newbie 4 08-13-2012 03:17 PM
File contains replace command? vikibd007 Red Hat 3 08-28-2009 03:55 PM
Command Line File Replace MissingLink Linux - Newbie 3 02-06-2009 03:47 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 12:48 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