awk to escape space in name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to escape space in name
# 1  
Old 09-22-2016
awk to escape space in name

I am searching a file and not able to escape a space if $i has a space in the name. I tried escaping it with a \ and also trying to add it to allow for spaces in the search. Neither are correct as the first awk only outputs path and the second awk doesn't run. Thank you Smilie.

first awk
Code:
awk -F"[]\":{}, ]*" '
        {for (i=1; i<NF; i++)   {if ($i =="path") print $(i+1)
                                 if ($i =="Sample \ Name") print $(i+1)
                                }
        }
' file

second awk
Code:
awk -F"[]\s":{}, ]*" '
        {for (i=1; i<NF; i++)   {if ($i =="path") print $(i+1)
                                 if ($i =="Sample \ Name") print $(i+1)
                                }
        }
' file

file
Code:
"path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "Sample Name": "MEV37",

desired output
Code:
/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52
MEV37


Last edited by cmccabe; 09-22-2016 at 01:31 PM.. Reason: added code tags
# 2  
Old 09-22-2016
Hello cmccabe,

If your Input_file is same as your sample Input_file then following may help you in same.
Code:
awk -F"[\"|,:]" '{for(i=1;i<=NF;i++){if($i == "path"){print $5};if($i == "Sample Name"){print $11}}}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-22-2016
I get the desired output by tweaking the FS in your "first awk"...
Code:
awk -F"\"[,:]* *\"*" '{
    for (i=1; i<NF; i++) {
        if ($i ~ "path") print $(i+1)
        else if ($i ~ "Sample Name") print $(i+1)
    }
}' file

This User Gave Thanks to shamrock For This Post:
# 4  
Old 09-22-2016
Thank you both Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

2. Shell Programming and Scripting

Escape ' in awk

I am trying to escape single quote in awk, but could not: awk ' BEGIN{ fstr="COMPRESS('Y','N')" } { substr($1,len-3,4)~/_IND/ len=length($1) if(substr($1,len-3,4)~/_IND/) printf("%-32s%-14s%-5s%-14s\n",$1,$2,$3,$fstr,$4,$5,$6,$7) } ' tt.txt > out.log Error: awk: Field... (8 Replies)
Discussion started by: ysvsr1
8 Replies

3. Shell Programming and Scripting

awk not escape my bash variable

I tried to parse data from switch configuration files vlan 1727 name SQ5506-15 by port tagged ethe 8/1 to 8/2 untagged ethe 1/13 ! vlan 2105 name SQ5620-7007(BR2) by port tagged ethe 8/1 to 8/2 untagged ethe 1/17 ! interface ethernet 1/13 port-name SQ5506-15.nic0 rate-limit... (2 Replies)
Discussion started by: winggundamth
2 Replies

4. Shell Programming and Scripting

Using awk with the date command and escape characters

I have a file that is a log file for web traffic. I would like to convert the timestamp in it to unix time or epoch time. I am using the date command in conjunction with awk to try to do this. Just myfile: 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813 28/Aug/1995:00:00:38 1... (3 Replies)
Discussion started by: jontjioe
3 Replies

5. 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

6. Shell Programming and Scripting

awk escape character

ll|awk '{print "INSERT INTO SCHEMA.TABLE_NAME VALUES (`"$9 "`,"$5");" }' INSERT INTO SCHEMA.TABLE_NAME VALUES (``,); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.dmp`,7923328); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.log`,1389); But I want ' in... (2 Replies)
Discussion started by: faruque.ahmed
2 Replies

7. Shell Programming and Scripting

escape space characters in loop from file

Hi Everyone! I want to build sql inserts from a list of countries/regions saved in a file. The list looks like this: United Kingdom Czech Republic ... The script I run is: while read i; do var=`expr $var + 1`; echo "INSERT INTO calltypes VALUES($var, '$i','$i');" >>... (5 Replies)
Discussion started by: linuca
5 Replies

8. Shell Programming and Scripting

Escape Space in a ssh command

The following statement does work. But the second command does not work as expected. ssh root@123.123.123.123 \\"mysqldump -h localhost -u root -pPassWord dbName -d | gzip -cf\\" | gunzip -c > database1.sql ssh root@123.123.123.123 \\"mysqlbinlog /var/log/mysql/mysql-bin.*... (0 Replies)
Discussion started by: shantanuo
0 Replies

9. Shell Programming and Scripting

Escape space in for loop

I have a file with the following contents # more hello.txt man hello man whereru The shell script i have tries to echo the contents of the file hello.txt for i in `cat hello.txt` do echo $i done but the output i am getting is taking the space as a new line.. #... (3 Replies)
Discussion started by: Tuxidow
3 Replies

10. Shell Programming and Scripting

awk / escape character

Hi I'm trying to split a dir listing eg /home/foo1/foo2 I'm using ksh I've tried dir=/home/foo1/foo2 splitit=`echo $dir | awk -F '\/' '{print $1}'` echo $splitit nothing is output! I have checked the escape character. The only one I have found is \ BTW `pwd` | awk -F \/... (8 Replies)
Discussion started by: OFFSIHR
8 Replies
Login or Register to Ask a Question