LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-25-2022, 07:00 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Rep: Reputation: Disabled
awk if statement with a string gives empty file


hi guys,

i've got an awk script that has a string as part of it's if statement. the script runs but gives me an empty output file.

here's an example of the data file
Code:
123, 54.1, abc-1, 100
123, 26.7, abc-1, 99
123, 31.8, def-2, 99
678, 59.7, ghi-2, 100
123, 22.1, abc-1, 100
so if i write the code as
Code:
{
if($1==123 && $4==99)
  print $2,$3
}
i get a file with
Code:
26.7, abc-1
31.8, def-2
i tried to write the code as
Code:
{
if($1==123 && $4==99 && $3=="abc-1")
  print $2,$3
}
but i get back an empty file. i was expecting to get back
Code:
26.7, abc-1
i've searched the net looking for what is wrong with my syntax, but everything i've found so far says i'm doing it right? it's probably something really simple

Last edited by atjurhs; 05-25-2022 at 07:10 AM.
 
Old 05-25-2022, 07:18 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,310
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
AWK syntax allows you to put the conditional at the beginning of the clause:

Code:
awk '$1==123 &&  $3=="abc-1" && $4==99 { print $2, $3; }' < infile > outfile
Otherwise, you might try using curly braces for clarity:

Code:
awk '{ if ($1==123 &&  $3=="abc-1" && $4==99) { print $2, $3; } }' < infile > outfile
 
Old 05-25-2022, 07:31 AM   #3
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
Turbocapitalist, that did not work, i got the same result, empty files.
 
Old 05-25-2022, 07:36 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
It looks like a space problem, with numbers it does not matter since they are basically ignored. Strings not so much.

Code:
'$1==123 && $3==" abc-1" && $4==99 {print $2, $3;}'

Last edited by michaelk; 05-25-2022 at 07:37 AM.
 
Old 05-25-2022, 07:56 AM   #5
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
yea michaelk that didn't work out either
 
Old 05-25-2022, 08:05 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Here is my test case using your data.
Code:
123, 54.1, abc-1, 100
123, 26.7, abc-1, 99
123, 31.8, def-2, 99
678, 59.7, ghi-2, 100
123, 22.1, abc-1, 100

#!/bin/bash

awk -F , '$1==123 && $3==" abc-1" && $4==99 {print $2, $3;}' test.txt

 26.7 abc-1
Is there any other information that you have not provided?

Last edited by michaelk; 05-25-2022 at 08:12 AM.
 
Old 05-25-2022, 08:14 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
regex anyone ?.
 
Old 05-25-2022, 08:36 AM   #8
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
Code:
#! /bin/bash/awk -f
for file in *test.csv;
do
awk 'BEGIN {FS=OFS=","}
NR==1 {print  "Double", "String"}
{ 
  a bunch of awk scripting stuff
{
if($1==123 && $3==" abc-1" && $4==99)
  {print $2,$3;}
}
}' "$file" > out_"$file"
done
it runs if i don't have the $3==" abc-1" condition, it just doesn't give me what i need

Last edited by atjurhs; 05-25-2022 at 08:40 AM.
 
Old 05-25-2022, 08:46 AM   #9
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
michaelk, your command gives me back an empty file ???
 
Old 05-25-2022, 09:54 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So just to be clear, you ran michaelk's exact code and got an empty file or you put it into your script?
Code:
$ awk -F , '$1==123 && $3==" abc-1" && $4==99 {print $2, $3;}' test.txt
 26.7  abc-1
His code works as described for me.

awk on your system is a symbolic link to gawk or a different type of awk?

This would be my alternate so as to ignore the spaces:
Code:
$ awk 'BEGIN{FS="[ ,]+";OFS=","}$1==123 && $3=="abc-1" && $4==99 {print $2, $3;}' f1
26.7,abc-1
 
Old 05-25-2022, 09:57 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Or you might be forced to use regex.

Last edited by michaelk; 05-25-2022 at 09:58 AM.
 
Old 05-25-2022, 10:26 AM   #12
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
i don't know regex
 
Old 05-25-2022, 10:55 PM   #13
cormacj
LQ Newbie
 
Registered: May 2022
Posts: 1

Rep: Reputation: 1
The problem is that awk by default treats spaces as field separators, but your data is CSV.

The fix is to pass -F ", " when running awk to tell it to use comma and space as a separator so that it will treat the data correctly.

Quote:
~/ cat awkt.txt
{
if($1==123 && $4==99 && $3=="abc-1")
print $2,$3
}

~/ cat inp.txt |awk -f awkt.txt
~/ cat inp.txt |awk -F ", " -f awkt.txt
26.7 abc-1
As to why your first script worked, well it was treating the fields as numbers and dropping the comma. This caused it to work.

Last edited by cormacj; 05-25-2022 at 11:13 PM.
 
1 members found this post helpful.
Old 05-26-2022, 01:20 AM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
That's fragile in that more blanks may appear - this would be my preference.
Code:
 awk -F, '{if ($1==123 && $4==99 && $3 ~ /abc-1/)   print $2,$3}' infile.txt

Last edited by syg00; 05-26-2022 at 02:16 AM. Reason: full statement including separator
 
Old 05-26-2022, 05:11 AM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
$3 ~ /abc-1/ is not good, causes another problem:
It matches xabc-1 and abc-1x

Better have a precise FS - as others stated already.
Code:
BEGIN { FS=", " }
# a comma followed by a space
Code:
BEGIN { FS=",[ \t]+" }
# a comma followed by a whitespace
This also fixes the cast to a number. There might be awk versions that cast 123, to a string "123,"
There is no standard how the number detection should work.

Last but not least, some awk versions treat
awk -F", " like
awk -F"[, ]"
(A misfeature IMHO.)
So I only recommend -F with one character, and use a BEGIN section for setting a more complex FS
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
To Substitute Various String Combinations with an Empty String: Sed, Awk...? charless Linux - General 1 07-17-2019 12:43 PM
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 12:30 PM
[SOLVED] awk: print statement adds one additional empty line to the output Reuti Programming 4 07-30-2015 04:58 AM
Scripting Help--Check empty string condition (not null, but empty!) sungchoiok Linux - Newbie 4 01-01-2012 03:46 PM
[SOLVED] replace string with empty string of dir path ted_chou12 Linux - Newbie 3 04-02-2011 06:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration