Modify a string to remove characters

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.
For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column
I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column
string delete
add a comment |Â
up vote
0
down vote
favorite
I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.
For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column
I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column
string delete
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.
For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column
I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column
string delete
I have a text file where the barcode is in column 18. I'm interested in removing the last 16 characters from a TCGA barcode for a long list of samples or alternatively I want to print only the first 12 characters in the string from column 18 to a new column.
The characters differ in each line of the file so I can not simply use the sed command to remove characters following a certain character.
For example: TCGA-2E-A9G8-01A-11D-A403-09 needs to be shorted to TCGA-2E-A9G8 and print the shorted ID in a new column
I've seen responses such as: echo "$string:0:-16" I'm very new to programing so i'm not sure how to automate this for a spreadsheet with over 300,000 lines and directed to a specific column
string delete
asked Jun 7 at 13:52
Meghan
1
1
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
Using awk:
awk 'print substr($18,1,12)' input
This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.
For CSV:
awk -F, 'print substr($18,1,12)' input
Based on Steeldriver's comment for adding this output to a new column:
awk '$(NF+1) = substr($18,1,12) 1' input > output
Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
1
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Using awk:
awk 'print substr($18,1,12)' input
This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.
For CSV:
awk -F, 'print substr($18,1,12)' input
Based on Steeldriver's comment for adding this output to a new column:
awk '$(NF+1) = substr($18,1,12) 1' input > output
Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
1
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
 |Â
show 2 more comments
up vote
3
down vote
Using awk:
awk 'print substr($18,1,12)' input
This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.
For CSV:
awk -F, 'print substr($18,1,12)' input
Based on Steeldriver's comment for adding this output to a new column:
awk '$(NF+1) = substr($18,1,12) 1' input > output
Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
1
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
 |Â
show 2 more comments
up vote
3
down vote
up vote
3
down vote
Using awk:
awk 'print substr($18,1,12)' input
This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.
For CSV:
awk -F, 'print substr($18,1,12)' input
Based on Steeldriver's comment for adding this output to a new column:
awk '$(NF+1) = substr($18,1,12) 1' input > output
Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.
Using awk:
awk 'print substr($18,1,12)' input
This will print characters 1-12 of column 18. This assumes that your text file is separated by whitespace. Which it likely isn't.
For CSV:
awk -F, 'print substr($18,1,12)' input
Based on Steeldriver's comment for adding this output to a new column:
awk '$(NF+1) = substr($18,1,12) 1' input > output
Unfortunately this creates a new file. gawk can modify a file in place but your best bet is to create a new file and then remove the old file.
edited Jun 7 at 14:10
answered Jun 7 at 13:58
Jesse_b
10.2k22658
10.2k22658
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
1
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
 |Â
show 2 more comments
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
1
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
Thanks, is there a way to print the trimmed down ID in a new column within the original file?
â Meghan
Jun 7 at 14:07
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
I'm not sure what i'm doing wrong. When i do awk 'print substr($18,1,12)' input>output i'm getting it to print the from the 12 to last column. Not the first 12 characters in column 18.
â Meghan
Jun 7 at 14:25
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
Getting it to print the what?
â Jesse_b
Jun 7 at 14:27
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
its returning data from the 12 to last column
â Meghan
Jun 7 at 14:28
1
1
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
Please add the sample data as an edit to your question in a code block so that it's legible.
â Jesse_b
Jun 7 at 14:50
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448433%2fmodify-a-string-to-remove-characters%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password