Get value in between a pattern from each line in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get value in between a pattern from each line in file
# 8  
Old 07-28-2009
Try this:
Code:
sed 's~^.*<XYZ>\(.*\)</XYZ>.*$~\1~' filename

# 9  
Old 07-28-2009
Nice use of regular expressions.
# 10  
Old 08-06-2009
Capture this value in variable ????

Thnx Guyz,
The above code also worked as expected. Smilie

Now, I want this value in between the tags to be captured in a variable.
I want to search my file for number of occurances for this value.

Eg.
If i have the following lines in my file:
Quote:
<ABC>hello</ABC><XYZ>I want This</XYZ><CDF>World</CDF>
<ABC>Vzup</ABC><XYZ>This data is Needful</XYZ><CDF>Moon</CDF>
<ABC>hello</ABC><XYZ>I want This</XYZ><CDF>World</CDF>
<ABC>hello</ABC><XYZ>I want This</XYZ><CDF>World</CDF>
<ABC>Vzup</ABC><XYZ>This data is Needful</XYZ><CDF>Moon</CDF>
<ABC>Vzup</ABC><XYZ>Search further</XYZ><CDF>Moon</CDF>
I want to count the number of occurances for each of these values inside tag <XYZ>
Ths, i want to capture this value in a variable.
So, i can further use the value of this variable in
Code:
grep -c $<Variable> <MyFile>

I tried it but i am able to get this value in a file and not a variable.

Help !!!
# 11  
Old 08-06-2009
Code:
 
sed 's~^.*<XYZ>\(.*\)</XYZ>.*$~\1~' xml4.xml > temp.txt
sed 's~^.*<XYZ>\(.*\)</XYZ>.*$~\1~' xml4.xml | sort -u | \
while read xx ; do
    tmps=$(egrep -c "$xx" temp.txt)
    echo "$xx="$tmps 
done

# 12  
Old 08-06-2009
Maybe a command like this can do the trick:
Code:
sed 's:.*<XYZ>\(.*\)</XYZ>.*:\1:g;' yourfile.txt | sort | uniq -c

# 13  
Old 08-11-2009
Bug Thankyou !!!!

Hey Guyz,
Both the codes worked as expected.
Thanks a lot.
Cheers Smilie
# 14  
Old 08-11-2009
Quote:
Originally Posted by thanhdat
Maybe a command like this can do the trick:
Code:
sed 's:.*<XYZ>\(.*\)</XYZ>.*:\1:g;' yourfile.txt | sort | uniq -c

Wov...that is nice.
I totally forgot about uniq -c option.
Good job.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

3. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

4. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. UNIX for Dummies Questions & Answers

Extracting line1 from a file with certain file pattern in line 7

Hello there, I am new to unix and would like to do the following, hoping someone would give some guide, thanks in advance. Lets say i have a file like this: A w x y w x 0.1 B w x y w x 0.3 C w x y w x 0.7 D w x y w x 0.9 E w x y w x 0.2 So i would like to extract line 1 data where line... (2 Replies)
Discussion started by: seiksoon
2 Replies

7. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

8. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

9. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

10. Shell Programming and Scripting

how to move the line after a certain pattern in the file

Hi, I have a file called /bb/bin/rstrt. I need to move the line/entry "ccpm_load_shared_memory" after the entry "initcorp". The problem is that there are several entries for "initcorp" in this file and I need the entry to be moved only after the first instance of "initcorp" Is there a way... (5 Replies)
Discussion started by: aoussenko
5 Replies
Login or Register to Ask a Question