capturing info between words.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting capturing info between words.
# 1  
Old 04-29-2010
capturing info between words.

Dear Friends,
I am facing 2 problems while writing a script
1) I have a flat file and I want to captur specific information from it.

Example
I have this string in the file

PACK: P42 77 UNPACK: MHTT DMK

I want to capture whatever is between word PACK: and word UNPACK: including spaces.

Expected o/p
P42 77

2) I want to capture next up to 20 characters appearing after pattern UNPACK: including spaces.

Example:
I have
PACK: P42 77 UNPACK: MHTT DMK

Expected o/p
MHTT DMK



Kindly guide me in captuing it.

Last edited by anushree.a; 04-29-2010 at 09:48 AM.. Reason: Code tags, please!
# 2  
Old 04-29-2010
Code:
# eval $(echo PACK: P42 77 UNPACK: MHTT DMK | awk '{sub("PACK: ","VAR1=\"");sub(" UNPACK: ","\";VAR2=\"");$0=$0"\""}1')
# echo $VAR1
P42 77
# echo $VAR2
MHTT DMK

# 3  
Old 04-29-2010
GNU sed:
Code:
$> sed 's/^PACK:[ \t]*\(.*\)UNPACK.*/\1/g' infile
P42 77
$> sed 's/.*UNPACK:[ \t]*\(.\{,20\}\).*/\1/g' infile
MHTT DMK

You have 104 posts already and still don't use code tags. Please edit your post accordingly as commented by radoulov. If have problems using code tags, let us know, ty.
# 4  
Old 04-29-2010
Please help me in finding code tags document so that i can start following it.
# 5  
Old 04-29-2010
Quote:
Originally Posted by anushree.a
Please help me in finding code tags document so that i can start following it.
Check Forum Rule (B): The UNIX and Linux Forums - Forum Rules
# 6  
Old 04-29-2010
Some misunderstanding.

I have
Code:
READY: BLPS3Q   PACK: DBL25464         UNPACK: UY3546P4

I want
Code:
DBL25464         in op.txt
UY354758         in op1.txt

here everything is variable in length except words READY:, PACK: and UNPACK:
We want to send anything appearing between word PACK: and UNPACK: to op.txt
Similarly next 20 characters of word UNPACK:

Last edited by zaxxon; 04-29-2010 at 11:58 AM.. Reason: you may guess... :)
# 7  
Old 04-29-2010
Code:
echo READY: BLPS3Q   PACK: DBL25464         UNPACK: UY3546P4 | awk -F:\  '{gsub("[PACK|UNPACK]",X);print $3 > "op.txt";print $NF > "op1.txt"}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. UNIX for Dummies Questions & Answers

Deleting words between every appearance of two words

Hi there, newbie there. I've been browsing the forums hoping to find a solution that answers a problem similar to what I need, but haven't had much luck. Any help would be greatly appreciated. Thanks! I need to delete a bunch of text between every appearance of two words in a really large file... (3 Replies)
Discussion started by: lendl
3 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

6. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

7. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. UNIX for Dummies Questions & Answers

Adding words after a set of words

Greetings. I am a UNIX newbies. I am currently facing difficulties dealing with a large data set and I would like to ask for helps. I have a input file like this: ak 1 AAM1 ak 2 AAM1 ak 3 AAM1 ak 11 AMM2 ak 12 AMM2 ak 13 AMM2 ak 14 AMM2 Is there any possibility for me to... (7 Replies)
Discussion started by: Amanda Low
7 Replies

10. Shell Programming and Scripting

Help on capturing /etc/group info.....!!

Gurus I am trying to capture all the data in /etc/group file in a CSV ,thru a fingerprinting engine. For hosts having ,unique group names and Ids ,following code works fine. Trouble starts when on a host,there are multiple groups defined with same name and id. e.g One of my hosts has 8... (10 Replies)
Discussion started by: ak835
10 Replies
Login or Register to Ask a Question