awk with tab delimeter

Tags
awk, delimeter, shell, shell scripts, tab

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
awk with tab delimeter

Hi Team

below test file contains tab delimeter file and i am excepting the number of files 3.

File : test.txt

Code:
a	b	c

Code:
awk -vFPAT='\t' -vOFS="\t" -v a="0" -v b="10"  ' NR>a {if (NF != b ) print NR"@"NF }' test.txt

current output is
1@2
required output is
1@3

Cloud you please help me on this one.
# 2  
Old 2 Weeks Ago
Hello bmk123,

Could you please try following.
Code:
awk -F'[[:space:]]+' '{for(i=1;i<=NF;i++){if($i!="b"){val=val?val OFS i}};print val;val=""}'  OFS="@" Input_file

Thanks,
R. Singh
# 3  
Old 2 Weeks Ago
Thanks singh.
i don't want the change the code agian and i need to implemnt in same code

Code:
awk -vFPAT='([^,]*)|("[^"]+")| [[\t]] ' -vOFS="\t" -v a="0" -v b="10"  ' NR>a {if (NF != b ) print NR"@"NF }' test.txt

i tried above on it is not working.
# 4  
Old 2 Weeks Ago
@OP You are using
Code:
-vFPAT='\t'

, which means that the fields are single TAB characters and the fields are separated by anything else.
If I change -vFPAT='\t' to -vFS='\t' then I get:
Code:
1@3

-vFS='\t' can be shortened to -F'\t', so it would become:
Code:
awk -F'\t' -v a="0" -v b="10"  ' NR>a {if (NF != b ) print NR"@"NF }' test.txt

or
Code:
awk -F'\t' -v a=0 -v b=10  'NR>a && NF!=b {print NR"@"NF}' test.txt

This User Gave Thanks to Scrutinizer For This Post:
bmk123 (1 Week Ago)
# 5  
Old 1 Week Ago
thanks Scrutinizer,
i tried as per your comment and it is not working.

Code:
awk -vFS='([^,]*)|("[^"]+")| [[\t]] ' -vOFS="\t" -v a="0" -v b="10"  ' NR>a {if (NF != b ) print NR"@"NF }' test.txt

# 6  
Old 1 Week Ago
Dear,
Please try below :
Code:
awk -F '[\t]' -v a="0" -v b="10"  ' NR>a {if (NF != b ) print NR"@"NF }' test.txt

This User Gave Thanks to sadique.manzar For This Post:
bmk123 (1 Week Ago)
# 7  
Old 1 Week Ago
Quote:
Originally Posted by bmk123
...
i tried as per your comment and it is not working.

Code:
awk -vFS='([^,]*)|("[^"]+")| [[\t]] ' -vOFS="\t" -v a="0" -v b="10"  ' NR>a {if (NF != b ) print NR"@"NF }' test.txt


That's because you are using a regular expression for FS that works differently than you (probably) think it does.
This part:
Code:
[^,]*

matches "0 or more occurrences of a character other than comma".
So, let's say I have this as my data:
Code:
a,b,c

Then each one of "a", "b" and "c" matches "0 or more occurrences of a character other than comma".
Hence awk considers each one of "a", "b" and "c" as the delimiter and the rest (i.e. commas) as the data tokens.
So it sees this:


Code:
data       delimiter      data       delimiter      data      delimiter     data
NULL       a                ,            b           ,           c          NULL

which explains the output below:

Code:
$
$ printf "a,b,c\n" | awk -vFS='([^,]*)' '{ print NF; print "{"$0"}"; for (i=1; i<=NF; i++){print "{"$i"}"} }'
4
{a,b,c}
{}
{,}
{,}
{}
$
$

If you have tabs instead of commas, then the entire string is a "delimiter" and it is sandwiched between two "data" tokens that are both NULL, as seen below:
Code:
$
$ printf "a\tb\tc\n" | awk -vFS='([^,]*)' '{ print NF; print "{"$0"}"; for (i=1; i<=NF; i++){print "{"$i"}"} }'
2
{a      b       c}
{}
{}
$
$

For the regular expression:
Code:
[[\t]]

awk considers the bold brackets as the character class in the following:
Code:
[[\t]]

Hence it processes both of the following as delimiters:
Code:
[]
\t]

You can test this by running the following one-liner:
Code:
$
$ printf "aaaa\t]bbbb[]cccc\n" | awk -vFS='[[\t]]' '{ print NF; print "{"$0"}"; for (i=1; i<=NF; i++){print "{"$i"}"} }'
3
{aaaa   ]bbbb[]cccc}
{aaaa}
{bbbb}
{cccc}
$
$

If you can explain what exactly you want as the delimiter, then maybe we can help you come up with a better regular expression.

Last edited by durden_tyler; 1 Week Ago at 07:59 PM..
These 2 Users Gave Thanks to durden_tyler For This Post:
bmk123 (1 Week Ago) jim mcnamara (1 Week Ago)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk to add tab to output of both conditions cmccabe Shell Programming and Scripting 2 10-21-2016 06:19 PM
awk to parse current and next row in tab-delimited file emiley UNIX for Beginners Questions & Answers 5 07-27-2016 01:36 PM
awk output seperated by tab cmccabe Shell Programming and Scripting 2 02-24-2016 04:00 PM
awk substr delimeter Ads89 Shell Programming and Scripting 6 02-17-2016 02:45 PM
Replace delimeter between quotes with \| using sed or awk BrahmaNaiduA Shell Programming and Scripting 4 05-04-2015 05:55 PM
awk - Print whole string ending with a Tab if key matched High-T Shell Programming and Scripting 11 01-31-2015 08:28 PM
Replace space and tab to pipe delimeter greenworld123 Shell Programming and Scripting 7 04-25-2013 08:11 AM
Replacing the delimeter with other delimeter i150371485 Shell Programming and Scripting 4 08-07-2012 07:06 AM
awk: string followed by tab comparision ysvsr1 Shell Programming and Scripting 3 01-31-2012 12:34 AM
Count the delimeter from a file and delete the row if delimeter count doesnt match. Akumar1 Shell Programming and Scripting 5 09-01-2010 12:41 AM
Need Awk command to get part of string based on delimeter shanneykar Shell Programming and Scripting 9 08-29-2010 05:07 AM
Conditional tab replacement sed/awk benackland Shell Programming and Scripting 3 05-17-2010 12:43 PM
output - tab formatted - awk Fredrick Shell Programming and Scripting 3 11-27-2009 08:32 AM
Awk to find space and tab. pinnacle Shell Programming and Scripting 5 06-03-2009 01:17 PM
splitting tab-delimited file with awk perkele Shell Programming and Scripting 4 03-11-2009 11:48 AM