awk to replace part of a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to replace part of a column
# 1  
Old 07-31-2012
awk to replace part of a column

dear all,
I'm trying to use Awk to eliminate the last two characters from the first column in a file. This two characters are "-1" and I need to eliminate them from each row that I have in the files. The files have two columns and look like:

ID_090-1 2
ID_3787-1 4
ID_0098-1 1
ID_12-1 4

I need to obtain something like:

ID_090 2
ID_3787 4
ID_0098 1
ID_12 4

Any help is suitable!! thank you in advance!
# 2  
Old 07-31-2012
Hi


Code:
$ awk '{sub(/..$/,"",$1);}1' file
ID_090 2
ID_3787 4
ID_0098 1
ID_12 4


Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 07-31-2012
thank you so much guru!!!it works!!
# 4  
Old 07-31-2012
Code:
$ awk -F'[- ]' '{print $1, $3}' a
ID_090 2
ID_3787 4
ID_0098 1
ID_12 4

This User Gave Thanks to cabrao For This Post:
# 5  
Old 08-06-2012
Code:
awk '{split($1,a,"-"); print a[1],$2}' file

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. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

3. Shell Programming and Scripting

Grep/awk part of info of a column

I have a question that I am at a loss to solve. I have 3 column tab-separated data, such as: abs nmod+n+n-commitment-n 349.200023 abs nmod+n+n-a-commitment-n 333.306429 abs into+ns-j+vn-pass-rb-divide-v 295.57316 abs nmod+n+ns-commitment-n 182.085018 abs nmod+n+n-pledge-n ... (2 Replies)
Discussion started by: owwow14
2 Replies

4. Shell Programming and Scripting

Use grep/awk to remove part of column

hi all, how can i use grep or awk to clean the following input data: n<>the<>96427210 861521305 123257583 n<>obj<>79634223 861521305 79634223 n<>nmod<>68404733 861521305 68422718 where the desired results is to remove all non-numeric characters?: 96427210 861521305 123257583 ... (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

How to Replace the value of a column using awk command?

Hi cat test.txt H|123|341|567|asfg D|dfg|trtyy|errt D|ert|frty|wer Here I need to replace the third column value with 100 of the first record only and while printing I need to print the full file content also..I am expecting a result like this H|123|100|567|asfg D|dfg|trtyy|errt... (3 Replies)
Discussion started by: saj
3 Replies

6. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

7. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

8. Shell Programming and Scripting

Changing part of column value with AWK

Hi All , Here is my req I am extracting a column value and I want to change part of its value according to user input Say the column I extracted is of 15 digits and I want to change the 11th to 14th position with user input 4 digit value Example column value 111111111111111 user input... (8 Replies)
Discussion started by: Pratik4891
8 Replies

9. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies

10. 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
Login or Register to Ask a Question