Help: Need to Print a string from a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help: Need to Print a string from a word
# 1  
Old 07-18-2011
Help: Need to Print a string from a word

Hi Folks

Can you please help me to grep a word from below test file

Below is the file looks like
ABCD Test[21455]:3 ZZZZYYYZZ
ABCD TEST[324494]:38 XXXYYYZZZ

I need below output
echo $A=21455
echo $B=3

and for second line

echo $A=324494
echo $B=38

I will use While read line for each line.. i just need the logic to grep those Strings from a word

Please hlp
Thanks
Machha
# 2  
Old 07-18-2011
Try this:

PHP Code:
echo $line sed 's/.*\[\(.*\)\]:\(.*\) .*/\1 \2/' read a b 
# 3  
Old 07-18-2011
Getting this

sed: command garbled: s/.*\[\(.*\)\]:\(.*\) .*/First part=\1

---------- Post updated at 02:29 PM ---------- Previous update was at 02:28 PM ----------

Command which I'm trying to run is as below

XXXX=`echo $line | sed 's/.*\[\(.*\)\]:\(.*\) .*/First part=\1'`
# 4  
Old 07-18-2011
Quote:
Originally Posted by Machha
Getting this

sed: command garbled: s/.*\[\(.*\)\]:\(.*\) .*/First part=\1

---------- Post updated at 02:29 PM ---------- Previous update was at 02:28 PM ----------

Command which I'm trying to run is as below

XXXX=`echo $line | sed 's/.*\[\(.*\)\]:\(.*\) .*/First part=\1'`
It works well for me.Which sed you are using?

You can even try the below one in awk,
PHP Code:
echo $line awk -F"[\]:\[]" '{ print $2" "$4; }' read a b 
# 5  
Old 07-18-2011
Nope its not working for me

Getting as below

root:375$ cat test1.txt
ABCD Test[21455]:3 ZZZZYYYZZ
root:376$ cat test1.txt | awk -F"[\]:\[]" '{ print $2" "$4; }' | read a b
root:377$ echo $a
21455]:3
root:378$ echo $b
ZZZZYYYZZ


I need

root:377$ echo $a
21455
root:378$ echo $b
3

Thanks
# 6  
Old 07-18-2011
Hi,

Please check below


$ cat read
ABCD Test[21455]:3 ZZZZYYYZZ
ABCD TEST[324494]:38 XXXYYYZZZ


$ cat read | sed 's/\(.*\)\[\(.*\)\]:\(.*\) \(.*\)/\2 \3/'
21455 3
324494 38
# 7  
Old 07-18-2011
Quote:
Originally Posted by Machha
Getting this

sed: command garbled: s/.*\[\(.*\)\]:\(.*\) .*/First part=\1

---------- Post updated at 02:29 PM ---------- Previous update was at 02:28 PM ----------

Command which I'm trying to run is as below

XXXX=`echo $line | sed 's/.*\[\(.*\)\]:\(.*\) .*/First part=\1'`
That's because you did not close the substitute command..
Code:
XXXX=`echo $line | sed 's/.*\[\(.*\)\]:\(.*\) .*/First part=\1/'`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

3. Shell Programming and Scripting

Print string after the word match

Hi, I have the logs : cat logsx.txt 744906,{"reportingGroups":,"version":"2.0"} 678874,{"reportingGroups":,"version":"2.0"} 193571,{"reportingGroups":,"version":"2.0"} 811537,{"reportingGroups":,"version":"2.0"} 772024,{"reportingGroups":,"version":"2.0"}... (5 Replies)
Discussion started by: justbow
5 Replies

4. Shell Programming and Scripting

Print the word after the string

Hi I have a requirment here. I have to out the string after the particular word. for example i have the to extract the first word after the word disk. help me out. i have tried the folloing code but it is not giving the output which i need. awk -F"*disk " '{print $1}' grep -n -o '' file Input... (2 Replies)
Discussion started by: saaisiva
2 Replies

5. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

8. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

9. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies
Login or Register to Ask a Question