Visit Our UNIX and Linux User Community


Search for string in a file, extract two another strings and concatenate to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for string in a file, extract two another strings and concatenate to a variable
# 1  
Old 06-20-2012
Question Search for string in a file, extract two another strings and concatenate to a variable

I have a file with
<suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux">
Need to find word "build" then
extract build number, which is 19921 also
release number, which is 6.1.5 then
concatenate them to one variable as "6.1.5_19921".

I understand how to do last step but how should I do with first two...Smilie
Please show me. Thank you.
# 2  
Old 06-20-2012
Handling XML isn't trivial. It's not one "thing", it's a lot of things.

How I'd suggest doing it is breaking down the tag into multiple lines so you can read "date=Trump Tue 06/19/2012 11:41 AM EDT", then "machine=garg-ln", etc, etc, until the tag is done. xargs can do that easily.

Then you read one by one, splitting on = or < or > into two variables, catch the names you want and ignore the rest.

Code:
#!/bin/sh

# Remove temp file on exit
trap "rm -f /tmp/$$" EXIT

# Little-known incredibly useful property of xargs:  it handles quotes.
# We can use it to transform x='a' y='b c d' into lines like
#
#       x=a
#       y=b c d
#       ...
#
# Ordinary 'while read' loop can handle that easily, splitting on the first =.
xargs -n 1 < file > /tmp/$$

while IFS="=<>" read NAME VALUE
do
        case "$NAME" in
        build)          BUILD="$VALUE"          ;;
        release)        RELEASE="$VALUE"        ;;
        *)                                      ;;
        esac
done < /tmp/$$

echo "${RELEASE}_${BUILD}"

# 3  
Old 06-20-2012
Thank you very much for quick response I'll try it, and let you know how it works! Thank you !!!

P.S. Looks like missed open brackets in loop, is it correct?

Last edited by garg; 06-20-2012 at 11:32 PM..
# 4  
Old 06-21-2012
This code worked, nothing's missing AFAIK.

What brackets do you think I'm missing? Shell doesn't use brackets to denote code blocks. ( ) denote subshells but aren't the same thing; and if you don't know if you need a subshell, you probably don't.
# 5  
Old 06-21-2012
Thank you,Corona688! One more question:
I have to change " < file > "to my real file name with path ( /usr/arch/suite.xml )
Should I change $$ in /tmp/$$ ?..
Sorry I just started to scripting and observe any information about.
Thank you again!

---------- Post updated at 09:47 AM ---------- Previous update was at 09:23 AM ----------

I found one more thing, from this thread:_https://www.unix.com/shell-programming-scripting/105016-search-string-file-extract-another-string-variable.html (since I don't have permission to publish URLs yet, I posted like that)
Code:
MYVAR=`awk -F'"' '/build/ { print $6 "_" $10 }' suite.xml`
export MYVAR
echo $MYVAR

It looks like
Code:
iway@garg-ln:~/scripts> ./new.sh
30772_6.1.5

Of cause it will work only for specific string,
and I still waiting for your advises, your code and idea I like better...
Thank you for your time and consideration!
# 6  
Old 06-21-2012
Quote:
Originally Posted by garg
Thank you,Corona688! One more question:
I have to change " < file > "to my real file name with path ( /usr/arch/suite.xml )
Naturally you have to give it the real file's location one way or another. The < > must stay, though! That's redirection, not fluff.
Quote:
Should I change $$ in /tmp/$$ ?..
Why change it? Does it not work?

$$ is the shell's PID, which I use to pick a unique temporary file. /tmp/ is the location for temporary files.
Quote:
I found one more thing, from this thread:_https://www.unix.com/shell-programming-scripting/105016-search-string-file-extract-another-string-variable.html (since I don't have permission to publish URLs yet, I posted like that)
Code:
MYVAR=`awk -F'"' '/build/ { print $6 "_" $10 }' suite.xml`
export MYVAR
echo $MYVAR

It looks like
Code:
iway@garg-ln:~/scripts> ./new.sh
30772_6.1.5

Of cause it will work only for specific string
If the order of things changes, it will break down. If it uses ' instead of ", it will break down. If they split the tag across lines, it will break down.
Quote:
I still waiting for your advises, your code and idea I like better...
Thank you for your time and consideration!
What advice are you needing? You figured out the one thing you needed to change -- the file location -- yourself. Does it work or not?

If it doesn't work, post the complete script you have including all changes you've made.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 06-21-2012
Yes! it works!
I just missed '<' suite.xml '>' - since I insert them it works perfect!
Thank you very much!
And previous code wasn't good enough you described what I thought.
Thank you for you help.
This User Gave Thanks to garg For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #68
Difficulty: Easy
Awk is abbreviated from the names of the developers - Aho, Weinberger, and Kernighan.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Extract a string from a file and store it in variable

Hi, I've a file ImageSizeDetails.txt with the following contents: Image Name: swncd 01.10.00.04 Created: Wed Jan 9 14:05:48 2013 Image Type: ARM Linux Multi-File Image (gzip compressed) Data Size: 7351011 Bytes = 7178.72 kB = 7.01 MB Load Address: 00008000 Entry Point: ... (6 Replies)
Discussion started by: Parameswaran
6 Replies

4. Shell Programming and Scripting

Search and Extract data between two strings

hi, In a given directory, i need to search for a string (eg:ABCD). For a given file, i have to extract the text between START and END strings . I need to extract all the text between START and END and there can be multiple START and END in a file. Sample: There is a directort... (3 Replies)
Discussion started by: flamingo_l
3 Replies

5. UNIX for Dummies Questions & Answers

Concatenate a string to a variable

Hello All, How to concatenate a string to a variable in a script I'm having a file which is consisting of data and i need to extract the first line of the file and append it to a string. /tmp/samp.list containg 60000 I like to concatenate it with a string (SS_) grep -w SS_$(head -1... (1 Reply)
Discussion started by: nkamalkishore
1 Replies

6. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies

7. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

8. Shell Programming and Scripting

using sed to conditionally extract stanzas of a file based on a search string

Dear All, I have a file with the syntax below (composed of several <log ..... </log> stanzas) I need to search this file for a number e.g. 2348022225919, and if it is found in a stanza, copy the whole stanza/section (<log .... </log>) to another output file. The numbers to search for are... (0 Replies)
Discussion started by: aitayemi
0 Replies

9. Shell Programming and Scripting

How to concatenate a string and a variable

I need a way to build variable in this manner: variable_$i Inside a for loop i need to create it. where i goes from 1 to 30.. and then i need to print them on screen with echo $variable_$i which is the best way to do this? (6 Replies)
Discussion started by: sreedivia
6 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies

Featured Tech Videos