Grab from file with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab from file with sed
# 1  
Old 01-18-2013
Grab from file with sed

Hello All

I have a file with this type of records:

Code:
=LDR  01157nas a22003011a 4500
=001  vtls000000013
=003  VRT
=005  20111020150800.0
=008  100128c19699999sp\a|||||\||||0\\\||spa|
=037  \\$a1327$i090$j090$k03
=039  \9$a201110201508$bstaff$c201001280942$dstaff$c200910281236$dstaff$c200906301240$dstaff$y2000092208370000$zload
=040  \\$aES-Ba-GIE$bcat$cEs-Ba-GIE
=025  \\$a14
=590  \\$aEn Curs
=027  \\$aAnuari

What I would like to do is just get the records where the field =590 is eq to \\$aEn Curs
as in the exemple, and from this condition get an other filed and put it into a csv file..for exemple I need the =001 field and the =025

=001 vtls000000013

So the csv should be:
Code:
vtls000000013;14

Is it possible to do with sed? I try the first filter with this, but I have errors:

Code:
sed -i '/590$a En curs/ p/' bib.txt

Someone could help?? Cheers
# 2  
Old 01-18-2013
Here is a solution using awk:
Code:
awk '/^=590/{ L=$0; gsub(/=590  /,"",L); print L; }' filename
\\$aEn Curs

Modify as per your requirement.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-18-2013
hello
Thanks for you reply.
Look the error I have, I'm trying your script with WINDOWS (I know).
Later I'll test on my linux:

Code:
C:\>awk '/^=590/{ L=$0; gsub(/=590  /,"",L); print L; }' bib.txt
awk: '/=590/{
awk: ^ invalid char ''' in expression

And I have a question, I do not see in the code the part where I must put the 2 field to join... In my exemple it was =025
Thanks
# 4  
Old 01-18-2013
sed? Try:
Code:
sed -n '/^=001/{s/.* //;h;}; /^=025/{s/.*\$//; H;}; /^=590.*\$aEn Curs/{g;s/\n/;/g;p;}' infile

awk:
Code:
awk '/^=001/{p=$2} /^=025/{sub(/.*\$a/,";",$2); p=p $2} /^=590.*\$aEn Curs/{print p}' infile

I believe awk on windows works with double quotes instead of single ones, but I am not sure, you'd have to search the forums..

Last edited by Scrutinizer; 01-18-2013 at 12:35 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-18-2013
Whassshh yes it works.
Many thanks,,, But how does it work?
I see 3 parts in you script.
the 2 first are the field I want to catch and join...ok and the last one is the filter condition. But I'm not abble to see more Smilie

But .. it's ok I can use this script ..If I want to catch other field I just have to change the code I guess, I'll try

Thans and have a nice week end

---------- Post updated at 11:38 AM ---------- Previous update was at 11:35 AM ----------

Hi,
I change single quote with " and the script works:
Code:
awk "/^=590/{ L=$0; gsub(/=590  /,\"\",L); print L; }" bib.txt

I have the fine script now with sed command !!
Thanks a lot

Last edited by Scrutinizer; 01-18-2013 at 12:45 PM.. Reason: code tags
# 6  
Old 01-18-2013
Regarding the sed script: The first is put in the hold buffer (h) and the second is appended to the hold buffer (H). It the 590 field is correct the hold buffer is retrieved (g), the linefeed (\n) is replaced by a semicolon and the result is printed..

I hope this helps..
# 7  
Old 01-19-2013
Hello
yes it helps...Thanks
But now I have other dubt, imagine I would like to add one or more fields, for exemple this ones:
Code:
=003  VRT
=005  20111020150800.0
=008  100128c19699999sp\a|||||\||||0\\\||spa|

So the =003, =005 and =008
Can I just adding this into the sed command?
Like this with =003 ?
Code:
sed -n '/^=001/{s/.* //;h;}; /^=003/{s/.*\$//; /^=025/{s/.*\$//; H;}; /^=590.*\$aEn Curs/{g;s/\n/;/g;p;}' bib.mrk

Thanks
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