Extract fragments from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract fragments from file
# 8  
Old 03-31-2014
To use the variables defined outside awk,
Code:
targetItem=string1
awk -v targetItem="$targetItem" "/measInfo/ || s {s=s?s\"\n\"$0:$0} /<\/measInfo>/{if (s ~ targetItem) print s; s=\"\"}" asd | wc -l

# 9  
Old 03-31-2014
Quote:
Originally Posted by SriniShoo
To use the variables defined outside awk,
Code:
targetItem=string1
awk -v targetItem="$targetItem" "/measInfo/ || s {s=s?s\"\n\"$0:$0} /<\/measInfo>/{if (s ~ targetItem) print s; s=\"\"}" asd | wc -l

You also need to go back to single quotes (or do some more escaping) so you can use awk's interpretation of $0 instead of the shell's interpretation of $0. And, if a line could every be a string just containing one or more zeros, you need to check for an empty string rather than assuming that s will evaluate to a zero value only when s is the empty string. Try:
Code:
targetItem=string1
awk -v targetItem="$targetItem" '
/<measInfo>/ || s != "" {s != "" ? s"\n"$0 : $0}
/<\/measInfo>/{if (s ~ targetItem) print s; s=""}
' asd | wc -l

These 2 Users Gave Thanks to Don Cragun For This Post:
# 10  
Old 03-31-2014
Quote:
Originally Posted by Scrutinizer
You could try:
Code:
awk '... if (s ~ k) ... }'  k="string1" asd ...

--
If there is always an empty line between those xml segments (only then) you could use:
Code:
awk '$0~k' k="string1" RS= asd

Thank you, it worked
# 11  
Old 03-31-2014
@Don, your code is missing s=.
The following is more simple:
Code:
awk -v targetItem="string1" '
s!="" {s=s"\n"$0}
/<measInfo>/ {s=$0}
/<\/measInfo>/ {if (s ~ targetItem) print s; s=""}
' asd

This User Gave Thanks to MadeInGermany For This Post:
# 12  
Old 04-16-2014
I have a new issue on this topic.
I've settled to this awk expression :

Code:
targetFile=log.xml
targetItem="string 1 string2"
awk '/<mi>/ || s {s=s?s"\n"$0:$0} /<\/mi>/{if (s ~ Item) print s; s=""}' Item=$targetItem $targetFile

I have problems in declaring Item variable which takes its value from $targetItem when $targetItem contains spaces. I tried to declare targetItem like this :
Code:
targetItem="string1\ string2"

but it doesn't work.
The awk expression takes "string2" as a value for $targetFile ....


...Nevermind I forgot to use double quotes :
Code:
awk '/<mi>/ || s {s=s?s"\n"$0:$0} /<\/mi>/{if (s ~ Item) print s; s=""}' Item="$targetItem" $targetFile

Applogize for taking your time with this.

Last edited by black_fender; 04-16-2014 at 09:55 AM.. Reason: Posted by mistake
# 13  
Old 04-16-2014
Try:
Code:
awk '....' Item="$targetItem" "$targetFile"

This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 04-16-2014
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk '....' Item="$targetItem" "$targetFile"

Yes, thanks. That was it..
I saw that after I posted...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why the results of these two code fragments are not the same?

Code 1: #!/bin/sh for arg1 in "$@" do counter=0 for arg2 in "$@" do if && then counter=$((counter+1)) continue fi (8 Replies)
Discussion started by: johnprogrammer
8 Replies

2. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

3. Shell Programming and Scripting

How to extract start/end times from log file to CSV file?

Hi, I have a log file (log.txt) that which contains lines of date/time. I need to create a script to extract a CSV file (out.csv) that gets all the sequential times (with only 1 minute difference) together by stating the start time and end time of this period. Sample log file (log.txt) ... (7 Replies)
Discussion started by: Mr.Zizo
7 Replies

4. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

6. IP Networking

Solaris 11 Express NAT/Router IP Fragments

Upon replacing my linux router/server with a Solaris one I've noticed very poor network performance. The server itself has no issues connecting to the net, but clients using the server as a router are getting a lot of IP fragments as indicated from some packet sniffing I conducted. Here was my... (3 Replies)
Discussion started by: vectox
3 Replies

7. Solaris

ipfilter blocking ip fragments

For some reason ipfilter is blocking inbound fragmented ip packets (the packets are larger than the interface's MTU) that are encapsulating UDP segments. The connection works, so I know ipfilter is letting some traffic through, it is just a lot slower than it should be. Rules that allow the... (3 Replies)
Discussion started by: ilikecows
3 Replies

8. UNIX for Advanced & Expert Users

fragments in Solaris 8

When discussing inodes and data blocks, I know Solaris creates these data blocks with a total size of 8192b, divided into eight 1024b "fragments." It stores data in "contiguous" fragments and solaris doesn't allow a file to use portions of two different fragments. If the file size permits, then the... (4 Replies)
Discussion started by: manderson19
4 Replies
Login or Register to Ask a Question