Grab from file with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab from file with sed
# 8  
Old 01-19-2013
Try:
Code:
sed -n '/^=001/{s/.* //;h;}; /^=003/{s/.* //;H;}; /^=025/{s/.*\$//;H;}; /^=590.*\$aEn Curs/{g;s/\n/;/g;p;}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 01-19-2013
Ok Great !!!
Thanks a lot, now I see the sctructure better.
I'll look for some tutorial in youtube in order to get it better.

Have a nive week end !!

PD: If you need some help with oracle database, just ask !
Cheers

---------- Post updated at 09:34 AM ---------- Previous update was at 06:00 AM ----------

Hello

looking the result more carefully I see that the field 310 is never present in the output file. Look the exemple:

Script executed:
Code:
sed -n '/^=001/{s/.* //;h;}; /^=310/{s/.* //;H;}; /^=037/{s/.* //;H;}; /^=050/{s/.* //;H;}; /^=099/{s/.*\$//; H;}; /^=590.*\$aEn Curs/{g;s/\n/;/g;p;}' bib.mrk >seriadas.csv

result:
Code:
vtls000000101;\\$a1570$i090$j090$k03;\\$a1570$i090$j090$k03;\\$a951$i090$j090$k03

source file:
Code:
=001  vtls000000101
=037  \\$a1570$i090$j090$k03
=037  \\$a1570$i090$j090$k03
=037  \\$a951$i090$j090$k03
=590  \\$aEn Curs
=027  \\$aAnuari
=050  \\$aA-0189
=099  \\$aSALA-17.01(I)
=310  \\$aAnual

As I see in the result is that only the field 037 have been passed, the other not...
The result have to be like:
Code:
vtls000000101;\\$aAnual;\\$a1570$i090$j090$k03-\\$a1570$i090$j090$k03-\\$a951$i090$j090$k03;\\$aA-0189;\\$aSALA-17.01(I)

Maybe one little detail to adapt no?
Thanks !
# 10  
Old 01-19-2013
That has to do with the changed order of sample fields. This gets a bit too complicated for sed, perhaps you could try if awk might be a better choice:
Code:
awk '
  {
    i=$1
    sub(i " *",x)
    A[i]=A[i](A[i]?"-":x) $0
  } 
  i=="=310"{
    if (A["=590"]~/\$aEn Curs/) print A["=001"],A["=310"],A["=037"],A["=050"],A["=099"]
    for(i in A) delete A[i]
  }
' OFS=\; file

# 11  
Old 01-19-2013
Ok,
I try and this is the result:

good
Code:
vtls000000013;\\$aAnual;\\$a1327$i090$j090$k03;\\$aG-0644;\\$aSALA-14.04(E)
vtls000000017;\\$aAnual;\\$a1465$i090$j090$k03-\\$a46$i090$j090$k03;\\$aG-0022;\\$aSALA-11.00(E)
vtls000000021;\\$aAnual;\\$a1196$i090$j090$k03-\\$a1196$i090$j090$k03;\\$aG-0541;\\$aDIPĂSIT
vtls000000028;\\$aAnual;\\$a1156$i090$j090$k03;\\$aG-0949;\\$aDIPĂSIT

and later you have this
not good
Code:
vtls000000167-vtls000000169-vtls000000171-vtls000000174-vtls000026748;\\$aAnual;\\$a1898$i090$j090$k03;\\$aB-0258-\\$aG-0781-\\$aGI-0144-\\$aGI-0160-\\$aA-0791;\\$aDIPĂSIT-\\$aDIPĂSIT-\\$aDIPĂSIT-\\$aDIPĂSIT-\\$aDIPĂSIT
vtls000000176-vtls000000179-vtls000021436-vtls000000202;\\$aAnual;\\$a832$i090$j090$k03-\\$a832$i090$j090$k03-\\$a832$i090$j090$k03;\\$aGI-0180-\\$aGI-0246-\\$aG-0180;\\$aDIPĂSIT-\\$aDIPĂSIT-\\$aSALA-17.01(E)

and then the file become good again.
Strange...
Maybe it's because the source file have some strange caracters.

THanks if you can take a look
I post the complete source file comressed if you want
# 12  
Old 01-19-2013
Your file does not appear to be standard UTF-8. There is a byte order mark and some extra strange characters for example in some of the =099 lines.
This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 01-20-2013
mmmm
so with notepad++ I open the file and "conversion=>utf8" will be enougth?
I try !

---------- Post updated at 12:22 PM ---------- Previous update was at 12:16 PM ----------

I try this way

I remove from you awk script the last ,A["=099"] in order not to evaluate this
But stell same result

Code:
vtls000000094;\\$aAnual;\\$a951$i090$j090$k03;\\$aA-0245
vtls000000167-vtls000000169-vtls000000171-vtls000000174-vtls000026748;\\$aAnual;\\$a1898$i090$j090$k03;\\$aB-0258-\\$aG-0781-\\$aGI-0144-\\$aGI-0160-\\$aA-0791

The test is valid?

---------- Post updated 01-20-13 at 05:02 AM ---------- Previous update was 01-19-13 at 12:22 PM ----------

Hello

ok I found the problem, but I don't know how to fix it with the awk script.
The problem happend when one of the field in the list do NOT exists into the bib.mrk file:
Exemple =310
I can see that adding =310 manually to the bib.mrk file, this record is created properly into the csv output file.
Can you modify the awk script in order to get this probability ?
Some fields in the list can NOT be present.

Thanks
# 14  
Old 01-20-2013
Some thing like this?
Code:
awk '
  function pr(){                                                                         # define the print array elements function
    if (A["=590"]~/\$aEn Curs/) print A["=001"],A["=310"],A["=037"],A["=050"],A["=099"]  # Print the array elements
    for(i in A) delete A[i]                                                              # Delete the array elements
  }
  {
    i=$1                                                                                 # i becomes the index in field $1
    sub(i " *",x)                                                                        # delete the index and spaces following it from the line
    A[i]=A[i](A[i]?"-":x) $0                                                             # add  the line to the array element with index "i" and insert a "-" when there is already an entry present
  } 
  !NF{                                                                                   # if there is an empty line then
    pr()                                                                                 # print array elements
  }
  END{                                                                                   # if there are no more records
    pr()                                                                                 # print array elements  
  }
' OFS=\; bib.mrk                                                                         # set the Output Field Separator to ";"


Last edited by Scrutinizer; 01-20-2013 at 06:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

2. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

3. Shell Programming and Scripting

Read file, grab ip with fail2ban

Solved with iptables. Many thanks... Hello, Objective: What I would like to accomplish is : - To read file1 line by line and search each word in file2. - To grab corresponding ip addresses found in file2 - To send related ip addresses to fail2ban (not iptables) By this way, when I... (5 Replies)
Discussion started by: baris35
5 Replies

4. Shell Programming and Scripting

Grab nth occurence in between two patterns using awk or sed

Hi , I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed ! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2... (8 Replies)
Discussion started by: OTNA
8 Replies

5. Shell Programming and Scripting

Grab 2 pieces of data within a file

I am a newbie and what I have is a captured file of content. I want to be able to grab 2 pieces of data, multiple times and print them to the screen. DataFile owner: locke user: fun data size: 60 location: Anaheim owner: david user: work data size: 80 location: Orange my script... (2 Replies)
Discussion started by: greglocke
2 Replies

6. Shell Programming and Scripting

Help me grab a passage out of this text file?

Hey guys, I need a command that grabs only this part of the .txt file (that is attached), and outputs it to another .txt file with only these contents below. Thanks in advanced brothers: Disk: Local Disk (C:), NTFS Disk Defragmentation Summary Disk Size 230.85 GB Free Space Size... (4 Replies)
Discussion started by: aabbasi
4 Replies

7. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

8. Shell Programming and Scripting

Grab from the file in one command

Dear All, I have a file in which there are 54 fields, i want to grab the all the lines and send to a new file where filed18 has lenght greater than 14. How can i do it without if condition and faster way: currently i am reading file line by line and comparing the length read fileLine... (9 Replies)
Discussion started by: bilalghazi
9 Replies

9. UNIX for Dummies Questions & Answers

search and grab data from a huge file

folks, In my working directory, there a multiple large files which only contain one line in the file. The line is too long to use "grep", so any help? For example, if I want to find if these files contain a string like "93849", what command I should use? Also, there is oder_id number... (1 Reply)
Discussion started by: ting123
1 Replies

10. UNIX for Dummies Questions & Answers

How to Grab the latest file

I have been trying to use the find command to grab the latest file in a directory and move it to another area. I can't seem to get only that file, I end up getting everything for the day. Any ideas? Thank you (1 Reply)
Discussion started by: n9ninchd
1 Replies
Login or Register to Ask a Question