Capture specific fields in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture specific fields in file
# 1  
Old 10-25-2012
Question Capture specific fields in file

Dear Friends,

I have a file

a.txt

Code:
1|3478.12|487|4578.04|4505.5478|rhfj|rehtire|rhj

I want to get the field numbers which have decimal values

output:

Code:
Fields: 2,4,5


Plz help
# 2  
Old 10-25-2012
Code:
awk -F "|" '{ for(i=1;i<=NF;i++){if($i ~ /[0-9]\.[0-9]/){s=s?s","i:i}}print s;s=""}' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 10-25-2012
Code:
perl -F'[|]' -ane 'print "Fields with periods: ";$n=0;
for (@F){$n++;print "$n " if /\d[.]\d/};print "\n"' file

# 4  
Old 10-25-2012
Quote:
s=s?s","i:i}}print s;s=""}'
Thanks for the reply pamu. Could you please explain the above part ?
# 5  
Old 10-25-2012
Quote:
Originally Posted by i150371485
Could you please explain the above part ?
s=s?s","i:i # Here we assign values to s. First here it checks s presents or not

s? --> Is s present? If present then do the true part else do the false part. see below.

Code:
s?True:False

Assume at start s="" so s? is false because s is not present. so it assing i to s.
Next time s? is true so it appends ","i to the s.

print s;s=""}' # Print s and set s="" means NULL.

Hope this helps youSmilie
This User Gave Thanks to pamu For This Post:
# 6  
Old 10-25-2012
Simply superb Pamu.. Thank you so much Smilie

@Elixir: Thanks for the reply. It works as Expected.Smilie
# 7  
Old 10-25-2012
May be it will be simple
Code:
grep '[0-9]\.[0-9]' test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting specific fields from an XML file

Hello All, I have a requirement to split the input.xml file different files and i have tried using earlier examples(where i have posted in the forum), but still no luck Here is my input.xml <jms-system-resource> <name>UMSJMSSystemResource</name> ... (4 Replies)
Discussion started by: Siv51427882
4 Replies

2. Shell Programming and Scripting

Using to perl to output specific fields to one file

Trying to use perl to output specific fields from all text files in a directory to one new file. Each text file on a new line. The below seems to work for one text file but not more. Thank you :). perl -ne 's/^#//; @n = (6, 7, 8, 16); print if $. ~~ @n' *.txt > out.txt format of all text... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Parse file for fields and specific text

I have a file of ~500,000 entries in the following: file.txt chr1 11868 12227 ENSG00000223972.5 . + HAVANA exon . gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type... (17 Replies)
Discussion started by: cmccabe
17 Replies

4. UNIX for Dummies Questions & Answers

Read the file and generate specific fields by awk

Hi I need to generate these output file from the below input file. Output : customer_id as customer, zip as zip_cd, catg_cd as catg, Input: out.customer::in.customer_id; out.zip_cd::in.zip; out.catg::in.catg_cd; Could you please help me on this. Please use code tags next... (1 Reply)
Discussion started by: Murugesh
1 Replies

5. AIX

Capture whoami log on a specific command

// AIX 6.1 TL8 Please advise on how to capture whoami log or the user and time info into a log file (i.e. /tmp/cmdcapture.log) whenever users are executing a certain command(s) so that I can keep the single log history (for all users) of who did what. The command(s) I need to monitor are a... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

6. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

7. Shell Programming and Scripting

How to fetch specific fields

Dear Friends, Please provide some commands to fecth specific filed (data yellow color) from below data.. Input data 2648: 1;20120707;3591|4;20290107;90|5;20290107;3|9;20120705;0|10;20120705;0|16;20290113;15|29;20120705;0 2658: 1;20120722;0|4;20290422;1200|9;20120705;0|10;20120705;0 2646:... (4 Replies)
Discussion started by: suresh3566
4 Replies

8. Shell Programming and Scripting

selecting specific fields in a file (maybe with sed?)

Hi, I have a file with following lines: chr1 10 AC=2;AF=1.00;AN=2;DP=2;Dels=0.00;HRun=0;HaplotypeScore=0.00;MQ=23.00;MQ0=0;QD=14.33;SB=-10.01 chrX 18 AB=0.52;AC=1;AF=0.50;AN=2;DP=203;DS;Dels=0.00;HRun=0;HaplotypeScore=20.01;MQ=15.63;MQ0=85;QD=12.80;SB=-1289.58 I need to extract 4... (2 Replies)
Discussion started by: menenuh
2 Replies

9. Shell Programming and Scripting

capture specific listing in the ls -l

Hi, my desired output needs only that has *.pdf ls -l *.pdf error: arg list too long so I have redirected ls -l > file.txt therefore, I get the following details in the file -rw-r--r-- 1 devusr devgrp 848015 Aug 11 15:13 0000795769.pdf -rw-r--r-- 1 devusr devgrp ... (3 Replies)
Discussion started by: techmoris
3 Replies

10. UNIX for Dummies Questions & Answers

Cut specific fields from a file containing multiline records

Hi, I am looking for a method to get column13 to column 50 data from the 1st line of a multiline reord. The records are stored in a large file and are separated by newline. sample format is (data in red is to be extracted) <header> A001dfhskhfkdsh hajfhksdhfjh... (3 Replies)
Discussion started by: sunayana3112
3 Replies
Login or Register to Ask a Question