LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 09: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, 10:35 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

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 10:37 AM.
 
Old 01-21-2013, 11:19 AM   #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, 11:24 AM   #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 11:26 AM.
 
Old 01-22-2013, 11:29 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 11:40 AM. Reason: moar better code
 
1 members found this post helpful.
Old 01-22-2013, 03:48 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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