ksh to find specific infomation in a delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh to find specific infomation in a delimited file
# 1  
Old 12-19-2013
ksh to find specific infomation in a delimited file

I am using ksh and looking for a simple way to print the entire row when a specific column contains specific infomation. I know I can use grep to find the information however I can not specify the column.

File test.txt contents:
Abc,223,223,223
efg,354,224,774
hij,354,2230,773
klm,154,223,775

I would like to list only all of the lines where the 3rd delimted filed contains the number 223:
Abc,223,223,223
klm,154,223,775

Does anyone have an idea on what I can use for this maybe awk?
# 2  
Old 12-19-2013
No need to use awk. Define IFS and read each fields into separate variables and check 3rd field variable value:
Code:
#!/bin/ksh

while IFS=, read f1 f2 f3 f4
do
        [ $f3 -eq 223 ] && print "${f1},${f2},${f3},${f4}"
done < test.txt

# 3  
Old 12-19-2013
Quote:
Originally Posted by oldman2
I would like to list only all of the lines where the 3rd delimted filed contains the number 223:
You can use "grep" without problems, you just have to construe your regexp accordingly:

Code:
grep '^[^,]*,[^,]*,223,' /path/to/input

searches for "line start, followed by any number of non-',' followed by a ',' (=field1), followed by any number of non-',' followed by a ',' (=field2), followed by '223,' (=field3).

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 12-19-2013
Quote:
Originally Posted by bakunin
You can use "grep" without problems, you just have to construe your regexp accordingly:

Code:
grep  /path/to/input

searches for "line start, followed by any number of non-',' followed by a ',' (=field1), followed by any number of non-',' followed by a ',' (=field2), followed by '223,' (=field3).

I hope this helps.

bakunin
bakunin, that works great! I never knew you could do that with grep. Any ideas how I could pull out infomation with both 223 or 224 or maybe another number? maybe like an egrep "'^[^,]*,[^,]*,223,'|'^[^,]*,[^,]*,224'"?
# 5  
Old 12-19-2013
You can try this to capture multiple values:

Code:
grep -E '^[^,]*,[^,]*,(223|224),'

# 6  
Old 12-19-2013
Quote:
Originally Posted by in2nix4life
You can try this to capture multiple values:

Code:
grep -E '^[^,]*,[^,]*,(223|224),'

This is true but in fact it is even easier, when values are adjacent. You won't even need extended regexps for this:

Code:
grep  '^[^,]*,[^,]*,22[34],'

search for '...' followed by "22", followed by either a "3" or a "4".

Such "character ranges" can do many things, here are a few examples:

"[a-z]" search for a single lowercase character

"[0-9]" search for a single digit number

"[0-9]*[02468]" search for an even number

"+*[0-9][0-9]*[^.]*" search for a positive integer (an optional plus-sign, followed by a non-zero number of numeric digits, followed by anything except a comma)

I hope this helps.

bakunin

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a content in a file with specific interval base on the delimited values using UNIX command

Hi All, we have a requirement to split a content in a text file every 5 rows and write in a new file . conditions: if 5th line falls between center of the statement . it should look upto after ";" files are below format: 1 UPDATE TABLE TEST1 SET VALUE ='AFDASDFAS' 2 WHERE... (3 Replies)
Discussion started by: KK230689
3 Replies

2. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

3. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

4. UNIX for Dummies Questions & Answers

Need to modify a delimited file using UNIX commands. Please find description

i have a '|' delimited file having 4 fields. now i want to sort the data by combination of first three fields without changing order of 4th field. input file looks like this: 3245|G|kop|45 1329|A|uty|76 9878|K|wer|12 3245|G|kop|15 1329|A|uty|56 9878|K|wer|2 3245|G|kop|105... (4 Replies)
Discussion started by: ankurgoyal2408
4 Replies

5. UNIX for Dummies Questions & Answers

find string and get the rest of the line in a pipe delimited file

Hi friends, I have a file where I should search for a string and get the rest of the line but without the delimiter using awk. for example I have the series of string in a file: input_string.txt bbb ccc aaa and the mapping file looks like this. mapping.txt aaa|12 bbb|23 ccc|43... (11 Replies)
Discussion started by: kokoro
11 Replies

6. UNIX for Dummies Questions & Answers

Adding tags to a specific column of a space delimited text file

I have a space delimited text file with two columns. I would like to add NA to the first column of the text file. Input: 19625 10.4791768259 19700 10.8146489183 19701 10.9084026759 19702 10.9861346978 19703 10.9304364984 Output: NA19625 10.4791768259 NA19700 10.8146489183... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

8. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

9. UNIX for Dummies Questions & Answers

How do you view specific columns from a space delimited text file?

I have a space delimited text file with 1,000,000+ columns? I would only like to view specific ones (let's say through 1:10), how can I do that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

10. UNIX for Dummies Questions & Answers

Deleting cells that contain a specific number only from a space delimited text file

I have this space delimited large text file with more than 1,000,000+ columns and about 100 rows. I want to delete all the cells that consist of just 2 (leave 2's that are not by themselves intact): File before modification aa bb cc 2 NA100 dd aa b1 c2 2 NA102 de File after modification... (1 Reply)
Discussion started by: evelibertine
1 Replies
Login or Register to Ask a Question