Add tab after digit in specific field in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add tab after digit in specific field in file
# 1  
Old 03-18-2016
Add tab after digit in specific field in file

I am trying to add a tab after the last digit in $3 in the input. The grep below is all I can think off. Thank you Smilie

Code:
sed -n 's/[:digit]:/&/p' input

input
Code:
chr1    955542    955763AGRN-6|gc=75
chr1    957570    957852AGRN-7|gc=61.2
chr1    976034    976270AGRN-9|gc=74.5

desired output
Code:
chr1    955542    955763     AGRN-6|gc=75
chr1    957570    957852     AGRN-7|gc=61.2
chr1    976034    976270     AGRN-9|gc=74.5

# 2  
Old 03-18-2016
Code:
perl -pe 's/^.*\s+.*\s+\d+/$&\t/' cmccabe.input

Code:
perl -pe 's/AGRN-/\t$&/' cmcabe.input

This User Gave Thanks to Aia For This Post:
# 3  
Old 03-18-2016
try also:
Code:
awk '{i=match($3, /[^0-9]/); $3=substr($3, 1, i-1) "\t" substr($3, i); print}' input

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 03-19-2016
Quote:
Originally Posted by cmccabe
I am trying to add a tab after the last digit in $3 in the input. The grep below is all I can think off. Thank you Smilie

Code:
sed -n 's/[:digit]:/&/p' input

input
Code:
chr1    955542    955763AGRN-6|gc=75
chr1    957570    957852AGRN-7|gc=61.2
chr1    976034    976270AGRN-9|gc=74.5

desired output
Code:
chr1    955542    955763     AGRN-6|gc=75
chr1    957570    957852     AGRN-7|gc=61.2
chr1    976034    976270     AGRN-9|gc=74.5

Hello cmccabe,

Considering that your Input_file's $3 always have AGRN or any alphabet that segregates the digits in 3rd field then following may help you in same.
Code:
awk '{sub(/[[:alpha:]]+/,"\t&",$3);print}' OFS="\t"  Input_file

Output will be as follows.
Code:
chr1    955542  955763  AGRN-6|gc=75
chr1    957570  957852  AGRN-7|gc=61.2
chr1    976034  976270  AGRN-9|gc=74.5

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-19-2016
Modifying your sed command:
Code:
sed 's/\([[:alnum:]]*[[:space:]]*\)\{2\}[[:digit:]]*/&\t/' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-19-2016
Thank you all very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update specific value in file with match and add +1 to specific digit

I am trying to use awk to match the NM_ in file with $1 of id which is tab-delimited. The NM_ will always be in the line of file that starts with > and be after the second _. When there is a match between each NM_ and id, then the value of $2 in id is substituted or used to update the NM_. Each NM_... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to add symbol to specific field

Trying to use awk to add a . to $4. The input and output is tab-delimeted, but the awk seems to add a . in front of $5 and is space-delimeted. It seems close, but I am not able to produce the desired output. Thank you :). file chr1 948895 949015 chr1:948895-949015 ISG15 chr1 ... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

sed to add field heards to specific fields

I have tab delimited input that prints out in the format below: I am trying to add field headers to $5 and $6. Not sure if sed is the best tool but my attempt is below. Thank you :). $5 = REF $6 = ALT file ID CHR START STOP 123 1 100 200 A ... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Add specific string to last field of each line in perl based on value

I am trying to add a condition to the below perl that will capture the GTtag and place a specific string in the last field of each line. The problem is that the GT value used is not right after the tag rather it is a few fields away. The values should always be 0/1 or 1/2 and are in bold in the... (12 Replies)
Discussion started by: cmccabe
12 Replies

5. Shell Programming and Scripting

Add value in specific field

Gents, I have many files to change increasing 40000 in specific field. Can you help me with a command with sed or awk to fix this problem. Where the the field has Marker FDU. I need to add 40000 plus example Marker FDU 27152.00 Marker FDU 67152.00 I should... (4 Replies)
Discussion started by: jiam912
4 Replies

6. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

7. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

8. Shell Programming and Scripting

how to add single digit in front of the word and line in the file.

Hi , how to add the single digit to front of the word and front of the lines in the one file with compare pattern file and get digit. like example pattern file pattern.txt pattern num bala 2 raja 3 muthu 4 File Name: chennai.dat muthu is good boy raja is bad boy selvam in super... (6 Replies)
Discussion started by: krbala1985
6 Replies

9. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

10. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies
Login or Register to Ask a Question