how to extract the data ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to extract the data ?
# 1  
Old 06-16-2009
how to extract the data ?

Hi,

I'm trying to pick out a data field eg. from below. I need the required field as below but they are filled sometimes with weird chars like \-(. or watever. How can I accurately extract the 3rd field in shell? Smilie

ID IDNO - REQUIRED FIELD

ID 1447 - MAT620BR.
ID 1452 - FGI-LOM3100R \ LOM FGI REPORT (.
ID 1453 - FGI-LOM3101R \ LOM FGI REPORT (.
ID 1512 - SAM05TRR.
ID 1514 - SAM6220R.
ID 1515 - SAM07R.
ID 1516 - SAM07R00.
ID 1517 - SAM10R.
ID 1518 - SAM10R00.
ID 1521 - SAM13R.
ID 1536 - MONJ001R.
ID 1537 - MONJ004R.
ID 1541 - FROLPS.
ID 1542 - FROAPD.
ID 1548 - MOS5610R.
ID 1550 - C009LP \ DAILY INVOICE.
ID 1554 - SAM49R.
ID 1559 - MAT310AR.
# 2  
Old 06-16-2009
You have various ways to extract lines from a text.

You could use head/tail
Code:
head -n 3 filename| tail -n 1

You can use sed
Code:
sed -n '3p' filename

You can use awk
Code:
awk 'NR == 3 {print}' filename

Etc...

To remove those chars, you can use tr
Code:
 redoubtable@Tsunami ~ $ awk 'NR == 3 {print}' filename|tr '.(\\' '\000'
ID 1453 - FGI-LOM3101R  LOM FGI REPORT 
redoubtable@Tsunami ~ $

# 3  
Old 06-16-2009
Code:
awk -F"-" '{print $NF}' file

# 4  
Old 06-16-2009
Can you show us what have you tried so far and where you are stuck?

Regards
# 5  
Old 06-16-2009
Quote:
Originally Posted by ghostdog74
Code:
awk -F"-" '{print $NF}' file


Hi Ghost,



I was checking the recommendation you gave. But there was a problem.The output with your recommendation gave:



LOM3100R \ LOM FGI REPORT (.



The correct output should be:



FGI-LOM3100R \ LOM FGI REPORT (



without the fullstop but includes the FGI-



ID 1452 - FGI-LOM3100R \ LOM FGI REPORT (.

ID 1453 - FGI-LOM3101R \ LOM FGI REPORT (.

-----Post Update-----

Quote:
Originally Posted by Franklin52
Can you show us what have you tried so far and where you are stuck?

Regards
Hi Franklin,

i've tried basic awk and cut to print 3rd field but they do not work. i used spaces and - as delimiters but they do not give the full output i require

Last edited by uxnoob; 06-16-2009 at 10:58 AM..
# 6  
Old 06-16-2009
Code:
awk -F" - " '{print $NF}' file

# 7  
Old 06-16-2009
Quote:
Originally Posted by ghostdog74
Code:
awk -F" - " '{print $NF}' file


Are there better ways of making the extraction more accurate? it works with the " - " now, however, there could still be mistakes if a field would contain something like below.

FGI-LOM3100R - LOM FGI REPORT (.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract header data from one file and combine it with data from another file

Hi, Great minds, I have some files, in fact header files, of CTD profiler, I tried a lot C programming, could not get output as I was expected, because my programming skills are very poor, finally, joined unix forum with the hope that, I may get what I want, from you people, Here I have attached... (17 Replies)
Discussion started by: nex_asp
17 Replies

2. Shell Programming and Scripting

Help with File Data Extract

Hello, Hope you are doing fine. I have been struggling with it for some time now and I would really appreciate your help. Following is file format: Currency,Name,Date, Term USD, ABC, 2011/11/11, T0, S1, S2, S3, S4 , , ,T1, 5.6, 2.3, 6.5, 4.5 , ... (5 Replies)
Discussion started by: srattani
5 Replies

3. Shell Programming and Scripting

help with data extract script

hi, i'am trying to write a script which takes a data element from one file and searches in a certain file path and after it finds it it redirects the file names to a new file...Now i have to pick the up the last modified file's Timestamp(MMDDYY) from the files collected for each data element. ... (2 Replies)
Discussion started by: kmr023
2 Replies

4. UNIX for Dummies Questions & Answers

extract lines from data

I have a file that looks like this: >cel-miR-35 MIMAT6 C eles le UCACCGGGUGGAAACUAGCAGU >hsa-let-7a MI062 H sa UGAGGUAGUAGGUUGUAUAGUU >cel-miR-36 M007 Ca ele UCACCGGGUGAAAAUUCGCAUG >hsa-let-7b MI63 Hmo sns le UGAGGUAGUAGGUUGUGUGGUUI would like to extract all the lines that start with... (7 Replies)
Discussion started by: jdhahbi
7 Replies

5. Shell Programming and Scripting

XML data extract

Hi all, I have the following xml document : <HEADER><El1>asdf</El1> <El2>3</El2> <El3>asad</El3> <El4>asasdf</El4> <El5>asdf</El5> <El6>asdf</El6> <El7>asdf</El7> <El8>A</El8> <El9>0</El9> <El10>75291028141917</El10> <El11>asdf</El11> <El12>sdf</El12> <El13>er</El13> <El14><El15>asdf... (1 Reply)
Discussion started by: nthed
1 Replies

6. Shell Programming and Scripting

Extract specific data content from a long list of data

My input: Data name: ABC001 Data length: 1000 Detail info Data Direction Start_time End_time Length 1 forward 10 100 90 1 forward 15 200 185 2 reverse 50 500 450 Data name: XFG110 Data length: 100 Detail info Data Direction Start_time End_time Length 1 forward 50 100 50 ... (11 Replies)
Discussion started by: patrick87
11 Replies

7. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

8. Shell Programming and Scripting

extract data from a data matrix with filter criteria

Here is what old matrix look like, IDs X1 X2 Y1 Y2 10914061 -0.364613333 -0.362922333 0.001691 -0.450094667 10855062 0.845956333 0.860396667 0.014440333 1.483899333... (7 Replies)
Discussion started by: ssshen
7 Replies

9. Shell Programming and Scripting

extract data from file

I m new to shell scripting & i need a help.... i have file like.... Name := sachin address:=something phone:=111 ... Note: There might be or not space between Name & := and between := & sachin. I need to extract the data from each line of file as var1=Name value1=sachin same for... (13 Replies)
Discussion started by: ps_sach
13 Replies

10. UNIX for Dummies Questions & Answers

Extract data correction???

Hi I have file "test.txt" i need to extract the data as below from this file. /home/cvs/gkc ->awk -v RS= '{print $(NF-2),$(NF-1),$NF}' test.txt Buildfile: build.xml Buildfile: build.xml Created dir: /dist/advance/2.0.4/20060926-0413 awk: The field -1 must be in the range 0 to 199. ... (6 Replies)
Discussion started by: gkrishnag
6 Replies
Login or Register to Ask a Question