Visit The New, Modern Unix Linux Community


awk with tab delimeter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with tab delimeter
# 1  
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  
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  
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  
@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:
# 5  
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  
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:
# 7  
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; 01-10-2019 at 07:59 PM..
These 2 Users Gave Thanks to durden_tyler For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #387
Difficulty: Medium
The Unix apropos utility locates commands using a neural network.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output seperated by tab

I am just trying to output the below awk separated by tabs. Thank you :). awk (added OFS as an attempt to itroduce tabs) awk '{split($5,a,"-"); OFS='\t' print $1,$2,$3,a}' file.bed > test.bed The awk runs and produces all the data in 1 field instead of 4 fields. current output ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk substr delimeter

Hi All, I have an awk command that uses the substr function - At the moment I know the length of the values so can use the example below i.e substr(i,0,1) However in future these lengths may change so wondered if you can use a delimiter within a substr? Like in bash you could use cut -d ';',... (6 Replies)
Discussion started by: Ads89
6 Replies

3. Shell Programming and Scripting

Replace delimeter between quotes with \| using sed or awk

I've a pipe delimited data in a file of size 3GB. if the text data conatins pipe delimiter that will be enclose with double quotes. I have to replace delimiter which exists between double quotes with #%@#%@#%@ using awk or sed. can some one provide a better and efficient solution to me. The below... (4 Replies)
Discussion started by: BrahmaNaiduA
4 Replies

4. Shell Programming and Scripting

Replace space and tab to pipe delimeter

I have file like below abc 12 34 45 code abcdef 451 1 4 code ghtyggg 4 56 3 code I need to change this to abc|12|34|45|code| abcdef|451|1|4|code| ghtyggg|4|56|3|code| I tried replace space with | in sed ... but in the middle some row has... (7 Replies)
Discussion started by: greenworld123
7 Replies

5. Shell Programming and Scripting

Replacing the delimeter with other delimeter

Hi Friends, I have a file1.txt as below 291239732012-0529353101240000000000047620001020390F 29123974201205293531012500000000000467700001020390F 29123975201205293531012600000000000468040001020390F 29123976201205293531012700000000000448200001020390F i have a file2.txt... (4 Replies)
Discussion started by: i150371485
4 Replies

6. Shell Programming and Scripting

awk: string followed by tab comparision

Hi all, Currently i am using if( $0~/ NOT / && $0~/ NULL /) { ................. } to check if the input record contains "NOT" and "NULL". But in some cases "NOT" and "NULL" are preceded and followed by tab. How do i find compare for these fields as well? (3 Replies)
Discussion started by: ysvsr1
3 Replies

7. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

8. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

9. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

10. Shell Programming and Scripting

Awk to find space and tab.

Wants to print line when there exist leading or trailing space or tab in fields 2,3 and 5 The below code prints all lines in file even if they dont have leading and trailing space or tab. nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^*" || $i ~ "*$")}}1' file file Ouput required: ... (5 Replies)
Discussion started by: pinnacle
5 Replies

Featured Tech Videos