Deletion of strings depending of the value in a specific column


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Deletion of strings depending of the value in a specific column
# 1  
Old 01-05-2017
Deletion of strings depending of the value in a specific column

Happy new year guys!

I have a new question for you!

Code:
Ubuntum, Bash version: 4.3.46 Bash

I have a csv file, composed from several columns.

Code:
 INPUT
x1 x2 x3 x4 x5
as 10 32 T 3
sd 50 7 B 48
af 18 98 D 25
fe 75 55 P 15

I want to cancel the strings where the x2 and/or x3 values are <=10 ...

Code:
 OUTPUT
x1 x2 x3 x4 x5
af 18 98 D 25
fe 75 55 P 15

I tried to use grep ... but it isn't a safe way because I can't specify at the system that it have to read the values from the columns x2 and x3 ...

I also found this code
Code:
awk -F, '{if ($2 == "10") print $0;}' inputfileX.csv > outputfileX2.csv
awk -F, '{if ($3 == "10") print $0;}' inputfileX.csv > outputfileX3.csv

... and I think it works... this codes save my strings with x2 = 10 or x3 =10... They are to indipendente conditions

How can I merge those codes to one string? in the way to have two conditions in the same code?


Best regards!!!

Last edited by echo manolis; 01-05-2017 at 12:35 PM..
# 2  
Old 01-05-2017
Code:
awk '$2>10 && $3>10' myFile

# 3  
Old 01-05-2017
With awk you can access the fields
Code:
awk 'NR==1 || $2<=10 || $3<=10' inputfile
awk 'NR==1 || !($2<=10 || $3<=10)' inputfile

NR is line number
|| is OR
! is NOT
The default action is {print}
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 01-05-2017
Many thanks guys! Awk seems too easy to use... For now i use only some bash commands...
# 5  
Old 01-05-2017
Please be aware that there can be a serious difference between AND & OR when you specify "where the x2 and/or x3 values" ... assuming OR for now. And, you seem to want to specify the columns by their header value, here x2 and x3.
Try:

Code:
awk -vDISCR="x2=10;x3=10" '
NR == 1 {for (n = split (DISCR, TMP1, ";"); n>0; n--)   {split (TMP1[n], TMP2, "=")
                                                         TMP3[TMP2[1]] = TMP2[2]
                                                        }
         for (i=1; i<=NF; i++) if ($i in TMP3) THRSH[i] = TMP3[$i]
        }

        {P = 1
         for (i in THRSH) if ($i <= THRSH[i]) P = 0
        }

P

' file
x1 x2 x3 x4 x5
af 18 98 D 25
fe 75 55 P 15

This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-05-2017
Here is one with bash builtins:
Code:
while read x1 x2 x3 x4 x5 junk
do
  if ! { [[ $x2$x3 != *[!0-9]* ]]  &&
    { [[ $x2 -le 10 ]] || [[ $x3 -le 10 ]]
    }
  }
  then
    printf "%s\n" "$x1 $x2 $x3 $x4 $x5"
  fi
done < inputfile

# 7  
Old 01-06-2017
OK!!!

Post solved
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

2. Shell Programming and Scripting

Three month old specific files deletion

Hi, I need to delete 3 month old files in my logpath. This path contains several logs and other important files The file names are be like this sl_details.env tomcatfiles_03062014.log application_zur_03.062014.log I need to delete only tomcatfiles logs. I wrote this command. can... (7 Replies)
Discussion started by: nag_sathi
7 Replies

3. Shell Programming and Scripting

Split certain strings in a line for a specific column.

Hi, i need help to extract certain strings/words from lines with different length. I have 3 columns separated by tab delimiter. like below Probable arabinan endo-1,5-alpha-L-arabinosidase A (EC 3.2.1.99) (Endo-1,5-alpha-L-arabinanase A) (ABN A) abnA Ady3G14620 Probable arabinan... (5 Replies)
Discussion started by: redse171
5 Replies

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

5. Shell Programming and Scripting

Split file depending on Column Value

Hi , my file look likes below , cat file.csv 12/09/2014,50,5,0,300 12/09/2014, ,5,0,300 12/09/2014,50,,,300 i need to split file , the first one contains values (2nd column is 50 , 3rd and fourth column is null ) the second file contains all others firstfile ... (2 Replies)
Discussion started by: ubaisalih
2 Replies

6. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

7. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

8. Shell Programming and Scripting

Add depending on the column

Hi all, If column 6 is negative then I want column 3 is be subtracted by 150. If column 6 is positive then I want 150 added to the value at column 2. The file that looks like this: Bull 38 158 HWI-ST600:206:D0L90ACXX:8:2214:15503:17988 0 -... (2 Replies)
Discussion started by: phil_heath
2 Replies

9. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

10. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies
Login or Register to Ask a Question