Awk: split column if special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: split column if special characters
# 1  
Old 01-27-2018
Awk: split column if special characters

Hi,

I've data like these:

Code:
Gene1,Gene2 snp1
Gene3 snp2
Gene4 snp3

I'd like to split line if comma and then print remaining information for the respective gene.

My code:

Code:

awk '{       
if($1 ~ /,/){
n = split($0, t, ",")
for (i = 0; ++i <= n;) {
print t[i],$2
}

}
else{
print $0
}
}' smalldata.txt

It gives me output:

Code:
Gene1 snp1
Gene2 snp1 snp1
Gene3 snp2
Gene4 snp3


I want an output like:

Code:
Gene1 snp1
Gene2 snp1
Gene3 snp2
Gene4 snp3

Line can have multiple commas.

Linux platform: 4.9.0-4-amd64 #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64 GNU/Linux

Last edited by genome; 01-27-2018 at 06:22 PM.. Reason: platform information
# 2  
Old 01-27-2018
Try:-
Code:
awk '
        {
                if ( $0 ~ /,/ )
                {
                        n = split ( $1, T, "," )
                        for ( i = 1; i <= n; i++ )
                                print T[i], $NF
                }
                else
                        print $1, $NF
        }
' file

# 3  
Old 01-27-2018
Code:
awk ' { for (i=1; i<=NF-1; i++) print $i, $NF } ' FS="[ ,]" file

# 4  
Old 01-27-2018
In case you would not mind to use Perl.

Code:
perl -pale 's/,/ $F[1]\n/' genome.file

Output:
Code:
Gene1 snp1
Gene2 snp1
Gene3 snp2
Gene4 snp3

# 5  
Old 01-28-2018
s/,/ $F[1]\n/g for multiple comas.
# 6  
Old 01-28-2018
Your original code needs to split on $1 (first field) not $O.
Code:
...
n = split($1, t, ",")
...

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 01-28-2018
And, you don't need the test for existence of commas in $1; split will yield one single element in the absence of separators.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handle special characters in awk -F

Hello Folks, Need to bisect strings based on a subset. Below works good. echo /a/b/c/d | awk -F"/c/d$" '{print $1}' /a/b However, it goes awry with special characters. echo /a/b/c+/d | awk -F"/c+/d$" '{print $1}' /a/b/c+/d Desired output: /a/b Escaping the special characters... (11 Replies)
Discussion started by: vibhor_agarwali
11 Replies

2. Shell Programming and Scripting

awk conditions failing (special characters?)

This is really frustrating because I can't figure it out. I'm running a health check script. One of the items I'm checking is the amount of memory on a server. I use the free command, which outputs something like this (excerpt) Mem: 100 100 100 100 Swap: 100 100 100 100 In my debugging... (5 Replies)
Discussion started by: JustaDude
5 Replies

3. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

4. Shell Programming and Scripting

Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts. I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file. awk -F, '{if ($NF ~ /^|^/) print >"goodfile";else print >"badfile"}' filename sample data 1,abc,def,1234,A * 2,bed,dec,342,* A ... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk loop: display special characters

Hi everybody; I have a code and this fetches data from first.txt,modify it and outputs it to second.txt file. l awk 'NR>1 {print "l ./gcsw "$1" lt all lset Data="$2" Value "$3}' /home/gcsw/first.txt > /home/gcsw/second.txt this outputs as: l ./gcsw 123 lt all lset Data=456 Value 789 ... (1 Reply)
Discussion started by: gc_sw
1 Replies

6. Shell Programming and Scripting

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 Replies

7. Shell Programming and Scripting

how to split special characters "|" using awk

Hi friends I need to splict special character "|" here. Here is my script which giving error LINE=INVTRAN|cd /home/msgGoogle TraxFolderType=`awk -F"|" '{print $1}' $LINE` filePath=`awk -F"|" '{print $2}' $LINE` echo "TraxFolderType: "$TraxFolderType echo "filePath :"$filePath ... (3 Replies)
Discussion started by: krishna9
3 Replies

8. Shell Programming and Scripting

Handling special characters using awk

Hi all, How do I extract a value without special characters? I need to extract the value of %Used from below and if its greater than 80, need to send a notification. I am doing this right now..Its giving 17%..Is there a way to extract the value and assign it to a variable in one step? df |grep... (3 Replies)
Discussion started by: sam_78_nyc
3 Replies

9. Shell Programming and Scripting

awk split characters

BEGIN{ printf ("%s\t%s\t%s\t\n", "First Char", "Last Char", "Total Chars"); } { chars = split($1, line, //); charline = line; firstchar = line; lastchar = substr(charline,length(charline)); countchar = length(chars); ... (1 Reply)
Discussion started by: knc9233
1 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question