Find odd numbered items from a csv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find odd numbered items from a csv
# 1  
Old 11-22-2014
Find odd numbered items from a csv

Hi,

This question might seem bit confusing. Sorry for that.
I have tried to express myself as clear as possible.

I have a text file in following format:

Code:
abc_456_def,abc_457_def,123_458_def,abc_3_459_def,zbc_123_456_def
ybc_123_457_def,xbc_3_458def,ybc_124_457_def

I want to check the numbers inside text and see if its odd or even and select those items which are odd. The output I intend to get is:

Code:
abc_457_def
abc_3_459_def
ybc_123_457_def
ybc_124_457_def

# 2  
Old 11-23-2014
Awk is awsome, but I'm not very good at it. Here's a quick bash example for small data sets.
Code:
#!/bin/bash
# odd.sh

# join multiple lines & build array
arry=($(cat odddata | tr '\n' ',' | tr ',' '\n'))
for i in ${arry[*]}
do
    num=$( echo "$i" | tr -d '_[:alpha:]' )
    [ $(( $num % 2 )) != 0 ] && echo $i
done

# output
# ------
# abc_457_def
# abc_3_459_def
# ybc_123_457_def
# ybc_124_457_def


Last edited by ongoto; 11-23-2014 at 02:34 AM.. Reason: ++ small data sets
This User Gave Thanks to ongoto For This Post:
# 3  
Old 11-23-2014
Try:

Code:
awk -F, '{for(i=1; i<=NF; i++) if($i~/[13579]_?def$/) print $i}' file

or gawk/mawk:
Code:
gawk -F, '/[13579]_?def$/' RS='[,\n]' file

or if your grep understands the -o option:
Code:
grep -Eo '[^,]*[13579]_?def' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-23-2014
Excellent! Thanks guys..Appreciate your timely help..Keep rocking. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find to delete lines with pattern and even or odd number

In the below directory I am trying to delete all lines with a .bam extention that have the pattern IonCode_ followed by an even number. I am also trying to delete all lines with a .fastq extention that have the pattern IonCode_ followed by an odd number. I was going to use find but can see all... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

3. UNIX for Dummies Questions & Answers

To find similar items in a column

HI, I have a long file which looks like "1xxx_0_1" "1xxx" 500 5 "ABC*3-DEF*3-LL" "2yyy_0_1" "2yyy" 600 10 "ABC*2-DEF*2-LL" "3ddd_0_1" "3ddd" 150 52 "ABC*3-DEF*3-LL" "1xxx_0_1" "1xxx" 500 5 "ABC*3-DEF*3-LL" "2yyy_0_1" "2yyy" 600 10 "ABC*2-DEF*2-LL" ... (3 Replies)
Discussion started by: XXLMMN
3 Replies

4. UNIX for Advanced & Expert Users

find/grep two items and display both

I have a file against which I can grep a string for. I can also check for that string count using wc -l (or grep -c). I need to display the results of both in one output i.e. 'line containing string' and 'count' - what would be the most efficient way of managing this? Thanks in advance. (3 Replies)
Discussion started by: haider1
3 Replies

5. Shell Programming and Scripting

rename numbered files to numbered files with leading zeroes

Hi, I have some hundreds/thousands of files named logX.dat, where X can be any integer, and they are sequential, X ranges between 1 and any number: log1.dat log2.dat log3.dat log6.dat log10.dat ... log6000.dat I would like to rename them to scatter_params_0001.dat... (6 Replies)
Discussion started by: pau
6 Replies

6. UNIX for Advanced & Expert Users

Odd message from find command

Running the find command as: find /abc -follow -ls gives, for some files, the message, which I have never seen before: find: /abc/def/123.txt: No error Does it mean that find found the file with out error. If it found it without error then why did it not output the "ls" particulars as with... (2 Replies)
Discussion started by: twk
2 Replies

7. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies
Login or Register to Ask a Question