LinuxQuestions.org
Visit Jeremy's Blog.
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 02-22-2010, 01:06 AM   #1
mauludi
LQ Newbie
 
Registered: Jan 2010
Posts: 21

Rep: Reputation: 0
How to modify a some particular elements of a column?


Dear All,

I indeed need your help. It is very importan for my research work.



for example mydata.txt:
id type x y z
1 6 0.474611 0.227223 0.583947
2 4 0.422894 0.22726 0.536791
3 5 0.448963 0.200148 0.560336
4 3 0.386478 0.207721 0.515293
5 6 0.371617 0.22361 0.582206
6 4 0.32123 0.222999 0.534782


how to change second column (type) whose values are 4 and 3 to value 1, so that mydata.text file become:

id type x y z
1 6 0.474611 0.227223 0.583947
2 1 0.422894 0.22726 0.536791
3 5 0.448963 0.200148 0.560336
4 1 0.386478 0.207721 0.515293
5 6 0.371617 0.22361 0.582206
6 1 0.32123 0.222999 0.534782


thank you very much for any help

with best wishes,
Mauludi
 
Old 02-22-2010, 01:30 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Didn't you already have this question answered?

http://www.linuxquestions.org/questi...67#post3830467

Evo2.
 
Old 02-22-2010, 01:46 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi mauludi,

Try this, This is working for me.
Code:
tr -s ' ' < new.text | sed -i 's/\(..\)\([4|3]\)/\11/' new.text

Last edited by vinaytp; 02-22-2010 at 02:14 AM.
 
Old 02-22-2010, 02:16 AM   #4
mauludi
LQ Newbie
 
Registered: Jan 2010
Posts: 21

Original Poster
Rep: Reputation: 0
to: Evo2

No, it looks similar but actually different case from my previous question. The previous question is how to delete, now is how to modify.

to: vinaytp
thank you for your help
 
Old 02-22-2010, 02:20 AM   #5
mauludi
LQ Newbie
 
Registered: Jan 2010
Posts: 21

Original Poster
Rep: Reputation: 0
to: vinaytp

tr -s ' ' < new.text | sed -i 's/\(..\)\([4|3]\)/\11/' new.text

which part of this command which determine second column? (which one I should change if I want to modify for example third column rather than second column?)
 
Old 02-22-2010, 03:10 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by mauludi View Post
to: Evo2

No, it looks similar but actually different case from my previous question. The previous question is how to delete, now is how to modify.
Well the answer is nearly the same.

Code:
awk '{ if ( $2==4 || $2==3 ) { $2 = 1 } print $0 }' mydata.txt
Evo2.
 
Old 02-22-2010, 03:12 AM   #7
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by mauludi View Post
which part of this command which determine second column? (which one I should change if I want to modify for example third column rather than second column?)
What ever solution I have provided is not generic. I have assumed first column will contain single letter, so my first dot in sed represents first column, second dot represents a space and next [4|3] will search for the pattern 4 or 3 in second column.

In the below script $2 stands for 2nd column.
you can change it to the column number you wish.

Code:
awk 'BEGIN {FS=" ";OFS=" "}
{
sub("4|3","1",$2)
print $0
}' new.text > temp
mv temp new.text
rm temp
I am not aware of column delimiters you are using in your file. kindly replace FS and OFS accordingly.

I dont know much about awk, Also I dont know how to make in place file editing as sed -i in awk.
Can any body tell how to make in place change to a file in awk.

Thanks in advance....

Last edited by vinaytp; 02-22-2010 at 03:23 AM.
 
  


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
Read text file column by column RVF16 Programming 11 05-31-2009 08:16 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 11:43 AM
How to modify the library path variable?modify the Electronkz Linux - Newbie 1 04-13-2004 07:18 AM
how to modify a column type in postgrest Bheki Linux - General 3 03-01-2004 04:37 AM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 04:34 AM

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

All times are GMT -5. The time now is 11:54 PM.

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