Replace 1st column entires


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace 1st column entires
# 1  
Old 02-20-2015
Replace 1st column entires

Hi,

I have an input file :
Code:
entries6091958        LEAN   assumption    11      112     0.999974        -       .       ID=hi_LEAN_10000003;
entries6091958        LEAN   prediction    11      112     0.999974        -       .       ID=hi_LEAN_10000003;
entries6091958        LEAN   CFD     11      112     .       -       0       pnt=chi_LEAN_10000003;
C6091988        LEAN   assumption    21      212     0.999974        -       .       ID=hi_LEAN_10000007;
C6091988        LEAN   prediction    21      212     0.999974        -       .       ID=hi_LEAN_10000007;
C6091988        LEAN   CFD     21      212     .       -       0       pnt=chi_LEAN_10000007;


My expected output file is
Code:
string6091958        LEAN   assumption    11      112     0.999974        -       .       ID=hi_LEAN_10000003;
string6091958        LEAN   prediction    11      112     0.999974        -       .       ID=hi_LEAN_10000003;
string6091958        LEAN   CFD     11      112     .       -       0       pnt=chi_LEAN_10000003;
string6091988        LEAN   assumption    21      212     0.999974        -       .       ID=hi_LEAN_10000007;
string6091988        LEAN   prediction    21      212     0.999974        -       .       ID=hi_LEAN_10000007;
string6091988        LEAN   CFD     21      212     .       -       0       pnt=chi_LEAN_10000007;

what i did so far is
1 sed 's/entries/string/g' : THIS REPLACED ALL entries by string in first column

2. then i did sed 's/C/string/g'

but it replaced the C in the CFD in third column.
I only want to replace in first column and retain data in other column entries

i am not sure how to proceed.
# 2  
Old 02-20-2015
Code:
sed -e 's/^[^0-9]*/string/'

This User Gave Thanks to derekludwig For This Post:
# 3  
Old 02-21-2015
remove the 'g' in your code
Code:
sed 's/entries/string/'
sed 's/C/string/'

# 4  
Old 02-21-2015
Anchor the pattern as derekludwig did:
Code:
sed 's/^C/string/'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-22-2015
thanks!
# 6  
Old 02-22-2015
Not having the g option means only one substitution (the first match) per line. This works if the first match is in the first column.
The ^ anchor does the match at the beginning of the line - perfect here.
More general is something that is column-aware
Code:
awk '{sub("C","string",$1)}1'

where the scope is $1. You can further use an anchor "^C" to have the match only at the beginning of column 1. And you could do the same for $2 or any other column!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

3. Shell Programming and Scripting

Get extract and replace column with link in a column where it exists

hi i have sample data a,b,c,d,e,g h http://mysite.xyx z,b,d,f,e,s t http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing output expected is a,b,c,d,e,http://mysite.xyx z,b,d,f,e,http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing i can get only links using grep -o 'http.*' i... (8 Replies)
Discussion started by: zozoo
8 Replies

4. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

5. Shell Programming and Scripting

Remove bracket part entires and separate entries after comma

Hi all This time my input conatin 3 columns: ERCC1 (PA155) Platinum compounds (PA164713176) Allele A is not associated with response to Platinum compounds in women with Ovarian Neoplasms as compared to allele C . CES1 (PA107) methylphenidate (PA450464) Genotype CT is not... (4 Replies)
Discussion started by: Priyanka Chopra
4 Replies

6. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Solaris

How to remove setfacl entires for a file

I gave the permission for a user using Setfacl as folllows setfacl -m u:user:rwx,m:rwx /home/master To a home path of a master user When i try to remove the same it showing as $ setfacl -r s /home/master usage: setfacl -f aclfile file ... setfacl -d... (2 Replies)
Discussion started by: GIC1986
2 Replies

9. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

10. Shell Programming and Scripting

Replace 10th column with a new column--- Terriblly hurry

Hi Can any one tell me how to replace the 10th column in a file(comma delimted) with a new file with a single column. Can any one Help me out of the please as soon as possible as i am in a terribley hurry!!!!!! Many THanks, (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies
Login or Register to Ask a Question