awk to remove text and split on two delimiters

awk split multiple delimiters
awk multiple delimiters including space
awk field separator multiple characters
bash cut multiple delimiters
awk '(split function)
awk '(print multiple columns with delimiter)
awk remove everything after character
cut delimiter multiple characters

I am trying to use awk to remove the text after the last digit and split by the :. That is common to both lines and I believe the first portion of the awk below will do that. If there is no _ in the line then $2 is repeated in $3 and I believe the split will do that. What I am not sure how to do is if the is an _ in the line then the number to the left of the _ is $2 and the number to the right of the _ is $3. Thank you :).

input

chr7:140453136A>T 
chr7:140453135_140453136delCAinsTT

desired

chr7    140453136   140453136 
chr7    140453135   140453136

awk

awk '{sub(/[^0-9]+$/, "", $1); {split($0,a,":"); print a[1],a[2]a[2]} 1' input

Here is one:

$ awk '
BEGIN { 
    FS="[:_]"               # using field separation for the job
    OFS="\t"
}
{
    sub(/[^0-9]*$/,"",$NF)  # strip non-digits off the end of last field
    if(NF==2)               # if only 2 fields
        $3=$2               # make the $2 from $2
}1' file                    # output

Output:

chr7    140453136       140453136
chr7    140453135       140453136

Tested on GNU awk, mawk, Busybox awk and awk version 20121220.

Two delimiters with AWK, $0 ); # delete all after close print; } ' # if there might be more than one <. 1 per line awk ' { n = split( $0, a, "<" ); # split the input line based on open for( i = 2; i <= n; Hello, I need use comma and spaces as field delimiters, but I can't: text: hello​  Awk to split input a tab-delimited file using multiple delimiters in the same field 2 Use awk to split first line of a file based on delimiter and print others as it is

Using GNU awk:

awk -v FPAT='[0-9]+|chr[0-9]*' -v OFS='\t' 'NF==2{$3=$2}{$1=$1}1'

This relies on the field pattern FPAT that is a regex representing a number or the string chr with a number.

The statement NF==2{$3=$2} is to duplicate the second field if there is only 2 in the record.

The last statement is to force awk to rebuild the record to have the wanted formatting.

Awk/sed command to cut on multiple delimiters, Here are some approaches. You haven't explained what you want to do with the string you want to extract so I will just print it. awk with multiple delimiters Two ways of separating fields in awk There’s actually more than one way of separating awk fields: the commonly used -F option (specified as a parameter of the awk command) and the field separator variable FS (specified inside the awk script code). awk -F field separator

$ awk -F'[:_]' '{print $1, $2+0, $NF+0}' file
chr7 140453136 140453136
chr7 140453135 140453136

How can I remove the text from a line after a certain character with awk, There are two general approaches. Set awk 's field separator to that character. You can then get the parts you want as $1 : $ echo "Today was cloudy; yesterday​  In this article of awk series, we will see how to use awk to read or parse text or CSV files containing multiple delimiters or repeating delimiters. Also, we will discuss about some peculiar delimiters and how to handle them using awk. Let us consider a sample file.

Could you please try following, more generic solution in terms of NO hard coding of copying fields values to another fields etc, you can simply mention maximum number of field value in awk variable and it will check each line(along with removing alphabets from their value) and will copy last value to till end of max value for that line.

awk -F'[:_]' -v max="3" '
{
  for(i=2;i<=max;i++){
    if($i==""){
      $i=$(i-1)
    }
    gsub(/[^0-9]+/,"",$i)
  }
}
1
'   Input_file

To get output in TAB delimited form append | column -t in above code.

How to using multiple delimiters in awk and sed, Multiple delimiter-separated fields in awk or sed. with daily development processing logs, we often encounter the use of delimiters to separate fields. How to remove the first line of a text file using the linux command line? Note that a blank space is used as delimiter. Processing the delimited files using awk. You can also use awk command for same purpose: $ awk -F':' '{ print $1 }' /etc/passwd. Output: root you me vivek httpd. Where,-F: – Use : as fs (delimiter) for the input field separator; print $1 – Print first field, if you want print second field use $2

Effective Awk Programming: Universal Text Processing and Pattern , Universal Text Processing and Pattern Matching Arnold Robbins input is separated into records, the second step is to separate the fields in the records. FS is set to a single character, the newline character always acts as a field separator. Remove text between /* and */, inclusive { if 76 | Chapter 4: Reading Input Files. awk: Print fields between two delimiters on separate lines and send to variables I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc

Bash Command Line and Shell Scripts Pocket Primer, This chapter introduces you to the awk command, which is a highly versatile utility This section also shows you how to delete lines and merge lines in datasets, and The fourth section illustrates how to “split” a text string that contains multiple “.” characters as a delimiter, followed by examples of awk to perform numeric  Works great - Thanks!----- Post updated at 09:32 PM ----- Previous update was at 09:12 PM -----BTW - I am trying to capture STDOUT so hence not read a 'file' but passing the string to awk via a pipe. $0 shows the whole string but $1 shows NULL so it doesn't seem to like the delimiters.

Data Cleaning Pocket Primer, Awk is essentially an entire programming language in a single command, which This section also shows you how to delete lines and merge lines in datasets, and The fourth section illustrates how to “split” a text string that contains multiple “.” characters as a delimiter, followed by examples of awk to perform numeric  Extracting name between two delimiters via awk. Ask Question awk -F"@" '{print $1;}' But the server lies within two different delimeters.

Comments
  • No problem, wait even longer, you might get a bitchin' cool answer.
  • Instead if using sub, you can just do $NF+=0. The conversion rules for string to number will kick in. The current version will fail on a string 1234gh5hsue