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
# 1  
Old 07-16-2009
Question Get value in between a pattern from each line in file

Hi Guyz,

I have a flat file with some data as below:-

Quote:
<ABC>hello</ABC><XYZ>I want This</XYZ><CDF>World</CDF>
<ABC>Vzup</ABC><XYZ>This data is Needful</XYZ><CDF>Moon</CDF>
I need only the data within tags <XYZ>, from each line of the file.

My desired output:-

Quote:
I want This
This data is Needful


Tried a lot, but not getting the desired result.
Thnx in advance.
# 2  
Old 07-16-2009
This might help.
# 3  
Old 07-16-2009
Try...
Code:
awk -F '</?XYZ>' 'NF==3{print $2}' file1

# 4  
Old 07-28-2009
Thankz

Thnx Ygor.
It worked. Smilie
# 5  
Old 07-28-2009
There is overreliance on awk. There are cases in which files are very large (log files) and using awk can literally take minutes to process such files.
The most efficient way is to use regular expressions.
Try this:
Code:
grep 'XYZ>..*</XYZ' filename

# 6  
Old 07-28-2009
Quote:
Originally Posted by gch
There is overreliance on awk. There are cases in which files are very large (log files) and using awk can literally take minutes to process such files.
The most efficient way is to use regular expressions.
Try this:
Code:
grep 'XYZ>..*</XYZ' filename

GCH
What do you think this will do?
What is the requirement?
Did you test it?
This will just list all the lines once again.
That will be garbage-in-garbage-out.
# 7  
Old 07-28-2009
Quote:
Originally Posted by edidataguy
GCH
What do you think this will do?
What is the requirement?
Did you test it?
This will just list all the lines once again.
That will be garbage-in-garbage-out.
Sorry. I was doing too many things at the time. I did not paste the correct line.
This is it:
Code:
cut -d \> -f4 filename | cut -d\< -f1

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